lcls2
C++ API: public headers of xtcdata, psalg, psdaq and psana
Loading...
Searching...
No Matches
SPSCQueue< T > Class Template Reference

Fixed-capacity ring buffer for one producer thread and one consumer thread. More...

#include <spscqueue.hh>

Public Member Functions

 SPSCQueue (int capacity)
 Allocate capacity slots; throws a C string (after printing a message) if capacity is not a power of 2.
 
 SPSCQueue (const SPSCQueue &)=delete
 Copying is disabled.
 
void operator= (const SPSCQueue &)=delete
 Copy assignment is disabled.
 
 SPSCQueue (SPSCQueue &&d) noexcept
 Move constructor: take over the slots, indices, capacity and mask of d; the terminate flag starts cleared.
 
void push (T value)
 Write value at the back and wake a reader if the queue was empty.
 
bool popW (T &value)
 Pop the front element into value, blocking while the queue is empty.
 
bool popW (T &value, unsigned msTmo)
 Like popW(T&), but waits at most msTmo milliseconds; returns false on timeout or if terminating while still empty.
 
bool pop (T &value)
 Pop the front element into value, polling for up to 1 ms and then blocking with popW(T&).
 
bool try_pop (T &value)
 Pop the front element into value without blocking; returns false if the queue is empty.
 
bool peek (T &value) const
 Copy the front element into value without removing it; returns false if the queue is empty.
 
T & front ()
 Return a reference to the slot at the read position (not checked for emptiness).
 
const T & front () const
 Return a const reference to the slot at the read position (not checked for emptiness).
 
T & back ()
 Return a reference to the slot at the write position, the next one push() will fill.
 
const T & back () const
 Return a const reference to the slot at the write position, the next one push() will fill.
 
bool is_empty () const
 Return true if the read and write positions are equal.
 
bool wait_for_nonempty () const
 Block until the queue is not empty; returns false if shutdown() was called while it is still empty, else true.
 
int guess_size () const
 Return the number of queued elements (write minus read position, plus the capacity if negative).
 
size_t size () const
 Return the capacity.
 
void shutdown ()
 Set the terminate flag and wake one blocked reader.
 
void startup ()
 Clear the terminate flag and reset both positions to 0.
 

Detailed Description

template<typename T>
class SPSCQueue< T >

Fixed-capacity ring buffer for one producer thread and one consumer thread.

push() never checks for a full queue; the read functions can block on a condition variable, and shutdown() releases blocked readers.

Constructor & Destructor Documentation

◆ SPSCQueue() [1/3]

template<typename T >
SPSCQueue< T >::SPSCQueue ( int  capacity)
inline

Allocate capacity slots; throws a C string (after printing a message) if capacity is not a power of 2.

◆ SPSCQueue() [2/3]

template<typename T >
SPSCQueue< T >::SPSCQueue ( const SPSCQueue< T > &  )
delete

Copying is disabled.

◆ SPSCQueue() [3/3]

template<typename T >
SPSCQueue< T >::SPSCQueue ( SPSCQueue< T > &&  d)
inlinenoexcept

Move constructor: take over the slots, indices, capacity and mask of d; the terminate flag starts cleared.

Member Function Documentation

◆ back() [1/2]

template<typename T >
T & SPSCQueue< T >::back ( )
inline

Return a reference to the slot at the write position, the next one push() will fill.

◆ back() [2/2]

template<typename T >
const T & SPSCQueue< T >::back ( ) const
inline

Return a const reference to the slot at the write position, the next one push() will fill.

◆ front() [1/2]

template<typename T >
T & SPSCQueue< T >::front ( )
inline

Return a reference to the slot at the read position (not checked for emptiness).

◆ front() [2/2]

template<typename T >
const T & SPSCQueue< T >::front ( ) const
inline

Return a const reference to the slot at the read position (not checked for emptiness).

◆ guess_size()

template<typename T >
int SPSCQueue< T >::guess_size ( ) const
inline

Return the number of queued elements (write minus read position, plus the capacity if negative).

◆ is_empty()

template<typename T >
bool SPSCQueue< T >::is_empty ( ) const
inline

Return true if the read and write positions are equal.

◆ operator=()

template<typename T >
void SPSCQueue< T >::operator= ( const SPSCQueue< T > &  )
delete

Copy assignment is disabled.

◆ peek()

template<typename T >
bool SPSCQueue< T >::peek ( T &  value) const
inline

Copy the front element into value without removing it; returns false if the queue is empty.

◆ pop()

template<typename T >
bool SPSCQueue< T >::pop ( T &  value)
inline

Pop the front element into value, polling for up to 1 ms and then blocking with popW(T&).

Returns false only if popW() does.

◆ popW() [1/2]

template<typename T >
bool SPSCQueue< T >::popW ( T &  value)
inline

Pop the front element into value, blocking while the queue is empty.

Returns false, without popping, if the queue was empty on entry and shutdown() has been called (even if an element arrived meanwhile); otherwise true.

◆ popW() [2/2]

template<typename T >
bool SPSCQueue< T >::popW ( T &  value,
unsigned  msTmo 
)
inline

Like popW(T&), but waits at most msTmo milliseconds; returns false on timeout or if terminating while still empty.

◆ push()

template<typename T >
void SPSCQueue< T >::push ( T  value)
inline

Write value at the back and wake a reader if the queue was empty.

Does not check whether the queue is full (per the code comment).

◆ shutdown()

template<typename T >
void SPSCQueue< T >::shutdown ( )
inline

Set the terminate flag and wake one blocked reader.

◆ size()

template<typename T >
size_t SPSCQueue< T >::size ( ) const
inline

Return the capacity.

◆ startup()

template<typename T >
void SPSCQueue< T >::startup ( )
inline

Clear the terminate flag and reset both positions to 0.

◆ try_pop()

template<typename T >
bool SPSCQueue< T >::try_pop ( T &  value)
inline

Pop the front element into value without blocking; returns false if the queue is empty.

◆ wait_for_nonempty()

template<typename T >
bool SPSCQueue< T >::wait_for_nonempty ( ) const
inline

Block until the queue is not empty; returns false if shutdown() was called while it is still empty, else true.


The documentation for this class was generated from the following file: