62#ifndef __ADS_SPSCQueue
63 #define __ADS_SPSCQueue
146 template<
class... Args >
160 std::optional< T >
pop(
void );
234 std::size_t
consume( F && f , std::size_t n = std::size_t( -1 ) );
238 void notify(
void )
noexcept { f_wait.notify(); }
252 f_wait.await( [ & ] {
return(
tryPop( x ) ); } );
256 [[nodiscard]] std::size_t
size(
void )
const;
259 [[nodiscard]] std::size_t
capacity(
void )
const {
return( f_mask + 1 ); }
262 static constexpr std::size_t line = 64;
263 static constexpr std::size_t pad = ( line +
sizeof( T ) - 1 ) /
sizeof( T );
266 [[nodiscard]] T * at( std::size_t i )
const {
267 return( f_A + pad + ( i & f_mask ) );
275 void refresh( std::size_t h );
278 [[nodiscard]] std::size_t batch(
void )
const {
279 return( std::min< std::size_t >( 64 ,
capacity() / 2 ) );
283 static void relax(
void );
288 static void prefetch(
const T * p ) { ADS::detail::prefetchLine( p ); }
295 alignas( 2 * line ) T * f_A;
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;
314#include "ADS/concurrent/impl/SPSCQueue.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 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