|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
A job queue with comprehensive backpressure mechanisms. More...
#include <backpressure_job_queue.h>


Public Member Functions | |
| backpressure_job_queue (std::size_t max_size, backpressure_config config=backpressure_config{}) | |
| Constructs a backpressure-aware job queue. | |
| ~backpressure_job_queue () override | |
| Virtual destructor. | |
| backpressure_job_queue (const backpressure_job_queue &)=delete | |
| backpressure_job_queue & | operator= (const backpressure_job_queue &)=delete |
| backpressure_job_queue (backpressure_job_queue &&)=delete | |
| backpressure_job_queue & | operator= (backpressure_job_queue &&)=delete |
| auto | enqueue (std::unique_ptr< job > &&value) -> common::VoidResult override |
| Enqueues a job with backpressure handling. | |
| auto | enqueue_batch (std::vector< std::unique_ptr< job > > &&jobs) -> common::VoidResult override |
| Enqueues a batch of jobs with backpressure handling. | |
| auto | get_pressure_level () const -> pressure_level |
| Returns the current pressure level. | |
| auto | get_pressure_ratio () const -> double |
| Returns the current pressure as a ratio. | |
| auto | set_backpressure_config (backpressure_config config) -> void |
| Sets the backpressure configuration. | |
| auto | get_backpressure_config () const -> const backpressure_config & |
| Returns the current backpressure configuration. | |
| auto | is_rate_limited () const -> bool |
| Checks if rate limiting is causing delays. | |
| auto | get_available_tokens () const -> std::size_t |
| Returns available rate limit tokens. | |
| auto | get_backpressure_stats () const -> backpressure_stats_snapshot |
| Returns backpressure statistics snapshot. | |
| auto | reset_stats () -> void |
| Resets backpressure statistics. | |
| auto | to_string () const -> std::string override |
| Returns string representation including backpressure state. | |
Public Member Functions inherited from kcenon::thread::job_queue | |
| job_queue (std::optional< std::size_t > max_size=std::nullopt) | |
Constructs a new, empty job_queue. | |
| virtual | ~job_queue (void) |
Virtual destructor. Cleans up resources used by the job_queue. | |
| auto | get_ptr (void) -> std::shared_ptr< job_queue > |
Obtains a std::shared_ptr that points to this queue instance. | |
| auto | is_stopped () const -> bool |
| Checks if the queue is in a "stopped" state. | |
| auto | set_notify (bool notify) -> void |
| Sets the 'notify' flag for this queue. | |
| auto | schedule (std::unique_ptr< job > &&value) -> common::VoidResult override |
| scheduler_interface implementation: schedule a job | |
| auto | get_next_job () -> common::Result< std::unique_ptr< job > > override |
| scheduler_interface implementation: get next job | |
| template<typename JobType , typename = std::enable_if_t<std::is_base_of_v<job, JobType>>> | |
| auto | enqueue (std::unique_ptr< JobType > &&value) -> common::VoidResult |
| Type-safe enqueue for job subclasses. | |
| virtual auto | dequeue (void) -> common::Result< std::unique_ptr< job > > |
| Dequeues a job from the queue in FIFO order (blocking operation). | |
| virtual auto | try_dequeue (void) -> common::Result< std::unique_ptr< job > > |
| Attempts to dequeue a job from the queue without blocking. | |
| virtual auto | dequeue_batch (void) -> std::deque< std::unique_ptr< job > > |
| Dequeues all remaining jobs from the queue without processing them. | |
| virtual auto | dequeue_batch_limited (std::size_t max_count) -> std::deque< std::unique_ptr< job > > |
| Dequeues up to N jobs in a single operation (micro-batching). | |
| virtual auto | clear (void) -> void |
| Removes all jobs currently in the queue without processing them. | |
| auto | empty (void) const -> bool |
| Checks if the queue is currently empty. | |
| auto | size (void) const -> std::size_t |
| Returns the current number of jobs in the queue. | |
| auto | get_memory_stats () const -> memory_stats |
| Get memory footprint statistics for debugging and monitoring. | |
| auto | stop (void) -> void |
| Signals the queue to stop waiting for new jobs (e.g., during shutdown). | |
| auto | get_capabilities () const -> queue_capabilities override |
| Returns capabilities of this job_queue implementation. | |
| auto | is_bounded () const -> bool |
| Check if queue has a size limit. | |
| auto | get_max_size () const -> std::optional< std::size_t > |
| Get the maximum queue size. | |
| auto | set_max_size (std::optional< std::size_t > max_size) -> void |
| Set maximum queue size. | |
| auto | is_full () const -> bool |
| Check if queue is at capacity. | |
| virtual auto | inspect_pending_jobs (std::size_t limit=100) const -> std::vector< diagnostics::job_info > |
| Inspects pending jobs in the queue without removing them. | |
Public Member Functions inherited from kcenon::thread::scheduler_interface | |
| virtual | ~scheduler_interface ()=default |
Public Member Functions inherited from kcenon::thread::queue_capabilities_interface | |
| virtual | ~queue_capabilities_interface ()=default |
| auto | has_exact_size () const -> bool |
| Check if size() returns exact values. | |
| auto | has_atomic_empty () const -> bool |
| Check if empty() check is atomic. | |
| auto | is_lock_free () const -> bool |
| Check if this is a lock-free implementation. | |
| auto | is_wait_free () const -> bool |
| Check if this is a wait-free implementation. | |
| auto | supports_batch () const -> bool |
| Check if batch operations are supported. | |
| auto | supports_blocking_wait () const -> bool |
| Check if blocking wait is supported. | |
| auto | supports_stop () const -> bool |
| Check if stop signaling is supported. | |
Private Member Functions | |
| auto | apply_backpressure (std::unique_ptr< job > &&value) -> common::VoidResult |
| Applies backpressure logic for a single job. | |
| auto | apply_rate_limiting () -> bool |
| Applies rate limiting check. | |
| auto | update_pressure_state () -> void |
| Updates pressure level and triggers callbacks if changed. | |
| auto | handle_block_policy (std::unique_ptr< job > &&value) -> common::VoidResult |
| Handles blocking policy with timeout. | |
| auto | handle_drop_oldest_policy (std::unique_ptr< job > &&value) -> common::VoidResult |
| Handles drop_oldest policy. | |
| auto | handle_callback_policy (std::unique_ptr< job > &&value) -> common::VoidResult |
| Handles callback policy. | |
| auto | handle_adaptive_policy (std::unique_ptr< job > &&value) -> common::VoidResult |
| Handles adaptive policy. | |
| auto | direct_enqueue (std::unique_ptr< job > &&value) -> common::VoidResult |
| Directly enqueues without backpressure (internal use). | |
Private Attributes | |
| backpressure_config | config_ |
| Backpressure configuration. | |
| std::mutex | config_mutex_ |
| Mutex for configuration access. | |
| std::unique_ptr< token_bucket > | rate_limiter_ |
| Token bucket rate limiter (nullptr if disabled). | |
| std::atomic< pressure_level > | current_pressure_ {pressure_level::none} |
| Current pressure level (atomic for lock-free reads). | |
| std::atomic< double > | current_pressure_ratio_ {0.0} |
| Current pressure ratio (atomic for lock-free reads). | |
| backpressure_stats | stats_ |
| Backpressure statistics. | |
| std::condition_variable | space_available_ |
| Condition variable for blocking policy wait. | |
Additional Inherited Members | |
Protected Attributes inherited from kcenon::thread::job_queue | |
| std::atomic_bool | notify_ |
If true, threads waiting for new jobs are notified when a new job is enqueued. If false, enqueuing does not automatically trigger a notification. | |
| std::atomic_bool | stop_ |
| Indicates whether the queue has been signaled to stop. | |
| std::mutex | mutex_ |
Mutex to protect access to the underlying queue_ container and related state. | |
| std::condition_variable | condition_ |
| Condition variable used to signal worker threads. | |
A job queue with comprehensive backpressure mechanisms.
Extends job_queue with backpressure handling to prevent resource exhaustion and provide graceful degradation under high load.
All methods are thread-safe. The queue inherits mutex-based synchronization from job_queue.
Definition at line 87 of file backpressure_job_queue.h.
|
explicit |
Constructs a backpressure-aware job queue.
| max_size | Maximum queue capacity. |
| config | Backpressure configuration. |
The queue is immediately ready for use after construction. If rate limiting is enabled, the token bucket is initialized with the specified parameters.
Implementation details:
| max_size | Maximum queue capacity |
| config | Backpressure configuration |
Definition at line 34 of file backpressure_job_queue.cpp.
References config_, and kcenon::thread::backpressure_config::enable_rate_limiting.
|
overridedefault |
Virtual destructor.
Destructor.
|
delete |
|
delete |
|
nodiscardprivate |
Applies backpressure logic for a single job.
Core backpressure implementation for single job.
| value | The job to potentially enqueue. |
This is the core backpressure implementation that handles all policy logic, rate limiting, and statistics tracking.
Implementation details:
| value | The job to enqueue |
Definition at line 252 of file backpressure_job_queue.cpp.
References kcenon::thread::adaptive, kcenon::thread::block, kcenon::thread::callback, kcenon::thread::drop_newest, kcenon::thread::drop_oldest, kcenon::thread::result< T >::is_ok(), kcenon::thread::make_error_result(), kcenon::thread::operation_timeout, and kcenon::thread::queue_full.

