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
impl LockFreeRingBuffer
Sourcepub fn new(capacity: usize) -> Self
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.
Sourcepub fn capacity(&self) -> usize
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.
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns true when the buffer is full (cannot accept more writes).
Sourcepub fn write(&self, sample: f32) -> bool
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.
Sourcepub fn read(&self) -> Option<f32>
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.
Sourcepub fn write_samples(&self, samples: &[f32]) -> usize
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.
Sourcepub fn read_samples(&self, dst: &mut [f32]) -> usize
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.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for LockFreeRingBuffer
impl RefUnwindSafe for LockFreeRingBuffer
impl Send for LockFreeRingBuffer
impl Sync for LockFreeRingBuffer
impl Unpin for LockFreeRingBuffer
impl UnsafeUnpin for LockFreeRingBuffer
impl UnwindSafe for LockFreeRingBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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