pub struct SpinLock<T: ?Sized> { /* private fields */ }
Expand description
A mutual exclusion primitive useful for protecting shared data
This spinlock will block threads waiting for the lock to become available. The
spinlock can be created via a new
constructor. Each spinlock has a type parameter
which represents the data that it is protecting. The data can only be accessed
through the RAII guards returned from lock
and try_lock
, which
guarantees that the data is only ever accessed when the spinlock is locked.
Examples
use alloc::sync::Arc;
use keos::sync::SpinLock;
use keos::thread;
const N: usize = 10;
// Spawn a few threads to increment a shared variable (non-atomically), and
// let the main thread know once all increments are done.
//
// Here we're using an Arc to share memory among threads, and the data inside
// the Arc is protected with a spinlock.
let data = Arc::new(SpinLock::new(0));
for _ in 0..N {
let data = Arc::clone(&data);
thread::ThreadBuilder::new("work").spawn(move || {
// The shared state can only be accessed once the lock is held.
// Our non-atomic increment is safe because we're the only thread
// which can access the shared state when the lock is held.
//
// We unwrap() the return value to assert that we are not expecting
// threads to ever fail while holding the lock.
let mut data = data.lock().unwrap();
*data += 1;
// the lock is unlocked here when `data` goes out of scope.
});
}
It is sometimes necessary to manually drop the spinlock guard to unlock it sooner than the end of the enclosing scope.
use alloc::sync::{Arc, SpinLock};
use keos::thread;
const N: usize = 3;
let data_spinlock = Arc::new(SpinLock::new(vec![1, 2, 3, 4]));
let res_spinlock = Arc::new(SpinLock::new(0));
let mut threads = Vec::with_capacity(N);
(0..N).for_each(|_| {
let data_spinlock_clone = Arc::clone(&data_spinlock);
let res_spinlock_clone = Arc::clone(&res_spinlock);
threads.push(thread::ThreadBuilder::new("work").spawn(move || {
let mut data = data_spinlock_clone.lock().unwrap();
// This is the result of some important and long-ish work.
let result = data.iter().fold(0, |acc, x| acc + x * 2);
data.push(result);
drop(data);
*res_spinlock_clone.lock().unwrap() += result;
}));
});
let mut data = data_spinlock.lock().unwrap();
// This is the result of some important and long-ish work.
let result = data.iter().fold(0, |acc, x| acc + x * 2);
data.push(result);
// We drop the `data` explicitly because it's not necessary anymore and the
// thread still has work to do. This allow other threads to start working on
// the data immediately, without waiting for the rest of the unrelated work
// to be done here.
//
// It's even more important here than in the threads because we `.join` the
// threads after that. If we had not dropped the spinlock guard, a thread could
// be waiting forever for it, causing a deadlock.
drop(data);
// Here the spinlock guard is not assigned to a variable and so, even if the
// scope does not end after this line, the spinlock is still released: there is
// no deadlock.
*res_spinlock.lock().unwrap() += result;
threads.into_iter().for_each(|thread| {
thread
.join()
.expect("The thread creating or execution failed !")
});
assert_eq!(*res_spinlock.lock().unwrap(), 800);
Implementations§
source§impl<T: ?Sized> SpinLock<T>
impl<T: ?Sized> SpinLock<T>
sourcepub fn lock(&self) -> SpinLockGuard<'_, T>
pub fn lock(&self) -> SpinLockGuard<'_, T>
Acquires a spinlock, blocking the current thread until it is able to do so.
This function will block the local thread until it is available to acquire the spinlock. Upon returning, the thread is the only thread with the lock held. An RAII guard is returned to allow scoped unlock of the lock. When the guard goes out of scope, the spinlock will be unlocked.
The exact behavior on locking a spinlock in the thread which already holds the lock is left unspecified. However, this function will not return on the second call (it might panic or deadlock, for example).
Examples
use alloc::sync::Arc;
use keos::sync::SpinLock;
use keos::thread;
let spinlock = Arc::new(SpinLock::new(0));
let c_spinlock = Arc::clone(&spinlock);
thread::spawn(move || {
*c_spinlock.lock().unwrap() = 10;
}).join().expect("thread::spawn failed");
assert_eq!(*spinlock.lock().unwrap(), 10);
sourcepub fn try_lock(&self) -> Result<SpinLockGuard<'_, T>, TryLockError>
pub fn try_lock(&self) -> Result<SpinLockGuard<'_, T>, TryLockError>
Attempts to acquire this lock.
If the lock could not be acquired at this time, then [Err
] is returned.
Otherwise, an RAII guard is returned. The lock will be unlocked when the
guard is dropped.
This function does not block.
Errors
If the spinlock could not be acquired because it is already locked, then
this call will return the WouldBlock
error.
Examples
use keos::sync::SpinLock;
use alloc::sync::Arc;
use keos::thread;
let spinlock = Arc::new(SpinLock::new(0));
let c_spinlock = Arc::clone(&spinlock);
thread::spawn(move || {
let mut lock = c_spinlock.try_lock();
if let Ok(ref mut spinlock) = lock {
**spinlock = 10;
} else {
println!("try_lock failed");
}
}).join().expect("thread::spawn failed");
assert_eq!(*spinlock.lock().unwrap(), 10);
sourcepub fn into_inner(self) -> Twhere
T: Sized,
pub fn into_inner(self) -> Twhere T: Sized,
Consumes this spinlock, returning the underlying data.
Examples
use keos::sync::SpinLock;
let spinlock = SpinLock::new(0);
assert_eq!(spinlock.into_inner().unwrap(), 0);