Skip to main content

LockFreeStack

Struct LockFreeStack 

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

Lock-free stack for object pooling.

§Safety

This implementation uses a pre-allocated array of slots with a generation counter packed in an AtomicU64 to prevent ABA problems and use-after-free without blocking.

§Capacity

The slot array is fixed at construction; Self::push returns the item back when every slot is occupied. Use Self::with_capacity to size the stack; Self::new uses DEFAULT_STACK_CAPACITY.

§Performance Characteristics

  • Push: O(1) amortized, < 20ns
  • Pop: O(1) amortized, < 30ns
  • Thread-safe: All operations are lock-free

Implementations§

Source§

impl<T> LockFreeStack<T>

Source

pub fn new() -> LockFreeStack<T>

Create a new empty lock-free stack with DEFAULT_STACK_CAPACITY slots.

Source

pub fn with_capacity(capacity: usize) -> LockFreeStack<T>

Create a new empty lock-free stack with exactly capacity slots.

A capacity of 0 yields a stack whose push always returns the item back.

§Panics

Panics if capacity >= u32::MAX (the sentinel index must stay unused).

Source

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

Push an item onto the stack.

§Errors

Returns Err(item) — handing the value back to the caller — when every slot is occupied. The item is never silently dropped.

Source

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

Pop an item from the stack.

Source

pub fn len(&self) -> usize

Get the current length of the stack.

Source

pub fn is_empty(&self) -> bool

Check if the stack is empty.

Source

pub fn capacity(&self) -> usize

Get the fixed slot capacity of the stack.

Trait Implementations§

Source§

impl<T> Default for LockFreeStack<T>

Source§

fn default() -> LockFreeStack<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Drop for LockFreeStack<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 for LockFreeStack<T>
where T: Send,

Source§

impl<T> Sync for LockFreeStack<T>
where T: Send,

Auto Trait Implementations§

§

impl<T> !Freeze for LockFreeStack<T>

§

impl<T> !RefUnwindSafe for LockFreeStack<T>

§

impl<T> Unpin for LockFreeStack<T>
where Box<[StackNode<T>]>: Unpin,

§

impl<T> UnsafeUnpin for LockFreeStack<T>
where Box<[StackNode<T>]>: UnsafeUnpin,

§

impl<T> UnwindSafe for LockFreeStack<T>
where Box<[StackNode<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 = !

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

fn try_from(value: U) -> Result<T, !>

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.