Skip to main content

LockFreeRingBuffer

Struct LockFreeRingBuffer 

Source
pub struct LockFreeRingBuffer { /* private fields */ }
Expand description

Single-producer single-consumer lock-free ring buffer for f32 samples.

Designed for real-time audio pipelines where one thread writes audio data (the audio callback or capture thread) and another thread reads it (the processing or playback thread).

Samples are stored as their u32 bit patterns in AtomicU32 cells so that the entire structure is Send + Sync without any unsafe code. The SPSC ring-buffer protocol using AtomicUsize head/tail indices ensures correct ordering between producer and consumer.

§Capacity

The actual number of samples that can be buffered is capacity - 1. Choose a power-of-two capacity (e.g., 2048, 4096) for best performance.

§Thread safety

Only one writer and one reader are supported. Using more than one writer or more than one reader concurrently results in incorrect data ordering.

Implementations§

Source§

impl LockFreeRingBuffer

Source

pub fn new(capacity: usize) -> Self

Create a new ring buffer that can hold up to capacity - 1 samples.

capacity should be a power of two for best performance. A minimum capacity of 2 is enforced.

Source

pub fn capacity(&self) -> usize

Returns the total capacity of the buffer (number of samples that can ever be stored). The usable capacity is capacity() - 1.

Source

pub fn available(&self) -> usize

Number of samples currently available for reading.

Source

pub fn free(&self) -> usize

Number of free slots available for writing.

Source

pub fn is_empty(&self) -> bool

Returns true when the buffer contains no readable samples.

Source

pub fn is_full(&self) -> bool

Returns true when the buffer is full (cannot accept more writes).

Source

pub fn write(&self, sample: f32) -> bool

Write a single sample.

Returns true when successful, false when the buffer is full.

Must only be called from the producer thread.

Source

pub fn read(&self) -> Option<f32>

Read a single sample.

Returns Some(sample) when data is available, None when empty.

Must only be called from the consumer thread.

Source

pub fn write_samples(&self, samples: &[f32]) -> usize

Write a block of samples.

Returns the number of samples actually written (may be less than samples.len() when the buffer does not have enough free space).

Must only be called from the producer thread.

Source

pub fn read_samples(&self, dst: &mut [f32]) -> usize

Read samples into dst.

Returns the number of samples actually read (may be less than dst.len() when the buffer does not have enough data).

Must only be called from the consumer thread.

Source

pub fn clear(&self)

Clear all samples from the buffer.

Must only be called when no concurrent reads/writes are in progress.

Trait Implementations§

Source§

impl Debug for LockFreeRingBuffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.