|
Sound Byte Libs 29c5ff3
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. | |
| uint32_t | size () const |
| Get number of elements in buffer. | |
| constexpr uint32_t | 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 volatile indices and memory barriers to ensure data coherency without locks or blocking operations.
On single-core Cortex-M, volatile guarantees that reads/writes are not optimized away or reordered by the compiler. The memory barrier (provided via template parameter) ensures data is committed before the index update becomes visible.
Key features:
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 72 of file ring_buffer.hpp.
|
inline |
Construct empty ring buffer.
Definition at line 81 of file ring_buffer.hpp.
|
inlineconstexpr |
Get maximum buffer capacity.
Definition at line 180 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 190 of file ring_buffer.hpp.
|
inline |
Check if buffer is empty.
Definition at line 151 of file ring_buffer.hpp.
|
inline |
Check if buffer is full.
Definition at line 160 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 126 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 95 of file ring_buffer.hpp.
|
inline |
Get number of elements in buffer.
Definition at line 171 of file ring_buffer.hpp.