Skip to main content

SpinLock

Struct SpinLock 

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

A spin lock for very short critical sections with TBB-inspired exponential backoff.

This implementation uses exponential backoff and adaptive yielding for better performance under contention. The lock is cache-line aligned to prevent false sharing.

Use only when you know the critical section is extremely short (< 1μs).

locked is wrapped in CacheAligned, which both aligns the lock to moirai_utils::DESTRUCTIVE_INTERFERENCE_SIZE (so a neighbouring object cannot share its sector) and separates the contended flag from data (so a spinning acquirer does not invalidate the payload). The separation is per-target and owned by moirai-utils, not a literal here.

Implementations§

Source§

impl<T> SpinLock<T>

Source

pub const fn new(data: T) -> Self

Create a new spin lock.

Source

pub fn lock(&self) -> SpinLockGuard<'_, T>

Lock the spin lock with TBB-inspired exponential backoff.

This implementation uses:

  • Read-before-CAS to reduce memory contention
  • Exponential backoff starting from 1 iteration up to 64
  • Adaptive yielding after prolonged spinning
Source

pub fn try_lock(&self) -> Option<SpinLockGuard<'_, T>>

Try to lock without spinning.

Trait Implementations§

Source§

impl<T> Debug for SpinLock<T>

Source§

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

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

impl<T: Send> Send for SpinLock<T>

Source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for SpinLock<T>

§

impl<T> !RefUnwindSafe for SpinLock<T>

§

impl<T> Unpin for SpinLock<T>
where UnsafeCell<T>: Unpin,

§

impl<T> UnsafeUnpin for SpinLock<T>

§

impl<T> UnwindSafe for SpinLock<T>

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.