|
Sound Byte Libs 1ee2ca6
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
|
Lock-free single-producer-single-consumer ring buffer. More...
#include <ring_buffer.hpp>
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. | |
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:
| T | Element type to store in buffer |
| Size | Buffer size (must be power of 2) |
| MemoryBarrierImpl | Platform-specific memory barrier implementation |
Definition at line 79 of file ring_buffer.hpp.
|
inline |
Construct empty ring buffer.
Definition at line 88 of file ring_buffer.hpp.
|
inlineconstexpr |
Get maximum buffer capacity.
Definition at line 191 of file ring_buffer.hpp.
|
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.
|
inline |
Check if buffer is empty.
Definition at line 159 of file ring_buffer.hpp.
|
inline |
Check if buffer is full.
Definition at line 168 of file ring_buffer.hpp.
|
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).
| item | Reference to store popped element |
Definition at line 133 of file ring_buffer.hpp.
|
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).
| item | Element to add to buffer |
Definition at line 102 of file ring_buffer.hpp.
|
inline |
Get number of elements in buffer.
Definition at line 180 of file ring_buffer.hpp.