LockFreeStack

Struct LockFreeStack 

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

A lock-free stack implementation using Treiber’s algorithm

This stack provides:

  • Wait-free push operations
  • Lock-free pop operations
  • ABA problem prevention through proper memory management
  • Performance metrics collection

§Type Parameters

  • T - The type of elements stored in the stack

§Examples

use velocityx::stack::LockFreeStack;

let stack = LockFreeStack::new();

// Push elements
stack.push(1);
stack.push(2);
stack.push(3);

// Pop elements
assert_eq!(stack.pop(), Some(3));
assert_eq!(stack.pop(), Some(2));
assert_eq!(stack.pop(), Some(1));
assert_eq!(stack.pop(), None);

Implementations§

Source§

impl<T> LockFreeStack<T>

Source

pub fn new() -> Self

Create a new empty lock-free stack

§Examples
use velocityx::stack::LockFreeStack;

let stack: LockFreeStack<i32> = LockFreeStack::new();
Source

pub fn push(&self, value: T)

Push a value onto the stack

This operation is wait-free and will always complete in a bounded number of steps.

§Arguments
  • value - The value to push onto the stack
§Examples
use velocityx::stack::LockFreeStack;

let stack = LockFreeStack::new();
stack.push(42);
Source

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

Pop a value from the stack

This operation is lock-free and will complete if other threads make progress.

§Returns
  • Some(value) if the stack was not empty
  • None if the stack was empty
§Examples
use velocityx::stack::LockFreeStack;

let stack = LockFreeStack::new();
stack.push(42);

let value = stack.pop();
assert_eq!(value, Some(42));
Source

pub fn is_empty(&self) -> bool

Check if the stack is empty

This operation is lock-free and provides a snapshot of the stack’s state.

§Returns

true if the stack is empty, false otherwise

§Examples
use velocityx::stack::LockFreeStack;

let stack = LockFreeStack::new();
assert!(stack.is_empty());

stack.push(42);
assert!(!stack.is_empty());
Source

pub fn len(&self) -> usize

Get the approximate number of elements in the stack

Note: This is an expensive operation as it traverses the entire stack. Use only for debugging or monitoring purposes.

§Returns

The approximate number of elements in the stack

§Examples
use velocityx::stack::LockFreeStack;

let stack = LockFreeStack::new();
assert_eq!(stack.len(), 0);

stack.push(1);
stack.push(2);
assert_eq!(stack.len(), 2);
Source

pub fn pop_batch(&self, max_count: usize) -> Vec<T>

Try to pop multiple elements at once

This can be more efficient than individual pops as it reduces atomic operations.

§Arguments
  • max_count - Maximum number of elements to pop
§Returns

A vector containing the popped elements (in LIFO order)

§Examples
use velocityx::stack::LockFreeStack;

let stack = LockFreeStack::new();
stack.push(1);
stack.push(2);
stack.push(3);

let elements = stack.pop_batch(2);
assert_eq!(elements.len(), 2);
Source

pub fn push_batch<I>(&self, values: I)
where I: IntoIterator<Item = T>,

Push multiple elements at once

Elements are pushed in the order they appear in the iterator. The last element will be at the top of the stack.

§Arguments
  • values - Iterator of values to push
§Examples
use velocityx::stack::LockFreeStack;

let stack = LockFreeStack::new();
let values = vec![1, 2, 3];
stack.push_batch(values);

assert_eq!(stack.pop(), Some(3));
assert_eq!(stack.pop(), Some(2));
assert_eq!(stack.pop(), Some(1));

Trait Implementations§

Source§

impl<T: Debug> Debug for LockFreeStack<T>

Source§

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

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

impl<T> Default for LockFreeStack<T>

Source§

fn default() -> Self

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§

impl<T> MetricsCollector for LockFreeStack<T>

Available on crate feature std only.
Source§

fn metrics(&self) -> PerformanceMetrics

Get current performance metrics
Source§

fn reset_metrics(&self)

Reset all metrics
Source§

fn set_metrics_enabled(&self, enabled: bool)

Enable or disable metrics collection
Source§

fn is_metrics_enabled(&self) -> bool

Check if metrics collection is enabled

Auto Trait Implementations§

§

impl<T> !Freeze for LockFreeStack<T>

§

impl<T> RefUnwindSafe for LockFreeStack<T>

§

impl<T> Send for LockFreeStack<T>

§

impl<T> Sync for LockFreeStack<T>

§

impl<T> Unpin for LockFreeStack<T>

§

impl<T> UnwindSafe for LockFreeStack<T>
where T: RefUnwindSafe,

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.