67#ifndef __ADS_MPSCQueue
68 #define __ADS_MPSCQueue
97template<
class T , std::
size_t B = 1024 >
100 static_assert( ( B >= 2 ) && ( ( B & ( B - 1 ) ) == 0 ) ,
101 "a block has 2^k places" );
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;
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 );
125 [[nodiscard]]
void * slot( std::size_t i ) {
126 return( raw + i *
sizeof( T ) );
134 alignas( 2 * line ) Block * in;
136 Block * spare =
nullptr;
137 alignas( 2 * line ) Block * out;
139 bool streaming =
false;
141 alignas( 2 * line ) std::atomic< Block * > given{
nullptr };
142 Lane * nextLane =
nullptr;
145 explicit Lane(
EventCount * w ) : in(
new Block ) , out( in ) , wait( w ) { }
161 : f_lane( std::exchange( o.f_lane ,
nullptr ) ) { }
165 f_lane = std::exchange( o.f_lane ,
nullptr );
191 template<
class... Args >
205 f_lane->wait->notify();
210 explicit Producer( Lane * l ) : f_lane( l ) { }
211 Lane * f_lane =
nullptr;
286 std::size_t
popInto( T * out , std::size_t n );
300 f_wait.await( [ & ] {
return(
tryPop( x ) ); } );
308 f_wait.await( [ & ] {
return( ( k =
popInto( out , n ) ) > 0 ); } );
319 std::size_t drain( Lane * l , T * out , std::size_t n );
322 static void giveBack( Lane * l , Block * b );
325 static void relax(
void );
327 std::atomic< Lane * > f_lanes{
nullptr };
328 Lane * f_at =
nullptr;
340#include "ADS/concurrent/impl/MPSCQueue.tpp"
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