ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SPSCQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File SPSCQueue.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
58/*--------------------------------------------------------------------------*/
59/*----------------------------- DEFINITIONS --------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62#ifndef __ADS_SPSCQueue
63 #define __ADS_SPSCQueue
64/* self-identification: #endif constant */
65
66/*--------------------------------------------------------------------------*/
67/*------------------------------ INCLUDES ----------------------------------*/
68/*--------------------------------------------------------------------------*/
69
70#include "ADS/Contract.h"
72#include "ADS/memory/Prefetch.h"
73
74#include <algorithm>
75#include <atomic>
76#include <bit>
77#include <cstddef>
78#include <memory>
79#include <new>
80#include <optional>
81#include <utility>
82
83/*--------------------------------------------------------------------------*/
84/*--------------------------- NAMESPACE ------------------------------------*/
85/*--------------------------------------------------------------------------*/
86
87namespace ADS
88{
89
90/*--------------------------------------------------------------------------*/
91/*---------------------------- CLASS SPSCQueue -----------------------------*/
92/*--------------------------------------------------------------------------*/
93
95template< class T >
97{
98
99/*--------------------------------------------------------------------------*/
100 public:
101 using Item = T;
102
105 explicit SPSCQueue( std::size_t capacity );
106
108 SPSCQueue( const SPSCQueue & ) = delete;
110 SPSCQueue & operator=( const SPSCQueue & ) = delete;
111
114
117 bool push( const T & v ) { return( emplace( v ) ); }
118
121 bool push( T && v ) { return( emplace( std::move( v ) ) ); }
122
146 template< class... Args >
147 bool emplace( Args &&... args );
148
160 std::optional< T > pop( void );
161
189 bool tryPop( T & x );
190
211 template< class It >
212 std::size_t pushMany( It first , std::size_t n );
213
233 template< class F >
234 std::size_t consume( F && f , std::size_t n = std::size_t( -1 ) );
235
238 void notify( void ) noexcept { f_wait.notify(); }
239
251 void waitPop( T & x ) {
252 f_wait.await( [ & ] { return( tryPop( x ) ); } );
253 }
254
256 [[nodiscard]] std::size_t size( void ) const;
257
259 [[nodiscard]] std::size_t capacity( void ) const { return( f_mask + 1 ); }
260
261 private:
262 static constexpr std::size_t line = 64;
263 static constexpr std::size_t pad = ( line + sizeof( T ) - 1 ) / sizeof( T );
264
266 [[nodiscard]] T * at( std::size_t i ) const {
267 return( f_A + pad + ( i & f_mask ) );
268 }
269
275 void refresh( std::size_t h );
276
278 [[nodiscard]] std::size_t batch( void ) const {
279 return( std::min< std::size_t >( 64 , capacity() / 2 ) );
280 }
281
283 static void relax( void );
284
288 static void prefetch( const T * p ) { ADS::detail::prefetchLine( p ); }
289
290 // the queue begins on a pair of lines, which the processor may fetch
291 // together (the adjacent line prefetcher of x86), so that each pair
292 // holds the line of the places and the tail, the copy of the head and
293 // the head, the copy of the tail and nothing: the copy that a thread
294 // rewrites while it spins shares no pair with the index of the other
295 alignas( 2 * line ) T * f_A;
296 std::size_t f_mask;
297 alignas( line ) std::atomic< std::size_t > f_tail{ 0 };
298 alignas( line ) std::size_t f_headSeen = 0;
299 alignas( line ) std::atomic< std::size_t > f_head{ 0 };
300 alignas( line ) std::size_t f_tailSeen = 0;
301 bool f_streaming = false;
302 alignas( line ) char f_end = 0;
303 EventCount f_wait;
304 }; // end( class SPSCQueue )
305
306/*--------------------------------------------------------------------------*/
307
308} // end( namespace ADS )
309
310/*--------------------------------------------------------------------------*/
311/*-------------------------- IMPLEMENTATION --------------------------------*/
312/*--------------------------------------------------------------------------*/
313
314#include "ADS/concurrent/impl/SPSCQueue.tpp"
315
316/*--------------------------------------------------------------------------*/
317
318#endif /* SPSCQueue.h included */
319
320/*--------------------------------------------------------------------------*/
321/*-------------------------- End File SPSCQueue.h --------------------------*/
322/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
An event count, beyond the book: the way for a thread to sleep until a condition on a lock-free struc...
The prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
SPSCQueue(const SPSCQueue &)=delete
no copy, since the two threads share it by its address
void notify(void) noexcept
wakes the consumer if it sleeps in waitPop(), after pushes: a fence and the read of a line when it do...
Definition SPSCQueue.h:238
bool tryPop(T &x)
dequeues an item into , false if the queue is empty; only the consumer calls it
bool emplace(Args &&... args)
enqueues the item built from args, false if the queue is full (and then nothing is built); only the p...
~SPSCQueue()
destroys the items left
std::size_t size(void) const
the number of items at some moment of the call,
SPSCQueue(std::size_t capacity)
the empty queue of capacity
SPSCQueue & operator=(const SPSCQueue &)=delete
no copy, since the two threads share it by its address
std::size_t consume(F &&f, std::size_t n=std::size_t(-1))
the calls f( x ) on the items in the queue, at most , each dequeued after its call,...
bool push(T &&v)
enqueues , moved, false if the queue is full; only the producer calls it
Definition SPSCQueue.h:121
T Item
the type of the items
Definition SPSCQueue.h:101
void waitPop(T &x)
dequeues an item into , sleeping until one comes [see EventCount.h], the producer calling notify() af...
Definition SPSCQueue.h:251
std::optional< T > pop(void)
dequeues an item, nothing if the queue is empty; only the consumer calls it
std::size_t capacity(void) const
the capacity
Definition SPSCQueue.h:259
bool push(const T &v)
enqueues a copy of , false if the queue is full; only the producer calls it
Definition SPSCQueue.h:117
std::size_t pushMany(It first, std::size_t n)
enqueues the first of the items from first that fit, and returns how many, publishing them all by on...
constexpr detail::Prefetch prefetch
a hint that is looked up next: s.prefetch( k ), else prefetch( s , k ) found by ADL,...
Definition Capabilities.h:451
Definition ADT.h:96