Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
sbl::system::RingBuffer< T, Size > Class Template Reference

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

Detailed Description

template<typename T, uint32_t Size>
class sbl::system::RingBuffer< T, Size >

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.

Ordering

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:

  • Single-core bare metal (Cortex-M): producer and consumer share one core - an ISR preempting a thread is like a signal handler - so the fences are atomic_signal_fence, compiler-only, zero instructions.
  • Host (workbench, unit tests): producer and consumer may run on different cores, so the fences are atomic_thread_fence (DMB on AArch64).

One slot is reserved to tell full from empty: capacity is Size - 1.

Usage:

// Producer (ISR)
if (!rx.push(byte)) { ++overflows; }
// Consumer (main)
uint8_t b;
while (rx.pop(b)) { handle(b); }
Lock-free single-producer, single-consumer ring buffer.
bool pop(T &item)
Pop the oldest element (consumer side)
bool push(const T &item)
Push an element (producer side)
Context
push() from the producer context only, pop() from the consumer context only. empty(), full() and size() are snapshots from either side.
Template Parameters
TElement type (copied in and out)
SizeSlot 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 ()
 

Member Function Documentation

◆ push()

template<typename T , uint32_t Size>
bool sbl::system::RingBuffer< T, Size >::push ( const T &  item)
inline

Push an element (producer side)

Returns
false if the buffer is full; the element is not stored
Context
Producer context only, ISR included; bounded, non-blocking.

Definition at line 89 of file ring_buffer.hpp.

◆ pop()

template<typename T , uint32_t Size>
bool sbl::system::RingBuffer< T, Size >::pop ( T &  item)
inline

Pop the oldest element (consumer side)

Returns
false if the buffer is empty; item is unchanged
Context
Consumer context only, ISR included; bounded, non-blocking.

Definition at line 107 of file ring_buffer.hpp.

◆ empty()

template<typename T , uint32_t Size>
bool sbl::system::RingBuffer< T, Size >::empty ( ) const
inline

Definition at line 119 of file ring_buffer.hpp.

◆ full()

template<typename T , uint32_t Size>
bool sbl::system::RingBuffer< T, Size >::full ( ) const
inline

Definition at line 121 of file ring_buffer.hpp.

◆ size()

template<typename T , uint32_t Size>
uint32_t sbl::system::RingBuffer< T, Size >::size ( ) const
inline

Element count - a snapshot, may change immediately after the call.

Definition at line 126 of file ring_buffer.hpp.

◆ capacity()

template<typename T , uint32_t Size>
static constexpr uint32_t sbl::system::RingBuffer< T, Size >::capacity ( )
inlinestaticconstexpr

Definition at line 130 of file ring_buffer.hpp.

◆ clear()

template<typename T , uint32_t Size>
void sbl::system::RingBuffer< T, Size >::clear ( )
inline

Empty the buffer. Only while neither producer nor consumer runs.

Definition at line 133 of file ring_buffer.hpp.


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