|
nodiscardprivate |
Applies rate limiting check.
Applies rate limiting using token bucket.
Definition at line 315 of file backpressure_job_queue.cpp.
|
nodiscardprivate |
Directly enqueues without backpressure (internal use).
Directly enqueues to base class without backpressure.
| value | The job to enqueue. |
Definition at line 598 of file backpressure_job_queue.cpp.
References kcenon::thread::job_queue::enqueue().

|
nodiscardoverridevirtual |
Enqueues a job with backpressure handling.
| value | The job to enqueue. |
Depending on the configured policy:
Rate limiting (if enabled) is applied before policy-based handling.
Error Codes:
Implementation details:
| value | The job to enqueue |
Reimplemented from kcenon::thread::job_queue.
Definition at line 68 of file backpressure_job_queue.cpp.
References kcenon::thread::invalid_argument, kcenon::thread::make_error_result(), and kcenon::thread::queue_stopped.

|
nodiscardoverridevirtual |
Enqueues a batch of jobs with backpressure handling.
| jobs | Vector of jobs to enqueue. |
Batch enqueue respects backpressure settings. If the batch would exceed capacity, behavior depends on policy:
Implementation details:
| jobs | Vector of jobs to enqueue |
Reimplemented from kcenon::thread::job_queue.
Definition at line 97 of file backpressure_job_queue.cpp.
References kcenon::thread::block, kcenon::thread::drop_oldest, kcenon::thread::job_queue::enqueue_batch(), kcenon::thread::invalid_argument, kcenon::thread::result< T >::is_ok(), kcenon::thread::make_error_result(), kcenon::thread::operation_timeout, kcenon::thread::queue_full, and kcenon::thread::queue_stopped.

