|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Lock-free Multi-Producer Multi-Consumer (MPMC) job queue (Internal implementation) More...
#include <lockfree_job_queue.h>


Classes | |
| struct | node |
| Internal queue node structure. More... | |
| class | node_pool |
| Lock-free node freelist (Treiber stack) for node recycling. More... | |
Public Member Functions | |
| lockfree_job_queue () | |
| Constructs an empty lock-free job queue. | |
| ~lockfree_job_queue () | |
| Destructor. | |
| lockfree_job_queue (const lockfree_job_queue &)=delete | |
| lockfree_job_queue & | operator= (const lockfree_job_queue &)=delete |
| lockfree_job_queue (lockfree_job_queue &&)=delete | |
| lockfree_job_queue & | operator= (lockfree_job_queue &&)=delete |
| auto | enqueue (std::unique_ptr< job > &&job) -> common::VoidResult |
| Enqueues a job into the queue (thread-safe) | |
| auto | dequeue () -> common::Result< std::unique_ptr< job > > |
| Dequeues a job from the queue (thread-safe) | |
| auto | try_dequeue () -> common::Result< std::unique_ptr< job > > |
| Tries to dequeue a job without blocking. | |
| auto | empty () const -> bool |
| Checks if the queue is empty. | |
| auto | size () const -> std::size_t |
| Gets approximate queue size. | |
| auto | schedule (std::unique_ptr< job > &&work) -> common::VoidResult override |
| Schedule a job (delegates to enqueue) | |
| auto | get_next_job () -> common::Result< std::unique_ptr< job > > override |
| Get next job (delegates to dequeue) | |
| auto | get_capabilities () const -> queue_capabilities override |
| Returns capabilities of lockfree_job_queue. | |
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 Types | |
| using | node_hp_domain = typed_safe_hazard_domain<node> |
Private Member Functions | |
| void | retire_node (node *n) |
| Retire a node through hazard pointers, recycling via pool on reclamation. | |
Private Attributes | |
| std::atomic< node * > | head_ |
| std::atomic< node * > | tail_ |
| std::shared_ptr< node_pool > | pool_ |
| std::atomic< std::size_t > | approximate_size_ {0} |
| std::atomic< bool > | shutdown_ {false} |
Lock-free Multi-Producer Multi-Consumer (MPMC) job queue (Internal implementation)
This class implements a lock-free MPMC queue using the Michael-Scott algorithm with Safe Hazard Pointers for memory reclamation. It uses explicit memory ordering to ensure correctness on weak memory model architectures (ARM, etc.)
Algorithm: Michael-Scott Queue (1996) Memory Reclamation: Safe Hazard Pointers with explicit memory ordering
Key Features:
Performance Characteristics:
Thread Safety:
Definition at line 63 of file lockfree_job_queue.h.
|
private |
Definition at line 312 of file lockfree_job_queue.h.
| kcenon::thread::detail::lockfree_job_queue::lockfree_job_queue | ( | ) |
Constructs an empty lock-free job queue.
Initializes the queue with a dummy node to simplify the algorithm. The dummy node is never removed, allowing concurrent enqueue/dequeue.
Definition at line 41 of file lockfree_job_queue.cpp.
References approximate_size_, head_, and tail_.
| kcenon::thread::detail::lockfree_job_queue::~lockfree_job_queue | ( | ) |
Destructor.
Drains the queue and reclaims all nodes. Thread-safe even if other threads are still accessing the queue (they will get errors).
Definition at line 53 of file lockfree_job_queue.cpp.
References dequeue(), head_, retire_node(), and shutdown_.

|
delete |
|
delete |
|
nodiscard |
Dequeues a job from the queue (thread-safe)
Time Complexity: O(1) amortized Memory Ordering: acquire/release semantics
Definition at line 144 of file lockfree_job_queue.cpp.
References kcenon::thread::detail::lockfree_job_queue::node::data, kcenon::thread::detail::lockfree_job_queue::node::next, kcenon::thread::safe_hazard_guard::protect(), and kcenon::thread::queue_empty.
Referenced by get_next_job(), try_dequeue(), and ~lockfree_job_queue().


|
nodiscard |
Checks if the queue is empty.
Definition at line 248 of file lockfree_job_queue.cpp.
References head_, kcenon::thread::detail::lockfree_job_queue::node::next, kcenon::thread::safe_hazard_guard::protect(), and kcenon::thread::retry.

|
nodiscard |
Enqueues a job into the queue (thread-safe)
| job | Unique pointer to the job to enqueue |
Time Complexity: O(1) amortized Memory Ordering: release semantics for visibility
Definition at line 76 of file lockfree_job_queue.cpp.
References kcenon::thread::invalid_argument, kcenon::thread::detail::lockfree_job_queue::node::next, kcenon::thread::safe_hazard_guard::protect(), kcenon::thread::queue_busy, and kcenon::thread::retry.
Referenced by schedule().


|
inlinenodiscardoverridevirtual |
Returns capabilities of lockfree_job_queue.
Capabilities:
Reimplemented from kcenon::thread::queue_capabilities_interface.
Definition at line 197 of file lockfree_job_queue.h.
|
inlineoverridevirtual |
Get next job (delegates to dequeue)
Implements kcenon::thread::scheduler_interface.
Definition at line 173 of file lockfree_job_queue.h.
References dequeue().

|
delete |
|
delete |
|
private |
Retire a node through hazard pointers, recycling via pool on reclamation.
Definition at line 286 of file lockfree_job_queue.cpp.
References kcenon::thread::safe_hazard_pointer_domain::instance(), pool_, and kcenon::thread::safe_hazard_pointer_domain::retire().
Referenced by ~lockfree_job_queue().


|
inlineoverridevirtual |
Schedule a job (delegates to enqueue)
| work | Job to schedule |
Implements kcenon::thread::scheduler_interface.
Definition at line 162 of file lockfree_job_queue.h.
References enqueue().

|
nodiscard |
Gets approximate queue size.
Definition at line 280 of file lockfree_job_queue.cpp.
References approximate_size_.
|
inlinenodiscard |
Tries to dequeue a job without blocking.
Definition at line 126 of file lockfree_job_queue.h.
References dequeue().

|
mutableprivate |
Definition at line 315 of file lockfree_job_queue.h.
Referenced by lockfree_job_queue(), and size().
|
private |
Definition at line 304 of file lockfree_job_queue.h.
Referenced by empty(), lockfree_job_queue(), and ~lockfree_job_queue().
|
private |
Definition at line 308 of file lockfree_job_queue.h.
Referenced by retire_node().
|
private |
Definition at line 318 of file lockfree_job_queue.h.
Referenced by ~lockfree_job_queue().
|
private |
Definition at line 305 of file lockfree_job_queue.h.
Referenced by lockfree_job_queue().