|
Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
|
Lock-free single-producer, single-consumer ring buffer. More...
Lock-free single-producer, single-consumer ring buffer.
Carries data from one execution context to another without blocking either: ISR -> main on hardware, thread -> thread on the workbench. Every operation is bounded and allocation-free.
The indices are relaxed std::atomic<uint32_t> - no torn or invented accesses, and no data race in the C++ sense. Ordering between the slot contents and the index that publishes them comes from a fence pair:
producer: write slot -> release fence -> store head consumer: load head -> acquire fence -> read slot -> release fence -> store tail
The fence strength follows the target:
atomic_signal_fence, compiler-only, zero instructions.atomic_thread_fence (DMB on AArch64).One slot is reserved to tell full from empty: capacity is Size - 1.
Usage:
| T | Element type (copied in and out) |
| Size | Slot count, a power of 2 from 2 to 65536; capacity is Size - 1 |
Definition at line 75 of file ring_buffer.hpp.
#include <ring_buffer.hpp>
Public Member Functions | |
| bool | push (const T &item) |
| Push an element (producer side) | |
| bool | pop (T &item) |
| Pop the oldest element (consumer side) | |
| bool | empty () const |
| bool | full () const |
| uint32_t | size () const |
| void | clear () |
Static Public Member Functions | |
| static constexpr uint32_t | capacity () |
|
inline |
Push an element (producer side)
Definition at line 89 of file ring_buffer.hpp.
|
inline |
Pop the oldest element (consumer side)
item is unchanged Definition at line 107 of file ring_buffer.hpp.
|
inline |
Definition at line 119 of file ring_buffer.hpp.
|
inline |
Definition at line 121 of file ring_buffer.hpp.
|
inline |
Element count - a snapshot, may change immediately after the call.
Definition at line 126 of file ring_buffer.hpp.
|
inlinestaticconstexpr |
Definition at line 130 of file ring_buffer.hpp.
|
inline |
Empty the buffer. Only while neither producer nor consumer runs.
Definition at line 133 of file ring_buffer.hpp.