pub struct RingQueue { /* private fields */ }Expand description
A bounded Multi-Producer Multi-Consumer (MPMC) queue utilizing a fixed-size ring buffer.
The implementation employs a sequence-based synchronization gate to coordinate access between threads, ensuring that producers and consumers only operate on slots that have been released by the previous “lap” of the buffer.
Memory and Hardware Considerations Cache Isolation: head, tail, and len cursors are isolated using CachePadded to mitigate false sharing and L1 cache-line contention between concurrent threads.
Memory Layout: Uses #[repr(C)] for a predictable field order, aiding hardware pre-fetchers.
Portability: Uses 64-bit atomics for cross-platform compatibility, including ARMv8 architectures where 128-bit atomic operations may not be natively supported.
Implementations§
Source§impl RingQueue
impl RingQueue
Sourcepub fn new(capacity: usize, backoff_config: BackoffConfig) -> Self
pub fn new(capacity: usize, backoff_config: BackoffConfig) -> Self
Creates a new queue with a capacity rounded up to the nearest power of two.
Each slot is initialized with a sequence number that permits the first lap of production.
Sourcepub fn push(&self, value: u64) -> Result<(), u64>
pub fn push(&self, value: u64) -> Result<(), u64>
Attempts to push a value into the buffer.
Returns Ok(()) if successful, or Err(u64) containing the value if the buffer is full.
§Synchronization
- Loads the slot sequence with
Acquireordering to observe the last consumer’s release. - Claims a slot via
compare_exchangeon thetailcursor. - Updates the slot value and publishes it by incrementing the sequence with
Releaseordering.
Sourcepub fn pop(&self) -> Option<u64>
pub fn pop(&self) -> Option<u64>
Attempts to pop a value from the buffer.
Returns Some(u64) if data is available, or None if the buffer is empty.
§Synchronization
- Validates that the sequence number matches the expected
headlap. - Claims the index via
compare_exchangeon theheadcursor. - Loads the value and resets the slot sequence to signal readiness to future producers (lap + capacity).