Soldered 125kHz RFID board Arduino library 1.0.0
Library for Soldered 125kHz RFID board.
Loading...
Searching...
No Matches
circular_queue_mp< T, ForEachArg > Class Template Reference

Instance class for a multi-producer, single-consumer circular queue / ring buffer (FIFO). This implementation is lock-free between producers and consumer for the available(), peek(), pop(), and push() type functions, but is guarded to safely allow only a single producer at any instant. More...

#include <circular_queue_mp.h>

Inherits circular_queue< T, void >.

Public Member Functions

 circular_queue_mp ()=default
 
 circular_queue_mp (const size_t capacity)
 
 circular_queue_mp (circular_queue< T, ForEachArg > &&cq)
 
bool capacity (const size_t cap)
 Resize the queue. The available elements in the queue are preserved. This is not lock-free, but safe, concurrent producer or consumer access is guarded.
 
bool IRAM_ATTR push ()=delete
 
bool IRAM_ATTR push (T &&val)
 Move the rvalue parameter into the queue, guarded for multiple concurrent producers.
 
bool IRAM_ATTR push (const T &val)
 Push a copy of the parameter into the queue, guarded for multiple concurrent producers.
 
size_t push_n (const T *buffer, size_t size)
 Push copies of multiple elements from a buffer into the queue, in order, beginning at buffer's head. This is guarded for multiple producers, push_n() is atomic.
 
T & pop_requeue ()
 Pops the next available element from the queue, requeues it immediately.
 
bool for_each_requeue (const Delegate< bool(T &), ForEachArg > &fun)
 Iterate over, pop and optionally requeue each available element from the queue, calling back fun with a reference of every single element. Requeuing is dependent on the return boolean of the callback function. If it returns true, the requeue occurs.
 

Protected Attributes

std::mutex m_pushMtx
 
- Protected Attributes inherited from circular_queue< T, void >
const T defaultValue
 
size_t m_bufSize
 
std::unique_ptr< T[]> m_buffer
 
std::unique_ptr< T > m_buffer
 
std::atomic< size_tm_inPos
 
std::atomic< size_tm_outPos
 

Additional Inherited Members

- Protected Member Functions inherited from circular_queue< T, void >
 circular_queue ()
 Constructs a valid, but zero-capacity dummy queue.
 
 circular_queue (const size_t capacity)
 Constructs a queue of the given maximum capacity.
 
 circular_queue (circular_queue &&cq)
 
 circular_queue (const circular_queue &)=delete
 
 ~circular_queue ()
 
circular_queueoperator= (circular_queue &&cq)
 
circular_queueoperator= (const circular_queue &)=delete
 
size_t capacity () const
 Get the numer of elements the queue can hold at most.
 
bool capacity (const size_t cap)
 Resize the queue. The available elements in the queue are preserved. This is not lock-free and concurrent producer or consumer access will lead to corruption.
 
void flush ()
 Discard all data in the queue.
 
size_t available () const
 Get a snapshot number of elements that can be retrieved by pop.
 
size_t available_for_push () const
 Get the remaining free elementes for pushing.
 
peek () const
 Peek at the next element pop will return without removing it from the queue.
 
T &IRAM_ATTR pushpeek () __attribute__((always_inline))
 Peek at the next pending input value.
 
bool IRAM_ATTR push () __attribute__((always_inline))
 Release the next pending input value, accessible by pushpeek(), into the queue.
 
bool IRAM_ATTR push (T &&val) __attribute__((always_inline))
 Move the rvalue parameter into the queue.
 
bool IRAM_ATTR push (const T &val) __attribute__((always_inline))
 Push a copy of the parameter into the queue.
 
size_t push_n (const T *buffer, size_t size)
 Push copies of multiple elements from a buffer into the queue, in order, beginning at buffer's head.
 
pop ()
 Pop the next available element from the queue.
 
size_t pop_n (T *buffer, size_t size)
 Pop multiple elements in ordered sequence from the queue to a buffer. If buffer is nullptr, simply discards up to size elements from the queue.
 
