|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Bounded single-producer/single-consumer queue. More...
#include <concurrency_utils.H>
Public Member Functions | |
| SpscQueue (size_t capacity) | |
| Construct a queue with the specified capacity. | |
| SpscQueue (const SpscQueue &)=delete | |
| Deleted copy constructor. | |
| SpscQueue & | operator= (const SpscQueue &)=delete |
| Deleted copy assignment operator. | |
| size_t | capacity () const noexcept |
| Return the queue's capacity. | |
| bool | empty () const noexcept |
| Check if the queue is empty. | |
| bool | full () const noexcept |
| Check if the queue is full. | |
| size_t | size () const noexcept |
| Return the number of items currently in the queue. | |
| bool | try_push (const T &value) |
| Attempt to push an item (copy) into the queue. | |
| bool | try_push (T &&value) |
| Attempt to push an item (move) into the queue. | |
| template<typename... Args> | |
| bool | emplace (Args &&... args) |
| Construct an item in-place at the tail of the queue. | |
| bool | try_pop (T &out) |
Attempt to pop an item from the queue into out. | |
| std::optional< T > | try_pop () |
| Attempt to pop an item from the queue. | |
Private Member Functions | |
| size_t | advance (size_t idx) const noexcept |
| Compute the next index in the circular buffer. | |
| template<typename... Args> | |
| bool | emplace_impl (Args &&... args) |
| Internal implementation for push/emplace. | |
Private Attributes | |
| std::vector< std::optional< T > > | slots_ |
| size_t | storage_size_ |
| std::atomic< size_t > | head_ {0} |
| std::atomic< size_t > | tail_ {0} |
Bounded single-producer/single-consumer queue.
This queue is a lock-free, bounded circular buffer designed for high- performance handoff between exactly one producer thread and one consumer thread. It is thread-safe only for that single-producer / single-consumer usage pattern. Concurrent calls from multiple producers or multiple consumers are not supported.
Operations are non-blocking and never close. Producers observe failure by receiving false when the ring is full; consumers observe emptiness by receiving false or std::nullopt. If element extraction throws, the slot remains occupied until extraction succeeds.
| T | The type of elements stored in the queue. |
Definition at line 1101 of file concurrency_utils.H.
|
inlineexplicit |
Construct a queue with the specified capacity.
| capacity | Maximum number of items the queue can hold. |
| std::invalid_argument | if capacity is zero. |
Definition at line 1134 of file concurrency_utils.H.
References Aleph::SpscQueue< T >::capacity().
Deleted copy constructor.
|
inlineprivatenoexcept |
Compute the next index in the circular buffer.
Definition at line 1109 of file concurrency_utils.H.
References Aleph::SpscQueue< T >::storage_size_.
Referenced by Aleph::SpscQueue< T >::emplace_impl(), Aleph::SpscQueue< T >::full(), Aleph::SpscQueue< T >::try_pop(), and Aleph::SpscQueue< T >::try_pop().
|
inlinenoexcept |
Return the queue's capacity.
| Nothing. |
noexcept. Definition at line 1152 of file concurrency_utils.H.
References Aleph::SpscQueue< T >::storage_size_.
Referenced by Aleph::SpscQueue< T >::SpscQueue().
|
inline |
Construct an item in-place at the tail of the queue.
| Args | Constructor argument types. |
| args | Constructor arguments. |
true if the item was enqueued, false if the queue is full. | Any | exception thrown while constructing T in-place. |
Definition at line 1217 of file concurrency_utils.H.
References Aleph::divide_and_conquer_partition_dp(), and Aleph::SpscQueue< T >::emplace_impl().
|
inlineprivate |
Internal implementation for push/emplace.
Definition at line 1116 of file concurrency_utils.H.
References Aleph::SpscQueue< T >::advance(), Aleph::divide_and_conquer_partition_dp(), Aleph::SpscQueue< T >::head_, Aleph::next(), Aleph::SpscQueue< T >::slots_, and Aleph::SpscQueue< T >::tail_.
Referenced by Aleph::SpscQueue< T >::emplace(), Aleph::SpscQueue< T >::try_push(), and Aleph::SpscQueue< T >::try_push().
|
inlinenoexcept |
Check if the queue is empty.
true if the queue currently contains no items. | Nothing. |
Definition at line 1159 of file concurrency_utils.H.
References Aleph::SpscQueue< T >::head_, and Aleph::SpscQueue< T >::tail_.
Referenced by TEST().
|
inlinenoexcept |
Check if the queue is full.
true if the queue currently has no free slots. | Nothing. |
Definition at line 1170 of file concurrency_utils.H.
References Aleph::SpscQueue< T >::advance(), Aleph::SpscQueue< T >::head_, and Aleph::SpscQueue< T >::tail_.
Referenced by TEST().
|
delete |
Deleted copy assignment operator.
|
inlinenoexcept |
Return the number of items currently in the queue.
| Nothing. |
Definition at line 1181 of file concurrency_utils.H.
References Aleph::SpscQueue< T >::head_, Aleph::SpscQueue< T >::storage_size_, and Aleph::SpscQueue< T >::tail_.
|
inline |
Attempt to pop an item from the queue.
std::optional, or std::nullopt if the queue is empty. | Any | exception thrown while copying or moving the queued value into the returned optional. |
Definition at line 1251 of file concurrency_utils.H.
References Aleph::SpscQueue< T >::advance(), Aleph::divide_and_conquer_partition_dp(), Aleph::SpscQueue< T >::head_, Aleph::SpscQueue< T >::slots_, and Aleph::SpscQueue< T >::tail_.
Attempt to pop an item from the queue into out.
| out | Reference where the popped item will be moved. |
true if an item was extracted, false if the queue is empty. | Any | exception thrown while copying or moving the queued value into a temporary or assigning into out. |
Definition at line 1230 of file concurrency_utils.H.
References Aleph::SpscQueue< T >::advance(), Aleph::divide_and_conquer_partition_dp(), Aleph::SpscQueue< T >::head_, Aleph::SpscQueue< T >::slots_, and Aleph::SpscQueue< T >::tail_.
Attempt to push an item (copy) into the queue.
| value | The item to push. |
true if the item was enqueued, false if the queue is full. | Any | exception thrown while copying T into the ring buffer. |
Definition at line 1197 of file concurrency_utils.H.
References Aleph::SpscQueue< T >::emplace_impl().
Attempt to push an item (move) into the queue.
| value | The item to push. |
true if the item was enqueued, false if the queue is full. | Any | exception thrown while moving T into the ring buffer. |
Definition at line 1206 of file concurrency_utils.H.
References Aleph::SpscQueue< T >::emplace_impl().
|
private |
Definition at line 1105 of file concurrency_utils.H.
Referenced by Aleph::SpscQueue< T >::emplace_impl(), Aleph::SpscQueue< T >::empty(), Aleph::SpscQueue< T >::full(), Aleph::SpscQueue< T >::size(), Aleph::SpscQueue< T >::try_pop(), and Aleph::SpscQueue< T >::try_pop().
|
private |
Definition at line 1103 of file concurrency_utils.H.
Referenced by Aleph::SpscQueue< T >::emplace_impl(), Aleph::SpscQueue< T >::try_pop(), and Aleph::SpscQueue< T >::try_pop().
|
private |
Definition at line 1104 of file concurrency_utils.H.
Referenced by Aleph::SpscQueue< T >::advance(), Aleph::SpscQueue< T >::capacity(), and Aleph::SpscQueue< T >::size().
|
private |
Definition at line 1106 of file concurrency_utils.H.
Referenced by Aleph::SpscQueue< T >::emplace_impl(), Aleph::SpscQueue< T >::empty(), Aleph::SpscQueue< T >::full(), Aleph::SpscQueue< T >::size(), Aleph::SpscQueue< T >::try_pop(), and Aleph::SpscQueue< T >::try_pop().