Expand description
§spsc-ring
Lock-free SPSC ring buffer.
§Design
Sequence-number protocol: each slot carries a stamp that the producer writes
after storing the value, and the consumer checks before reading.
Acquire/Release ordering only (no SeqCst). Cache-line padding prevents
false sharing between producer and consumer cursors.
The SPSC contract is enforced at compile time: ring returns a
(Producer<T>, Consumer<T>) pair. Each half is Send but not Clone.
§Example
use spsc_ring::{ring, TryRecvError, TrySendError};
use std::thread;
let (tx, rx) = ring::<u64>(64).unwrap();
thread::spawn(move || {
for i in 0..100u64 {
loop {
match tx.try_push(i) {
Ok(()) => break,
Err(TrySendError::Full(_)) => std::hint::spin_loop(),
Err(TrySendError::Disconnected(_)) => return,
}
}
}
});
let mut received = Vec::new();
while received.len() < 100 {
match rx.try_pop() {
Ok(v) => received.push(v),
Err(TryRecvError::Empty) => std::hint::spin_loop(),
Err(TryRecvError::Disconnected) => break,
}
}
assert_eq!(received, (0..100).collect::<Vec<_>>());Structs§
- Consumer
- Read half of the SPSC ring. Not
Clone— only one consumer exists. - Invalid
Capacity - Error returned by
ringwhen capacity is zero or not a power of two. - Producer
- Write half of the SPSC ring. Not
Clone— only one producer exists. - Recv
Error - Error returned by
Consumer::popwhen the producer has been dropped. - Send
Error - Error returned by
Producer::pushwhen the consumer has been dropped.
Enums§
- TryRecv
Error - Error returned by
Consumer::try_pop. - TrySend
Error - Error returned by
Producer::try_push. - Wait
Strategy - Strategy used by blocking
Producer::pushandConsumer::popwhile waiting.
Functions§
- ring
- Create an SPSC ring buffer with the given capacity (must be a power of 2).