|
ADS
Algorithms and Data Structures
|
a bounded queue from one producer to one consumer, lock-free More...
#include <ADS/concurrent/SPSCQueue.h>
Public types | |
| using | Item = T |
| the type of the items | |
Public member functions | |
| SPSCQueue (std::size_t capacity) | |
| the empty queue of capacity \( c \) | |
| SPSCQueue (const SPSCQueue &)=delete | |
| no copy, since the two threads share it by its address | |
| SPSCQueue & | operator= (const SPSCQueue &)=delete |
| no copy, since the two threads share it by its address | |
| ~SPSCQueue () | |
| destroys the items left | |
| bool | push (const T &v) |
| enqueues a copy of \( v \), false if the queue is full; only the producer calls it | |
| bool | push (T &&v) |
| enqueues \( v \), moved, false if the queue is full; only the producer calls it | |
| template<class... Args> | |
| bool | emplace (Args &&... args) |
| enqueues the item built from args, false if the queue is full (and then nothing is built); only the producer calls it | |
| std::optional< T > | pop (void) |
| dequeues an item, nothing if the queue is empty; only the consumer calls it | |
| bool | tryPop (T &x) |
| dequeues an item into \( x \), false if the queue is empty; only the consumer calls it | |
| template<class It> | |
| std::size_t | pushMany (It first, std::size_t n) |
| enqueues the first of the \( n \) items from first that fit, and returns how many, publishing them all by one store of the tail; only the producer calls it | |
| template<class F> | |
| std::size_t | consume (F &&f, std::size_t n=std::size_t(-1)) |
| the calls f( x ) on the items \( x \) in the queue, at most \( n \), each dequeued after its call, which may move it, with no wait for a batch; how many, all freed by one store of the head; only the consumer calls it | |
| void | notify (void) noexcept |
| wakes the consumer if it sleeps in waitPop(), after pushes: a fence and the read of a line when it does not; only the producer calls it | |
| void | waitPop (T &x) |
| dequeues an item into \( x \), sleeping until one comes [see EventCount.h], the producer calling notify() after its pushes; only the consumer calls it | |
| std::size_t | size (void) const |
| the number of items at some moment of the call, \( t - h \) | |
| std::size_t | capacity (void) const |
| the capacity \( c \) | |
a bounded queue from one producer to one consumer, lock-free
| using ADS::SPSCQueue< T >::Item = T |
the type of the items
|
explicit |
the empty queue of capacity \( c \)
|
delete |
no copy, since the two threads share it by its address
| ADS::SPSCQueue< T >::~SPSCQueue | ( | ) |
destroys the items left
|
inlinenodiscard |
the capacity \( c \)
| std::size_t ADS::SPSCQueue< T >::consume | ( | F && | f, |
| std::size_t | n = std::size_t(-1) ) |
the calls f( x ) on the items \( x \) in the queue, at most \( n \), each dequeued after its call, which may move it, with no wait for a batch; how many, all freed by one store of the head; only the consumer calls it
In pseudocode (the queue of Lamport for a batch, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{consume}( f , n ) \cr 2 & \quad h' \gets h;\ \textbf{if}\ h' = \mathit{seen}\ \textbf{then} \ \mathit{seen} \gets t\quad \triangleright\ \text{acquire} \cr 3 & \quad k \gets \min\{ n , \mathit{seen} - h' \} \cr 4 & \quad \textbf{for}\ j \gets 0\ \textbf{to}\ k - 1\ \textbf{do} \ f( A[ ( h' + j ) \bmod c ] ) \cr 5 & \quad h \gets h' + k\quad \triangleright\ \text{release}; \ \textbf{return}\ k \end{array}} \]
If a call throws, the items before it and its own are dequeued.
enqueues the item built from args, false if the queue is full (and then nothing is built); only the producer calls it
In pseudocode (the queue of Lamport, beyond the book), \( t \) and \( h \) the tail and the head, which only grow, \( c \) the capacity and \( \mathit{seen} \) the copy of the head that the producer keeps:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{emplace}( x ) \cr 2 & \quad t' \gets t\quad \triangleright\ \text{relaxed, only this} \text{ thread writes it} \cr 3 & \quad \textbf{if}\ t' - \mathit{seen} = c\ \textbf{then} \cr 4 & \qquad \mathit{seen} \gets h\quad \triangleright\ \text{acquire:} \text{ the places freed are seen free} \cr 5 & \qquad \textbf{if}\ t' - \mathit{seen} = c\ \textbf{then} \ \textbf{return false} \cr 6 & \quad A[ t' \bmod c ] \gets x \cr 7 & \quad t \gets t' + 1\quad \triangleright\ \text{release: the item} \text{ is written before the tail} \cr 8 & \quad \textbf{return true} \end{array}} \]
|
inlinenoexcept |
wakes the consumer if it sleeps in waitPop(), after pushes: a fence and the read of a line when it does not; only the producer calls it
|
delete |
no copy, since the two threads share it by its address
| std::optional< T > ADS::SPSCQueue< T >::pop | ( | void | ) |
dequeues an item, nothing if the queue is empty; only the consumer calls it
It is tryPop() below, with the item returned.
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pop}() \cr 2 & \quad \textbf{if}\ \mathrm{tryPop}( x )\ \textbf{then return}\ x \ \textbf{else return nil} \end{array}} \]
|
inline |
enqueues a copy of \( v \), false if the queue is full; only the producer calls it
|
inline |
enqueues \( v \), moved, false if the queue is full; only the producer calls it
| std::size_t ADS::SPSCQueue< T >::pushMany | ( | It | first, |
| std::size_t | n ) |
enqueues the first of the \( n \) items from first that fit, and returns how many, publishing them all by one store of the tail; only the producer calls it
In pseudocode (the queue of Lamport for a batch, beyond the book), as emplace():
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pushMany}( x_0 , \ldots , x_{n - 1} ) \cr 2 & \quad t' \gets t;\ \textbf{if}\ t' - \mathit{seen} < n \ \textbf{then}\ \mathit{seen} \gets h\quad \triangleright \ \text{acquire} \cr 3 & \quad k \gets \min\{ n , c - ( t' - \mathit{seen} ) \} \cr 4 & \quad \textbf{for}\ j \gets 0\ \textbf{to}\ k - 1\ \textbf{do} \ A[ ( t' + j ) \bmod c ] \gets x_j \cr 5 & \quad t \gets t' + k\quad \triangleright\ \text{release}; \ \textbf{return}\ k \end{array}} \]
|
nodiscard |
the number of items at some moment of the call, \( t - h \)
| bool ADS::SPSCQueue< T >::tryPop | ( | T & | x | ) |
dequeues an item into \( x \), false if the queue is empty; only the consumer calls it
In pseudocode (the queue of Lamport with the copies of Lee et al. and the wait for a batch of B-Queue, beyond the book), \( \mathit{seen} \) the copy of the tail that the consumer keeps and \( \mathit{streaming} \) true if its last pop took an item:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{tryPop}( x ) \cr 2 & \quad h' \gets h\quad \triangleright\ \text{relaxed, only this} \text{ thread writes it} \cr 3 & \quad \textbf{if}\ h' = \mathit{seen}\ \textbf{then} \cr 4 & \qquad \mathrm{prefetch}( A[ h' \bmod c ] );\ \mathit{seen} \gets t\quad \triangleright\ \text{acquire} \cr 5 & \qquad \textbf{if}\ \mathit{streaming}\ \textbf{and}\ 0 < \mathit{seen} - h' < \min( 64 , c / 2 )\ \textbf{then} \cr 6 & \qquad\quad \text{wait 64 pauses};\ \mathit{seen} \gets t \cr 7 & \qquad \textbf{if}\ h' = \mathit{seen}\ \textbf{then} \ \mathit{streaming} \gets \textbf{false};\ \textbf{return false} \cr 8 & \quad x \gets A[ h' \bmod c ] \cr 9 & \quad h \gets h' + 1\quad \triangleright\ \text{release: the place} \text{ is read before it is freed} \cr 10 & \quad \mathit{streaming} \gets \textbf{true}; \ \textbf{return true} \end{array}} \]
|
inline |
dequeues an item into \( x \), sleeping until one comes [see EventCount.h], the producer calling notify() after its pushes; only the consumer calls it
In pseudocode (beyond the book), on the wait of EventCount:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{waitPop}( x ) \cr 2 & \quad \mathrm{await}( \mathrm{tryPop}( x ) ) \end{array}} \]