pub struct Mutex<T: ?Sized> { /* private fields */ }Expand description
An async mutex for protecting shared data.
See the module level documentation for more.
Implementations§
Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub const fn new(t: T) -> Self
pub const fn new(t: T) -> Self
Creates a new mutex in an unlocked state ready for use.
§Examples
use mea::mutex::Mutex;
let mutex = Mutex::new(5);Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the mutex, returning the underlying data.
§Examples
use mea::mutex::Mutex;
let mutex = Mutex::new(1);
let n = mutex.into_inner();
assert_eq!(n, 1);Source§impl<T: ?Sized> Mutex<T>
impl<T: ?Sized> Mutex<T>
Sourcepub async fn lock(&self) -> MutexGuard<'_, T>
pub async fn lock(&self) -> MutexGuard<'_, T>
Locks this mutex, causing the current task to yield until the lock has been acquired. When
the lock has been acquired, function returns a MutexGuard.
This method is async and will yield the current task if the mutex is currently held by another task. When the mutex becomes available, the task will be woken up and given the lock.
§Cancel safety
This method uses a queue to fairly distribute locks in the order they were requested.
Cancelling a call to lock makes you lose your place in the queue.
§Examples
use mea::mutex::Mutex;
let mutex = Mutex::new(1);
let mut n = mutex.lock().await;
*n = 2;Sourcepub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
Attempts to acquire the lock, and returns None if the lock is currently held somewhere
else.
§Examples
use mea::mutex::Mutex;
let mutex = Mutex::new(1);
let mut guard = mutex.try_lock().expect("mutex is locked");
*guard += 1;
assert_eq!(2, *guard);Sourcepub async fn lock_owned(self: Arc<Self>) -> OwnedMutexGuard<T>
pub async fn lock_owned(self: Arc<Self>) -> OwnedMutexGuard<T>
Locks this mutex, causing the current task to yield until the lock has been acquired. When
the lock has been acquired, this returns an OwnedMutexGuard.
This method is async and will yield the current task if the mutex is currently held by another task. When the mutex becomes available, the task will be woken up and given the lock.
This method is identical to Mutex::lock, except that the returned guard references the
Mutex with an Arc rather than by borrowing it. Therefore, the Mutex must be
wrapped in an Arc to call this method, and the guard will live for the 'static lifetime,
as it keeps the Mutex alive by holding an Arc.
§Cancel safety
This method uses a queue to fairly distribute locks in the order they were requested.
Cancelling a call to lock_owned makes you lose your place in the queue.
§Examples
use std::sync::Arc;
use mea::mutex::Mutex;
let mutex = Arc::new(Mutex::new(1));
let mut n = mutex.clone().lock_owned().await;
*n = 2;Sourcepub fn try_lock_owned(self: Arc<Self>) -> Option<OwnedMutexGuard<T>>
pub fn try_lock_owned(self: Arc<Self>) -> Option<OwnedMutexGuard<T>>
Attempts to acquire the lock, and returns None if the lock is currently held somewhere
else.
This method is identical to Mutex::try_lock, except that the returned guard references
the Mutex with an Arc rather than by borrowing it. Therefore, the Mutex must be
wrapped in an Arc to call this method, and the guard will live for the 'static lifetime,
as it keeps the Mutex alive by holding an Arc.
§Examples
use std::sync::Arc;
use mea::mutex::Mutex;
let mutex = Arc::new(Mutex::new(1));
let mut guard = mutex.clone().try_lock_owned().expect("mutex is locked");
*guard += 1;
assert_eq!(2, *guard);Sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the underlying data.
Since this call borrows the Mutex mutably, no actual locking needs to take place: the
mutable borrow statically guarantees no locks exist.
§Examples
use mea::mutex::Mutex;
let mut mutex = Mutex::new(1);
let n = mutex.get_mut();
*n = 2;