pub struct SpinLock<T> { /* private fields */ }
Expand description
a primitive for mutual exclusion that spins in a loop
Implementations§
source§impl<T> SpinLock<T>
impl<T> SpinLock<T>
sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Creates a new spinlock.
Examples
use lib_wc::sync::SpinLock;
let spinlock = SpinLock::new(0);
{
let mut guard = spinlock.lock();
*guard += 1;
} // The guard is dropped here, unlocking the mutex
{
let mut guard = spinlock.lock();
*guard += 1;
} // The guard is dropped here, unlocking the mutex
assert_eq!(*spinlock.lock(), 2);
sourcepub fn lock(&self) -> Guard<'_, T>
pub fn lock(&self) -> Guard<'_, T>
Acquires a lock on the spinlock, spinning the current thread in a loop until it is able to do so.
This function returns a Guard
which will release the lock when dropped.
Examples
use lib_wc::sync::SpinLock;
let spinlock = SpinLock::new(0);
{
let mut guard = spinlock.lock();
*guard += 1;
} // The guard is dropped here, unlocking the mutex
{
let mut guard = spinlock.lock();
*guard += 1;
} // The guard is dropped here, unlocking the mutex
assert_eq!(*spinlock.lock(), 2);