void for_each (const Delegate< void(T &&), void > &fun)
 Iterate over and remove each available element from queue, calling back fun with an rvalue reference of every single element.
 
void for_each (Delegate< void(T &&), void > fun)
 
bool for_each_rev_requeue (const Delegate< bool(T &), void > &fun)
 In reverse order, iterate over, pop and optionally requeue each available element from the queue, calling back fun with a reference of every single element. Requeuing is dependent on the return boolean of the callback function. If it returns true, the requeue occurs.
 
bool for_each_rev_requeue (Delegate< bool(T &), void > fun)
 

Detailed Description

template<typename T, typename ForEachArg = void>
class circular_queue_mp< T, ForEachArg >

Instance class for a multi-producer, single-consumer circular queue / ring buffer (FIFO). This implementation is lock-free between producers and consumer for the available(), peek(), pop(), and push() type functions, but is guarded to safely allow only a single producer at any instant.

Constructor & Destructor Documentation

◆ circular_queue_mp() [1/3]

template<typename T , typename ForEachArg = void>
circular_queue_mp< T, ForEachArg >::circular_queue_mp ( )
default

◆ circular_queue_mp() [2/3]

template<typename T , typename ForEachArg = void>
circular_queue_mp< T, ForEachArg >::circular_queue_mp ( const size_t capacity)
inline

◆ circular_queue_mp() [3/3]

template<typename T , typename ForEachArg = void>
circular_queue_mp< T, ForEachArg >::circular_queue_mp ( circular_queue< T, ForEachArg > && cq)
inline

Member Function Documentation

◆ capacity()

template<typename T , typename ForEachArg = void>
bool circular_queue_mp< T, ForEachArg >::capacity ( const size_t cap)
inline

Resize the queue. The available elements in the queue are preserved. This is not lock-free, but safe, concurrent producer or consumer access is guarded.

Returns
True if the new capacity could accommodate the present elements in the queue, otherwise nothing is done and false is returned.

◆ for_each_requeue()

template<typename T , typename ForEachArg >
bool circular_queue_mp< T, ForEachArg >::for_each_requeue ( const Delegate< bool(T &), ForEachArg > & fun)

Iterate over, pop and optionally requeue each available element from the queue, calling back fun with a reference of every single element. Requeuing is dependent on the return boolean of the callback function. If it returns true, the requeue occurs.

◆ pop_requeue()

template<typename T , typename ForEachArg >
T & circular_queue_mp< T, ForEachArg >::pop_requeue ( )

Pops the next available element from the queue, requeues it immediately.

Returns
A reference to the just requeued element, or the default value of type T if the queue is empty.

◆ push() [1/3]

template<typename T , typename ForEachArg = void>
bool IRAM_ATTR circular_queue_mp< T, ForEachArg >::push ( )
delete

◆ push() [2/3]

template<typename T , typename ForEachArg = void>
bool IRAM_ATTR circular_queue_mp< T, ForEachArg >::push ( const T & val)
inline

Push a copy of the parameter into the queue, guarded for multiple concurrent producers.

Returns
true if the queue accepted the value, false if the queue was full.

◆ push() [3/3]

template<typename T , typename ForEachArg = void>
bool IRAM_ATTR circular_queue_mp< T, ForEachArg >::push ( T && val)
inline

Move the rvalue parameter into the queue, guarded for multiple concurrent producers.

Returns
true if the queue accepted the value, false if the queue was full.

◆ push_n()

template<typename T , typename ForEachArg = void>
size_t circular_queue_mp< T, ForEachArg >::push_n ( const T * buffer,
size_t size )
inline

Push copies of multiple elements from a buffer into the queue, in order, beginning at buffer's head. This is guarded for multiple producers, push_n() is atomic.

Returns
The number of elements actually copied into the queue, counted from the buffer head.

Member Data Documentation

◆ m_pushMtx

template<typename T , typename ForEachArg = void>
std::mutex circular_queue_mp< T, ForEachArg >::m_pushMtx
protected

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