pub struct SpinLock<T, const SPIN: usize = 5, const YIELD: usize = 10, const SLEEP: u64 = 100> { /* private fields */ }unsafe_sync only.Expand description
๐งต A spinlock providing mutual exclusion without blocking.
๐ work/sync
Uses an atomic flag for synchronization, with a configurable backoff strategy.
ยงExamples
let lock = SpinLock::<i32, 5, 10, 100>::new(42);
let mut guard = lock.lock(); // Acquire the lock
*guard += 1; // Modify the locked value
if let Some(mut guard) = lock.try_lock() {
*guard *= 2; // Modify only if lock was acquired
}
// Lock is automatically released when `guard` goes out of scopeImplementationsยง
Sourceยงimpl<T, const SPIN: usize, const YIELD: usize, const SLEEP: u64> SpinLock<T, SPIN, YIELD, SLEEP>
impl<T, const SPIN: usize, const YIELD: usize, const SLEEP: u64> SpinLock<T, SPIN, YIELD, SLEEP>
Sourcepub fn lock(&self) -> SpinLockGuard<'_, T, SPIN, YIELD, SLEEP>
pub fn lock(&self) -> SpinLockGuard<'_, T, SPIN, YIELD, SLEEP>
Acquires the lock, spinning until it is available.
This method employs an adaptive backoff strategy to minimize CPU contention:
- Spins (
spin_loop()) forSPINiterations to avoid unnecessary thread switching. - Yields (
ThreadExt::yield_now()) for the nextYIELD - SPINiterations, allowing other threads to progress. - Sleeps (
ThreadExt::sleep_ns(SLEEP)) ifSLEEP > 0, reducing CPU load under high contention.
ยงEnvironment
- In
stdenvironments, the full backoff strategy is used. - In
no_stdenvironments, onlyspin_loop()is available.
ยงCompile-Time Optimization
Unnecessary branches are removed at compile time if their respective constant is set to 0.
Sourcepub fn try_lock(&self) -> Option<SpinLockGuard<'_, T, SPIN, YIELD, SLEEP>> โ
pub fn try_lock(&self) -> Option<SpinLockGuard<'_, T, SPIN, YIELD, SLEEP>> โ
Attempts to acquire the lock without blocking.
Returns Some(SpinLockGuard<T>) if successful, otherwise None.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the lock and returns the inner value.
Sourcepub fn try_into_inner(&self) -> Option<T> โ
pub fn try_into_inner(&self) -> Option<T> โ
Tries to consume the lock and return the inner value.
If the lock is currently unlocked, this method returns Some(T).
Otherwise, it returns None without blocking or modifying the lock.
Sourcepub unsafe fn debug_force_unlock(&self)
Available on debug-assertions enabled only.
pub unsafe fn debug_force_unlock(&self)
Forces the lock to be released without dropping the guard.
ยงSafety
- This is a manual unlocking mechanism. If used incorrectly,
it may allow multiple threads to access
Tsimultaneously.
Trait Implementationsยง
Sourceยงimpl<T, const SPIN: usize, const YIELD: usize, const SLEEP: u64> Debug for SpinLock<T, SPIN, YIELD, SLEEP>
impl<T, const SPIN: usize, const YIELD: usize, const SLEEP: u64> Debug for SpinLock<T, SPIN, YIELD, SLEEP>
Sourceยงimpl<T: Default, const SPIN: usize, const YIELD: usize, const SLEEP: u64> Default for SpinLock<T, SPIN, YIELD, SLEEP>
impl<T: Default, const SPIN: usize, const YIELD: usize, const SLEEP: u64> Default for SpinLock<T, SPIN, YIELD, SLEEP>
impl<T> Send for SpinLock<T>where
T: Send,
impl<T> Sync for SpinLock<T>where
T: Send,
Auto Trait Implementationsยง
impl<T, const SPIN: usize = 5, const YIELD: usize = 10, const SLEEP: u64 = 100> !Freeze for SpinLock<T, SPIN, YIELD, SLEEP>
impl<T, const SPIN: usize = 5, const YIELD: usize = 10, const SLEEP: u64 = 100> !RefUnwindSafe for SpinLock<T, SPIN, YIELD, SLEEP>
impl<T, const SPIN: usize = 5, const YIELD: usize = 10, const SLEEP: u64 = 100> !Send for SpinLock<T, SPIN, YIELD, SLEEP>
impl<T, const SPIN: usize = 5, const YIELD: usize = 10, const SLEEP: u64 = 100> !Sync for SpinLock<T, SPIN, YIELD, SLEEP>
impl<T, const SPIN: usize, const YIELD: usize, const SLEEP: u64> Unpin for SpinLock<T, SPIN, YIELD, SLEEP>where
T: Unpin,
impl<T, const SPIN: usize, const YIELD: usize, const SLEEP: u64> UnsafeUnpin for SpinLock<T, SPIN, YIELD, SLEEP>where
T: UnsafeUnpin,
impl<T, const SPIN: usize, const YIELD: usize, const SLEEP: u64> UnwindSafe for SpinLock<T, SPIN, YIELD, SLEEP>where
T: UnwindSafe,
Blanket Implementationsยง
Sourceยงimpl<T> AnyExt for T
impl<T> AnyExt for T
Sourceยงfn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
TypeId of Self using a custom hasher.Sourceยงfn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Sourceยงfn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
fn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
alloc only.Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> ByteSized for T
impl<T> ByteSized for T
Sourceยงconst BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Sourceยงfn byte_align(&self) -> usize
fn byte_align(&self) -> usize
Sourceยงfn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Sourceยงimpl<T> MemExt for Twhere
T: ?Sized,
impl<T> MemExt for Twhere
T: ?Sized,
Sourceยงconst NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Sourceยงfn mem_align_of<T>() -> usize
fn mem_align_of<T>() -> usize
Sourceยงfn mem_align_of_val(&self) -> usize
fn mem_align_of_val(&self) -> usize
Sourceยงfn mem_size_of<T>() -> usize
fn mem_size_of<T>() -> usize
Sourceยงfn mem_size_of_val(&self) -> usize
fn mem_size_of_val(&self) -> usize
Sourceยงfn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true if dropping values of this type matters. Read moreSourceยงfn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self without running its destructor. Read moreSourceยงfn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Sourceยงunsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSourceยงunsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSourceยงfn mem_as_bytes(&self) -> &[u8] โ
fn mem_as_bytes(&self) -> &[u8] โ
unsafe_slice only.