Sound Byte Libs 0.5.1-121-g3358a44
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
ring_buffer.hpp File Reference

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

Detailed Description

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. A DMB would be the wrong strength, not a safer one (interrupt research doc §5.3).
  • 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:

RingBuffer<uint8_t, 256> rx;
// Producer (ISR)
if (!rx.push(byte)) { ++overflows; }
// Consumer (main)
uint8_t b;
while (rx.pop(b)) { handle(b); }
Template Parameters
TElement type (copied in and out)
SizeSlot count, a power of 2
Note
push() from the producer context only, pop() from the consumer context only. empty()/full()/size() are snapshots from either side.

Definition in file ring_buffer.hpp.

#include <atomic>
#include <cstdint>

Go to the source code of this file.

Classes

class  sbl::primitives::buffers::RingBuffer< T, Size >
 

Namespaces

namespace  sbl
 Root namespace for all Sound Byte Libs code.
 
namespace  sbl::primitives
 Hardware-side primitives: buffers and integer math.
 
namespace  sbl::primitives::buffers
 Ring buffers.
 
namespace  sbl::primitives::buffers::detail
 

Functions

void sbl::primitives::buffers::detail::ring_acquire_fence ()
 
void sbl::primitives::buffers::detail::ring_release_fence ()