Expand description
Lock-free single-producer/multi-consumer ring buffer used by
dashboard_ws to fan out live training events without an unbounded
backlog. Was previously compiled (mod ring_buffer; was missing from
this file) but never reachable from outside the crate, so its tests
never ran; declared here rather than deleted since dashboard_ws
depends on it and both are real, working implementations.
Lock-free single-producer single-consumer (SPSC) ring buffer.
Uses power-of-2 capacity so that modulo operations reduce to bitwise AND.
The implementation is based on two atomic indices (head for writes, tail
for reads) with Acquire/Release ordering — the same well-known pattern
used by LMAX Disruptor and many embedded real-time systems.
§Concurrency model
LockFreeRingBuffer is SPSC — exactly one producer and one
consumer thread at a time. The Send + Sync implementations are
deliberately provided because the buffer is safe to move across threads;
it is the caller’s responsibility to ensure only one thread pushes and
one thread pops concurrently.
§Example
use trustformers_debug::ring_buffer::LockFreeRingBuffer;
use std::sync::Arc;
use std::thread;
let buf: Arc<LockFreeRingBuffer<u64>> = Arc::new(LockFreeRingBuffer::new(16));
let producer = Arc::clone(&buf);
let consumer = Arc::clone(&buf);
let t = thread::spawn(move || {
for i in 0..8_u64 {
while producer.push(i).is_err() {}
}
});
t.join().unwrap();
for i in 0..8_u64 {
assert_eq!(consumer.pop(), Some(i));
}Structs§
- Lock
Free Ring Buffer - Lock-free SPSC ring buffer with power-of-2 capacity.
- Statistics
Window - A simple sliding-window buffer that retains the most-recent
capacityvalues and exposes statistical helpers. - Timestamped
Ring Buffer - An SPSC ring buffer that stores
TimestampedValueentries and exposes time-range queries and throughput estimation. - Timestamped
Value - A value paired with a nanosecond-resolution timestamp.
Enums§
- Ring
Buffer Error - Errors that can arise from ring-buffer operations.