Struct SpscRing
pub struct SpscRing<T>{ /* private fields */ }Expand description
Lock-free single-producer single-consumer ring buffer.
The hot path for audio data. No heap allocation after initialization. Uses atomic head/tail pointers with cache-line padding to prevent false sharing.
§Performance
- Wait-free on the fast path (atomic store/load only)
- Power-of-two capacity for bitwise modulo (single-cycle AND vs multi-cycle DIV)
- Bounded memory, no allocation after init
§Safety
This buffer is safe for concurrent use by exactly one producer and one consumer. Multiple producers or multiple consumers will cause data races.
Implementations§
§impl<T> SpscRing<T>
impl<T> SpscRing<T>
pub fn new(capacity: usize) -> SpscRing<T>
pub fn new(capacity: usize) -> SpscRing<T>
Create a new ring buffer with the given capacity.
Capacity is rounded up to the next power of two for efficient modulo.
§Panics
Panics if capacity is 0.
pub fn write(&self, data: &[T]) -> usize
pub fn write(&self, data: &[T]) -> usize
Write samples into the ring buffer.
Returns the number of samples actually written (may be less than
data.len() if the buffer is nearly full).
This is the producer method — call from exactly one thread.
pub fn read(&self, out: &mut [T]) -> usize
pub fn read(&self, out: &mut [T]) -> usize
Read samples from the ring buffer into out.
Returns the number of samples actually read (may be less than
out.len() if the buffer has fewer samples available).
This is the consumer method — call from exactly one thread.
pub fn clear(&self)
pub fn clear(&self)
Discard all buffered data without reading it.