Skip to main content

LockFreeRing

Struct LockFreeRing 

Source
pub struct LockFreeRing<T> { /* private fields */ }
Expand description

Lock-free SPSC ring buffer.

This ring buffer is designed for Single-Producer Single-Consumer scenarios, where one thread enqueues items and another dequeues them. It uses atomic operations for synchronization without any locks.

§Performance

  • Enqueue: O(1) amortized
  • Dequeue: O(1) amortized
  • Batch operations amortize atomic overhead

§Cache Optimization

  • Head and tail indices are cache-line padded to prevent false sharing
  • Buffer capacity is always a power of 2 for fast modulo via bitwise AND

Implementations§

Source§

impl<T> LockFreeRing<T>

Source

pub fn new(capacity: usize) -> Self

Creates a new ring buffer with the specified capacity.

The actual capacity will be rounded up to the next power of 2.

§Panics

Panics if capacity is 0 or allocation fails.

Source

pub fn with_default_capacity() -> Self

Creates a ring buffer with the default capacity.

Source

pub const fn capacity(&self) -> usize

Returns the ring capacity.

Source

pub fn len(&self) -> usize

Returns the number of items currently in the ring.

Source

pub fn is_empty(&self) -> bool

Returns true if the ring is empty.

Source

pub fn is_full(&self) -> bool

Returns true if the ring is full.

Source

pub fn free_slots(&self) -> usize

Returns the number of free slots.

Source

pub fn enqueue(&self, item: T) -> Result<(), T>

Enqueues a single item.

Returns Err(item) if the ring is full.

Source

pub fn dequeue(&self) -> Option<T>

Dequeues a single item.

Returns None if the ring is empty.

Source

pub fn enqueue_batch(&self, items: &[T]) -> usize
where T: Copy,

Enqueues multiple items in a batch.

Returns the number of items successfully enqueued. Items that couldn’t be enqueued remain in the slice.

Source

pub fn dequeue_batch(&self, out: &mut [T]) -> usize
where T: Copy,

Dequeues multiple items in a batch.

Returns the number of items dequeued.

Source

pub unsafe fn peek(&self) -> Option<&T>

Peeks at the next item to be dequeued without removing it.

§Safety

The returned reference is only valid until the next dequeue operation.

Source

pub unsafe fn clear(&self)

Clears all items from the ring.

§Safety

This should only be called when no concurrent operations are in progress.

Trait Implementations§

Source§

impl<T> Debug for LockFreeRing<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for LockFreeRing<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: Send> Send for LockFreeRing<T>

Source§

impl<T: Send> Sync for LockFreeRing<T>

Auto Trait Implementations§

§

impl<T> !Freeze for LockFreeRing<T>

§

impl<T> !RefUnwindSafe for LockFreeRing<T>

§

impl<T> Unpin for LockFreeRing<T>

§

impl<T> UnsafeUnpin for LockFreeRing<T>

§

impl<T> UnwindSafe for LockFreeRing<T>
where T: UnwindSafe,

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, 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.