Sound Byte Libs 1ee2ca6
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
Public Member Functions | List of all members
sbl::core::primitives::buffers::RingBuffer< T, Size, MemoryBarrierImpl > Class Template Reference

Lock-free single-producer-single-consumer ring buffer. More...

#include <ring_buffer.hpp>

Collaboration diagram for sbl::core::primitives::buffers::RingBuffer< T, Size, MemoryBarrierImpl >:

Public Member Functions

 RingBuffer ()
 Construct empty ring buffer.
 
bool push (const T &item)
 Push element to buffer (producer side)
 
bool pop (T &item)
 Pop element from buffer (consumer side)
 
bool empty () const
 Check if buffer is empty.
 
bool full () const
 Check if buffer is full.
 
common::types::BufferIndex size () const
 Get number of elements in buffer.
 
constexpr common::types::BufferIndex capacity () const
 Get maximum buffer capacity.
 
void clear ()
 Clear all elements from buffer.
 

Detailed Description

template<typename T, common::types::BufferSize Size, typename MemoryBarrierImpl>
class sbl::core::primitives::buffers::RingBuffer< T, Size, MemoryBarrierImpl >

Lock-free single-producer-single-consumer ring buffer.

Thread-safe ring buffer designed for communication between ISR and main contexts. Uses atomic operations and memory barriers to ensure data coherency without locks or blocking operations.

Key features:

Typical use cases:

Usage:

// Create ring buffer for audio samples
// (assumes platform provides MemoryBarrier type)
// In ISR (producer)
ISR(ADC_COMPLETE) {
int16_t sample = ADC_read();
if (!audio_buffer.push(sample)) {
// Buffer full - handle overflow
}
}
// In main loop (consumer)
void process_audio() {
int16_t sample;
while (audio_buffer.pop(sample)) {
// Process sample
apply_filter(sample);
}
}
Lock-free single-producer-single-consumer ring buffer.
bool push(const T &item)
Push element to buffer (producer side)
bool pop(T &item)
Pop element from buffer (consumer side)
Template Parameters
TElement type to store in buffer
SizeBuffer size (must be power of 2)
MemoryBarrierImplPlatform-specific memory barrier implementation

Definition at line 79 of file ring_buffer.hpp.

Constructor & Destructor Documentation

◆ RingBuffer()

template<typename T , common::types::BufferSize Size, typename MemoryBarrierImpl >
sbl::core::primitives::buffers::RingBuffer< T, Size, MemoryBarrierImpl >::RingBuffer ( )
inline

Construct empty ring buffer.

Definition at line 88 of file ring_buffer.hpp.

Member Function Documentation

◆ capacity()

template<typename T , common::types::BufferSize Size, typename MemoryBarrierImpl >
constexpr common::types::BufferIndex sbl::core::primitives::buffers::RingBuffer< T, Size, MemoryBarrierImpl >::capacity ( ) const
inlineconstexpr

Get maximum buffer capacity.

Returns
Maximum number of elements buffer can hold

Definition at line 191 of file ring_buffer.hpp.

◆ clear()

template<typename T , common::types::BufferSize Size, typename MemoryBarrierImpl >
void sbl::core::primitives::buffers::RingBuffer< T, Size, MemoryBarrierImpl >::clear ( )
inline

Clear all elements from buffer.

Resets buffer to empty state. Should only be called when both producer and consumer are stopped to avoid data races.

Definition at line 201 of file ring_buffer.hpp.

◆ empty()

template<typename T , common::types::BufferSize Size, typename MemoryBarrierImpl >
bool sbl::core::primitives::buffers::RingBuffer< T, Size, MemoryBarrierImpl >::empty ( ) const
inline

Check if buffer is empty.

Returns
true if buffer contains no elements

Definition at line 159 of file ring_buffer.hpp.

◆ full()

template<typename T , common::types::BufferSize Size, typename MemoryBarrierImpl >
bool sbl::core::primitives::buffers::RingBuffer< T, Size, MemoryBarrierImpl >::full ( ) const
inline

Check if buffer is full.

Returns
true if buffer cannot accept more elements

Definition at line 168 of file ring_buffer.hpp.

◆ pop()

template<typename T , common::types::BufferSize Size, typename MemoryBarrierImpl >
bool sbl::core::primitives::buffers::RingBuffer< T, Size, MemoryBarrierImpl >::pop ( T &  item)
inline

Pop element from buffer (consumer side)

Removes and returns the oldest element from the buffer if available. This method is designed to be called from the consumer context (typically the main thread).

Parameters
itemReference to store popped element
Returns
true if element was popped, false if buffer is empty

Definition at line 133 of file ring_buffer.hpp.

◆ push()

template<typename T , common::types::BufferSize Size, typename MemoryBarrierImpl >
bool sbl::core::primitives::buffers::RingBuffer< T, Size, MemoryBarrierImpl >::push ( const T &  item)
inline

Push element to buffer (producer side)

Adds element to the buffer if space is available. This method is ISR-safe and designed to be called from the producer context (typically an interrupt handler).

Parameters
itemElement to add to buffer
Returns
true if element was added, false if buffer is full
Note
This method has bounded execution time suitable for ISR use

Definition at line 102 of file ring_buffer.hpp.

◆ size()

template<typename T , common::types::BufferSize Size, typename MemoryBarrierImpl >
common::types::BufferIndex sbl::core::primitives::buffers::RingBuffer< T, Size, MemoryBarrierImpl >::size ( ) const
inline

Get number of elements in buffer.

Returns
Current number of elements
Note
This is a snapshot - value may change immediately after call

Definition at line 180 of file ring_buffer.hpp.


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