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>
impl<T> SpinLock<T>
Sourcepub fn lock(&self) -> SpinLockGuard<'_, T>
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
Sourcepub fn try_lock(&self) -> Option<SpinLockGuard<'_, T>>
pub fn try_lock(&self) -> Option<SpinLockGuard<'_, T>>
Try to lock without spinning.