pub struct Mutex<T> {
t: UnsafeCell<T>,
waiters: SpinLock<VecDeque<ParkHandle>>,
}Expand description
A mutual exclusion primitive useful for protecting shared data
This mutex will block threads waiting for the lock to become available.
The mutex 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 guards returned from lock and
try_lock, which guarantees that the data is only ever accessed when the
mutex is locked.
§Examples
use alloc::sync::Arc;
use keos::sync::Mutex;
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 mutex.
let data = Arc::new(Mutex::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 must be "explicitly" unlocked.
data.unlock();
});
}Fields§
§t: UnsafeCell<T>§waiters: SpinLock<VecDeque<ParkHandle>>Implementations§
Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub fn lock(&self) -> MutexGuard<'_, T>
pub fn lock(&self) -> MutexGuard<'_, T>
Acquires a mutex, 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 mutex. Upon returning, the thread is the only thread with the lock held. An guard is returned to allow scoped unlock of the lock. When the guard goes out of scope, the mutex will be unlocked.
The exact behavior on locking a mutex 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::Mutex;
use keos::thread;
let mutex = Arc::new(Mutex::new(0));
let c_mutex = Arc::clone(&spinlock);
thread::spawn(move || {
*c_mutex.lock().unwrap() = 10;
}).join().expect("thread::spawn failed");
assert_eq!(*mutex.lock().unwrap(), 10);Sourcepub fn try_lock(&self) -> Result<MutexGuard<'_, T>, WouldBlock>
pub fn try_lock(&self) -> Result<MutexGuard<'_, T>, WouldBlock>
Attempts to acquire this lock.
If the lock could not be acquired at this time, then [Err] is
returned. Otherwise, an guard is returned.
This function does not block.
§Errors
If the mutex could not be acquired because it is already locked, then
this call will return the WouldBlock error.
§Examples
use keos::sync::Mutex;
use alloc::sync::Arc;
use keos::thread;
let mutex = Arc::new(Mutex::new(0));
let c_mutex = Arc::clone(&spinlock);
thread::spawn(move || {
let mut lock = c_mutex.try_lock();
if let Ok(ref mut mutex) = lock {
**mutex = 10;
} else {
println!("try_lock failed");
}
}).join().expect("thread::spawn failed");
assert_eq!(*mutex.lock().unwrap(), 10);Sourcepub fn into_inner(self) -> Twhere
T: Sized,
pub fn into_inner(self) -> Twhere
T: Sized,
Consumes this mutex, returning the underlying data.
§Examples
use keos::sync::Mutex;
let mutex = Mutex::new(0);
assert_eq!(mutex.into_inner().unwrap(), 0);