ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MPSCQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File MPSCQueue.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
63/*--------------------------------------------------------------------------*/
64/*----------------------------- DEFINITIONS --------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#ifndef __ADS_MPSCQueue
68 #define __ADS_MPSCQueue
69/* self-identification: #endif constant */
70
71/*--------------------------------------------------------------------------*/
72/*------------------------------ INCLUDES ----------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75#include "ADS/Contract.h"
77
78#include <algorithm>
79#include <atomic>
80#include <cstddef>
81#include <memory>
82#include <new>
83#include <utility>
84
85/*--------------------------------------------------------------------------*/
86/*--------------------------- NAMESPACE ------------------------------------*/
87/*--------------------------------------------------------------------------*/
88
89namespace ADS
90{
91
92/*--------------------------------------------------------------------------*/
93/*---------------------------- CLASS MPSCQueue -----------------------------*/
94/*--------------------------------------------------------------------------*/
95
97template< class T , std::size_t B = 1024 >
99{
100 static_assert( ( B >= 2 ) && ( ( B & ( B - 1 ) ) == 0 ) ,
101 "a block has 2^k places" );
102
103 static constexpr std::size_t line = 64;
106 static constexpr std::size_t want = B < 64 ? B : 64;
107 static constexpr int pauses = 64;
108
111 struct Block
112 {
114 alignas( 2 * line ) std::atomic< std::size_t > filled{ 0 };
116 std::atomic< Block * > next{ nullptr };
117 Block * spare = nullptr;
119 alignas( 2 * line ) unsigned char raw[ B * sizeof( T ) ];
121 [[nodiscard]] T * at( std::size_t i ) {
122 return( std::launder( reinterpret_cast< T * >( raw ) ) + i );
123 }
125 [[nodiscard]] void * slot( std::size_t i ) {
126 return( raw + i * sizeof( T ) );
127 }
128 };
129
132 struct Lane
133 {
134 alignas( 2 * line ) Block * in;
135 std::size_t pos = 0;
136 Block * spare = nullptr;
137 alignas( 2 * line ) Block * out;
138 std::size_t at = 0;
139 bool streaming = false;
141 alignas( 2 * line ) std::atomic< Block * > given{ nullptr };
142 Lane * nextLane = nullptr;
143 EventCount * wait;
145 explicit Lane( EventCount * w ) : in( new Block ) , out( in ) , wait( w ) { }
146 };
147
148/*--------------------------------------------------------------------------*/
149 public:
150 using Item = T;
151
154 {
155 public:
157 Producer( void ) = default;
158
160 Producer( Producer && o ) noexcept
161 : f_lane( std::exchange( o.f_lane , nullptr ) ) { }
162
164 Producer & operator=( Producer && o ) noexcept {
165 f_lane = std::exchange( o.f_lane , nullptr );
166 return( *this );
167 }
168
191 template< class... Args >
192 void emplace( Args &&... args );
193
195 void push( const T & v ) { emplace( v ); }
196
198 void push( T && v ) { emplace( std::move( v ) ); }
199
203 void notify( void ) {
204 ADS_PRE( f_lane != nullptr );
205 f_lane->wait->notify();
206 }
207
208 private:
209 friend class MPSCQueue;
210 explicit Producer( Lane * l ) : f_lane( l ) { }
211 Lane * f_lane = nullptr;
212 }; // end( class Producer )
213
215 MPSCQueue( void ) = default;
217 MPSCQueue( const MPSCQueue & ) = delete;
219 MPSCQueue & operator=( const MPSCQueue & ) = delete;
220
223
238 [[nodiscard]] Producer producer( void );
239
250 bool tryPop( T & x );
251
286 std::size_t popInto( T * out , std::size_t n );
287
299 void waitPop( T & x ) {
300 f_wait.await( [ & ] { return( tryPop( x ) ); } );
301 }
302
305 std::size_t waitPopInto( T * out , std::size_t n ) {
306 ADS_PRE( n >= 1 );
307 std::size_t k = 0;
308 f_wait.await( [ & ] { return( ( k = popInto( out , n ) ) > 0 ); } );
309 return( k );
310 }
311
314 [[nodiscard]] bool empty( void );
315
316/*--------------------------------------------------------------------------*/
317 private:
319 std::size_t drain( Lane * l , T * out , std::size_t n );
320
322 static void giveBack( Lane * l , Block * b );
323
325 static void relax( void );
326
327 std::atomic< Lane * > f_lanes{ nullptr };
328 Lane * f_at = nullptr;
329 EventCount f_wait;
330 }; // end( class MPSCQueue )
331
332/*--------------------------------------------------------------------------*/
333
334} // end( namespace ADS )
335
336/*--------------------------------------------------------------------------*/
337/*-------------------------- IMPLEMENTATION --------------------------------*/
338/*--------------------------------------------------------------------------*/
339
340#include "ADS/concurrent/impl/MPSCQueue.tpp"
341
342/*--------------------------------------------------------------------------*/
343
344#endif /* MPSCQueue.h included */
345
346/*--------------------------------------------------------------------------*/
347/*-------------------------- End File MPSCQueue.h --------------------------*/
348/*--------------------------------------------------------------------------*/
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 sleep of threads until a condition holds, woken by notify()
Definition EventCount.h:112
the handle of a producer, its lane: one thread at a time pushes
Definition MPSCQueue.h:154
friend class MPSCQueue
Definition MPSCQueue.h:209
void emplace(Args &&... args)
enqueues the item built from args in the lane of the handle
void push(T &&v)
enqueues , moved, as emplace()
Definition MPSCQueue.h:198
void notify(void)
wakes the consumer if it sleeps in waitPop() or waitPopInto(), after pushes: a fence and the read of ...
Definition MPSCQueue.h:203
Producer(void)=default
a handle of no lane, to be assigned one from producer()
Producer & operator=(Producer &&o) noexcept
the lane of , which is left with none
Definition MPSCQueue.h:164
void push(const T &v)
enqueues a copy of , as emplace()
Definition MPSCQueue.h:195
Producer(Producer &&o) noexcept
the lane of , which is left with none
Definition MPSCQueue.h:160
~MPSCQueue()
destroys the items left, the blocks and the lanes
bool empty(void)
true if no item was found in a turn of the lanes; only the consumer calls it
std::size_t waitPopInto(T *out, std::size_t n)
the items of popInto(), sleeping until at least one comes
Definition MPSCQueue.h:305
MPSCQueue & operator=(const MPSCQueue &)=delete
no copy, since the producers hold its lanes
void waitPop(T &x)
dequeues an item into , sleeping until one comes [see EventCount.h], the producers calling notify() a...
Definition MPSCQueue.h:299
T Item
the type of the items
Definition MPSCQueue.h:150
bool tryPop(T &x)
dequeues an item into , false if none was found in a turn of the lanes; only the consumer calls it
MPSCQueue(const MPSCQueue &)=delete
no copy, since the producers hold its lanes
Producer producer(void)
a new producer, with a lane of its own; any thread may call it
MPSCQueue(void)=default
the queue with no lane
std::size_t popInto(T *out, std::size_t n)
dequeues up to items into out, those of a lane before the next one, and returns how many; only the c...
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96