|
nodiscard |
Returns available rate limit tokens.
Definition at line 684 of file backpressure_job_queue.cpp.
References config_, kcenon::thread::backpressure_config::enable_rate_limiting, and rate_limiter_.
|
nodiscard |
Returns the current backpressure configuration.
Returns current configuration.
Definition at line 662 of file backpressure_job_queue.cpp.
References config_.
|
nodiscard |
Returns backpressure statistics snapshot.
Returns a snapshot of current stats. For ongoing monitoring, call periodically.
Definition at line 696 of file backpressure_job_queue.cpp.
References kcenon::thread::backpressure_stats::snapshot(), and stats_.

|
nodiscard |
Returns the current pressure level.
Returns current pressure level.
Calculated based on current queue depth relative to watermarks:
Definition at line 607 of file backpressure_job_queue.cpp.
References current_pressure_.
Referenced by to_string().

|
nodiscard |
Returns the current pressure as a ratio.
Returns current pressure ratio.
Can exceed 1.0 if queue somehow exceeds max_size (shouldn't happen with proper backpressure, but included for robustness).
Definition at line 615 of file backpressure_job_queue.cpp.
References kcenon::thread::job_queue::get_max_size(), and kcenon::thread::job_queue::size().
Referenced by to_string().


|
nodiscardprivate |
Handles adaptive policy.
| value | The job to potentially enqueue. |
Implementation details:
Definition at line 525 of file backpressure_job_queue.cpp.
References kcenon::thread::result< T >::is_ok(), kcenon::thread::make_error_result(), and kcenon::thread::queue_full.

|
nodiscardprivate |
Handles blocking policy with timeout.
| value | The job to enqueue. |
Definition at line 405 of file backpressure_job_queue.cpp.
References kcenon::thread::result< T >::is_ok(), kcenon::thread::make_error_result(), and kcenon::thread::operation_timeout.

|
nodiscardprivate |
Handles callback policy.
| value | The job to potentially enqueue. |
Definition at line 477 of file backpressure_job_queue.cpp.
References kcenon::thread::accept, kcenon::thread::delay, kcenon::thread::drop_and_accept, kcenon::thread::job_queue::enqueue(), kcenon::thread::make_error_result(), kcenon::thread::queue_busy, kcenon::thread::queue_full, and kcenon::thread::reject.

|
nodiscardprivate |
Handles drop_oldest policy.
| value | The job to enqueue after dropping oldest. |
Definition at line 457 of file backpressure_job_queue.cpp.
References kcenon::thread::result< T >::is_ok().

|
nodiscard |
Checks if rate limiting is causing delays.
Checks if rate limiting is active.
Returns false if rate limiting is disabled.
Definition at line 670 of file backpressure_job_queue.cpp.
References config_, kcenon::thread::backpressure_config::enable_rate_limiting, kcenon::thread::backpressure_config::rate_limit_burst_size, and rate_limiter_.
|
delete |
|
delete |
| auto kcenon::thread::backpressure_job_queue::reset_stats | ( | ) | -> void |
Resets backpressure statistics.
Resets statistics.
Definition at line 704 of file backpressure_job_queue.cpp.
| auto kcenon::thread::backpressure_job_queue::set_backpressure_config | ( | backpressure_config | config | ) | -> void |
Sets the backpressure configuration.
Sets backpressure configuration.
| config | New configuration to apply. |
Updates take effect immediately. If rate limiting is being enabled or its parameters change, the token bucket is recreated or updated.
Definition at line 629 of file backpressure_job_queue.cpp.
|
nodiscardoverridevirtual |
Returns string representation including backpressure state.
Returns string representation.
Reimplemented from kcenon::thread::job_queue.
Definition at line 712 of file backpressure_job_queue.cpp.
References kcenon::thread::backpressure_policy_to_string(), config_, utility_module::formatter::format(), get_pressure_level(), get_pressure_ratio(), kcenon::thread::backpressure_config::policy, kcenon::thread::pressure_level_to_string(), and kcenon::thread::job_queue::size().

|
private |
Updates pressure level and triggers callbacks if changed.
Updates pressure state and triggers callbacks.
Definition at line 345 of file backpressure_job_queue.cpp.
References kcenon::thread::critical, kcenon::thread::high, kcenon::thread::low, and kcenon::thread::none.
|
private |
Backpressure configuration.
Definition at line 302 of file backpressure_job_queue.h.
Referenced by backpressure_job_queue(), get_available_tokens(), get_backpressure_config(), is_rate_limited(), and to_string().
|
mutableprivate |
Mutex for configuration access.
Definition at line 307 of file backpressure_job_queue.h.
|
private |
Current pressure level (atomic for lock-free reads).
Definition at line 317 of file backpressure_job_queue.h.
Referenced by get_pressure_level().
|
private |
Current pressure ratio (atomic for lock-free reads).
Definition at line 322 of file backpressure_job_queue.h.
|
private |
Token bucket rate limiter (nullptr if disabled).
Definition at line 312 of file backpressure_job_queue.h.
Referenced by get_available_tokens(), and is_rate_limited().
|
private |
Condition variable for blocking policy wait.
Definition at line 332 of file backpressure_job_queue.h.
|
private |
Backpressure statistics.
Definition at line 327 of file backpressure_job_queue.h.
Referenced by get_backpressure_stats().