Skip to main content

RingQueue

Struct RingQueue 

Source
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

Source

pub fn new(capacity: usize) -> 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.

Source

pub fn push(&self, value: u64, context: &ThreadContext) -> 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
  1. Loads the slot sequence with Acquire ordering to observe the last consumer’s release.
  2. Claims a slot via compare_exchange on the tail cursor.
  3. Updates the slot value and publishes it by incrementing the sequence with Release ordering.
Source

pub fn pop(&self, context: &ThreadContext) -> 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
  1. Validates that the sequence number matches the expected head lap.
  2. Claims the index via compare_exchange on the head cursor.
  3. Loads the value and resets the slot sequence to signal readiness to future producers (lap + capacity).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V