arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | List of all members
RingBufferSPSC< T > Class Template Reference

Lock-free Single-Producer Single-Consumer ring buffer. More...

#include <RingBufferSPSC.h>

Inheritance diagram for RingBufferSPSC< T >:
BaseBuffer< T >

Public Member Functions

 RingBufferSPSC ()=default
 
 RingBufferSPSC (const RingBufferSPSC &)=delete
 
 RingBufferSPSC (size_t capacity)
 
 ~RingBufferSPSC () override
 
Taddress () override
 returns the address of the start of the physical read buffer
 
int available () override
 provides the number of entries that are available to read
 
int availableForWrite () override
 provides the number of entries that are available to write
 
virtual int bufferCountEmpty ()
 Provides the number of entries that are available to write: -1 does not apply.
 
virtual int bufferCountFilled ()
 Provides the number of entries that are available to read: -1 does not apply.
 
void clear ()
 same as reset
 
virtual int clearArray (int len)
 Removes the next len entries.
 
virtual void flush ()
 
bool isEmpty ()
 
virtual bool isFull ()
 checks if the buffer is full
 
virtual float levelPercent ()
 Returns the level of the buffer in %.
 
RingBufferSPSCoperator= (const RingBufferSPSC &)=delete
 
bool peek (T &result) override
 peeks the actual entry from the buffer
 
bool read (T &result) override
 reads a single value
 
int readArray (T data[], int len) override
 reads multiple values
 
void reset () override
 clears the buffer
 
bool resize (size_t capacity) override
 Resizes the buffer if supported: returns false if not supported.
 
size_t size () override
 
bool write (T data) override
 write add an entry to the buffer
 
int writeArray (const T data[], int len) override
 Fills the buffer data.
 
virtual int writeArrayOverwrite (const T data[], int len)
 Fills the buffer data and overwrites the oldest data if the buffer is full.
 

Detailed Description

template<typename T = uint8_t>
class audio_tools::RingBufferSPSC< T >

Lock-free Single-Producer Single-Consumer ring buffer.

Safe to use from two cores / contexts simultaneously only when there is exactly one writer and exactly one reader — e.g. an ISR or a second core filling the buffer while the main loop drains it (USB audio RX path on RP2040 with core-1 tud_task).

Design ──────

Not suitable for MPMC (multiple producers or consumers); use QueueLockFree for that.

Template Parameters
TElement type. Works for any trivially-copyable T; the bulk-copy optimisation is most effective for T=uint8_t (audio streams).
Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ RingBufferSPSC() [1/3]

template<typename T = uint8_t>
RingBufferSPSC ( )
default

◆ RingBufferSPSC() [2/3]

template<typename T = uint8_t>
RingBufferSPSC ( size_t  capacity)
inlineexplicit

◆ ~RingBufferSPSC()

template<typename T = uint8_t>
~RingBufferSPSC ( )
inlineoverride

◆ RingBufferSPSC() [3/3]

template<typename T = uint8_t>
RingBufferSPSC ( const RingBufferSPSC< T > &  )
delete

Member Function Documentation

◆ address()

template<typename T = uint8_t>
T * address ( )
inlineoverridevirtual

returns the address of the start of the physical read buffer

Implements BaseBuffer< T >.

◆ available()

template<typename T = uint8_t>
int available ( )
inlineoverridevirtual

provides the number of entries that are available to read

Implements BaseBuffer< T >.

◆ availableForWrite()

template<typename T = uint8_t>
int availableForWrite ( )
inlineoverridevirtual

provides the number of entries that are available to write

Implements BaseBuffer< T >.

◆ bufferCountEmpty()

template<typename T = uint8_t>
virtual int bufferCountEmpty ( )
inlinevirtualinherited

Provides the number of entries that are available to write: -1 does not apply.

Reimplemented in SynchronizedNBufferRTOST< T >, SynchronizedNBufferZephyrT< T >, NBuffer< T >, NBuffer< Frame >, NBuffer< T >, and NBuffer< uint8_t >.

◆ bufferCountFilled()

template<typename T = uint8_t>
virtual int bufferCountFilled ( )
inlinevirtualinherited

Provides the number of entries that are available to read: -1 does not apply.

Reimplemented in SynchronizedNBufferRTOST< T >, SynchronizedNBufferZephyrT< T >, NBuffer< T >, NBuffer< Frame >, NBuffer< T >, and NBuffer< uint8_t >.

◆ clear()

template<typename T = uint8_t>
void clear ( )
inlineinherited

same as reset

◆ clearArray()

template<typename T = uint8_t>
virtual int clearArray ( int  len)
inlinevirtualinherited

◆ flush()

template<typename T = uint8_t>
virtual void flush ( )
inlinevirtualinherited

Submit any partially-filled write buffer so the reader can access it. Only meaningful for NBuffer-style block pools; no-op for ring buffers.

Reimplemented in NBuffer< T >, NBuffer< Frame >, NBuffer< T >, and NBuffer< uint8_t >.

◆ isEmpty()

template<typename T = uint8_t>
bool isEmpty ( )
inlineinherited

◆ isFull()

template<typename T = uint8_t>
virtual bool isFull ( )
inlinevirtualinherited

◆ levelPercent()

template<typename T = uint8_t>
virtual float levelPercent ( )
inlinevirtualinherited

Returns the level of the buffer in %.

Reimplemented in AdaptiveResamplingBuffer.

◆ operator=()

template<typename T = uint8_t>
RingBufferSPSC & operator= ( const RingBufferSPSC< T > &  )
delete

◆ peek()

template<typename T = uint8_t>
bool peek ( T result)
inlineoverridevirtual

peeks the actual entry from the buffer

Implements BaseBuffer< T >.

◆ read()

template<typename T = uint8_t>
bool read ( T result)
inlineoverridevirtual

reads a single value

Implements BaseBuffer< T >.

◆ readArray()

template<typename T = uint8_t>
int readArray ( T  data[],
int  len 
)
inlineoverridevirtual

reads multiple values

Reimplemented from BaseBuffer< T >.

◆ reset()

template<typename T = uint8_t>
void reset ( )
inlineoverridevirtual

clears the buffer

Implements BaseBuffer< T >.

◆ resize()

template<typename T = uint8_t>
bool resize ( size_t  bytes)
inlineoverridevirtual

Resizes the buffer if supported: returns false if not supported.

Reimplemented from BaseBuffer< T >.

◆ size()

template<typename T = uint8_t>
size_t size ( )
inlineoverridevirtual

Implements BaseBuffer< T >.

◆ write()

template<typename T = uint8_t>
bool write ( T  data)
inlineoverridevirtual

write add an entry to the buffer

Implements BaseBuffer< T >.

◆ writeArray()

template<typename T = uint8_t>
int writeArray ( const T  data[],
int  len 
)
inlineoverridevirtual

Fills the buffer data.

Reimplemented from BaseBuffer< T >.

◆ writeArrayOverwrite()

template<typename T = uint8_t>
virtual int writeArrayOverwrite ( const T  data[],
int  len 
)
inlinevirtualinherited

Fills the buffer data and overwrites the oldest data if the buffer is full.


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