Skip to main content

BoundedMpmcQueue

Struct BoundedMpmcQueue 

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

Lock-free bounded multi-producer/multi-consumer FIFO queue of fixed capacity.

try_push/try_pop never block; they return Err/None immediately if the queue is full/empty rather than waiting — callers needing to wait layer their own backoff/parking on top (this is what sync::mpsc does with its WaitQueue-based registration).

Implementations§

Source§

impl<T> BoundedMpmcQueue<T>

Source

pub fn new(capacity: usize) -> Self

Build an empty queue holding up to capacity elements (rounded up to at least 1).

Source

pub const fn capacity(&self) -> usize

The fixed capacity this queue was constructed with.

Source

pub fn try_push(&self, value: T) -> Result<(), T>

Push value, returning it back in Err if the queue is currently full. Never blocks.

§Errors

Returns value back, unmodified, if the queue is at capacity.

Source

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

Pop the oldest pushed value, or None if the queue is currently empty. Never blocks.

Source

pub fn enqueue_snapshot(&self) -> usize

A snapshot of how many try_push calls have claimed a slot so far (via winning the enqueue_pos CAS), including any still in-flight (claimed but not yet published). Paired with [Self::try_pop_until] so a drain-everything caller can wait out an in-flight push rather than mistaking it for “queue empty”.

Source

pub fn drain_until(&self, target: usize, on_pop: impl FnMut(T))

Pop everything pushed up to (not including) target (an Self::enqueue_snapshot taken earlier), spin-waiting out any push that’s claimed a slot but not yet published rather than treating a transient empty read as “done”.

Ordinary Self::try_pop alone can’t safely implement “drain every currently-registered entry”: it returns None the instant dequeue catches up to a slot that’s been claimed (CAS won) but not yet published (its value written and sequence released), which looks identical to a genuinely empty queue. A caller that stops draining on the first None can permanently miss that about-to-be-published entry if this was the last drain anyone will ever perform (see sync::broadcast’s module doc for the real, reproducible hang this caused via WaitQueue::wake_all). Looping until target is reached closes that window: any slot below target was claimed before this call started, so it can only be transiently unpublished, never permanently absent.

Source

pub fn len(&self) -> usize

Approximate current length — racy under concurrent push/pop, same caveat as every other lock-free length check in this module.

Source

pub fn is_empty(&self) -> bool

Whether the queue currently has no elements. Racy, same caveat as Self::len.

Trait Implementations§

Source§

impl<T> Drop for BoundedMpmcQueue<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 BoundedMpmcQueue<T>

Source§

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

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