Struct Mutex
pub struct Mutex<R, T: ?Sized> { /* private fields */ }
A mutual exclusion primitive useful for protecting shared data
This mutex will block threads waiting for the lock to become available. The
mutex can also be statically initialized or created via a new
constructor. Each mutex 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 mutex is locked.
Implementations
impl<R, T> Mutex<R, T>
const fn from_raw(raw_mutex: R, val: T) -> Mutex<R, T>Creates a new mutex based on a pre-existing raw mutex.
const fn const_new(raw_mutex: R, val: T) -> Mutex<R, T>Creates a new mutex based on a pre-existing raw mutex.
This allows creating a mutex in a constant context on stable Rust.
This method is a legacy alias for
from_raw.
impl<R: RawMutex, T> Mutex<R, T>
const fn new(val: T) -> Mutex<R, T>Creates a new mutex in an unlocked state ready for use.
fn into_inner(self) -> TConsumes this mutex, returning the underlying data.
impl<R: RawMutex, T: ?Sized> Mutex<R, T>
unsafe fn make_guard_unchecked(&self) -> MutexGuard<'_, R, T>Creates a new
MutexGuardwithout checking if the mutex is locked.Safety
This method must only be called if the thread logically holds the lock.
Calling this function when a guard has already been produced is undefined behaviour unless the guard was forgotten with
mem::forget.fn lock(&self) -> MutexGuard<'_, R, 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 mutex held. An RAII guard is returned to allow scoped unlock of the lock. When the guard goes out of scope, the mutex will be unlocked.
Attempts to lock a mutex in the thread which already holds the lock will result in a deadlock.
fn try_lock(&self) -> Option<MutexGuard<'_, R, T>>Attempts to acquire this lock.
If the lock could not be acquired at this time, then
Noneis returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.This function does not block.
fn get_mut(&mut self) -> &mut TReturns a mutable reference to the underlying data.
Since this call borrows the
Mutexmutably, no actual locking needs to take place---the mutable borrow statically guarantees no locks exist.fn is_locked(&self) -> boolChecks whether the mutex is currently locked.
unsafe fn force_unlock(&self)Forcibly unlocks the mutex.
This is useful when combined with
mem::forgetto hold a lock without the need to maintain aMutexGuardobject alive, for example when dealing with FFI.Safety
This method must only be called if the current thread logically owns a
MutexGuardbut that guard has been discarded usingmem::forget. Behavior is undefined if a mutex is unlocked when not locked.unsafe fn raw(&self) -> &RReturns the underlying raw mutex object.
Note that you will most likely need to import the
RawMutextrait fromlock_apito be able to call functions on the raw mutex.Safety
This method is unsafe because it allows unlocking a mutex while still holding a reference to a
MutexGuard.fn data_ptr(&self) -> *mut TReturns a raw pointer to the underlying data.
This is useful when combined with
mem::forgetto hold a lock without the need to maintain aMutexGuardobject alive, for example when dealing with FFI.Safety
You must ensure that there are no data races when dereferencing the returned pointer, for example if the current thread logically owns a
MutexGuardbut that guard has been discarded usingmem::forget.
impl<R: RawMutexFair, T: ?Sized> Mutex<R, T>
unsafe fn force_unlock_fair(&self)Forcibly unlocks the mutex using a fair unlock protocol.
This is useful when combined with
mem::forgetto hold a lock without the need to maintain aMutexGuardobject alive, for example when dealing with FFI.Safety
This method must only be called if the current thread logically owns a
MutexGuardbut that guard has been discarded usingmem::forget. Behavior is undefined if a mutex is unlocked when not locked.
impl<R: RawMutexTimed, T: ?Sized> Mutex<R, T>
fn try_lock_for(&self, timeout: R::Duration) -> Option<MutexGuard<'_, R, T>>Attempts to acquire this lock until a timeout is reached.
If the lock could not be acquired before the timeout expired, then
Noneis returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.fn try_lock_until(&self, timeout: R::Instant) -> Option<MutexGuard<'_, R, T>>Attempts to acquire this lock until a timeout is reached.
If the lock could not be acquired before the timeout expired, then
Noneis returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.
Trait Implementations
impl<R: RawMutex + Send, T: ?Sized + Send> Send for Mutex<R, T>
impl<R: RawMutex + Sync, T: ?Sized + Send> Sync for Mutex<R, T>
impl<R: RawMutex, T> From<T> for Mutex<R, T>
fn from(t: T) -> Mutex<R, T>
impl<R: RawMutex, T: ?Sized + Debug> Debug for Mutex<R, T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<R: RawMutex, T: ?Sized + Default> Default for Mutex<R, T>
fn default() -> Mutex<R, T>
Auto Trait Implementations
impl<R, T> !Freeze for Mutex<R, T>
impl<R, T> !RefUnwindSafe for Mutex<R, T>
impl<R, T> Unpin for Mutex<R, T>
where
R: Unpin,
UnsafeCell<T>: Unpin,
T: ?Sized,
impl<R, T> UnsafeUnpin for Mutex<R, T>
where
R: UnsafeUnpin,
UnsafeCell<T>: UnsafeUnpin,
T: ?Sized,
impl<R, T> UnwindSafe for Mutex<R, T>
where
R: UnwindSafe,
UnsafeCell<T>: UnwindSafe,
T: ?Sized,
Blanket Implementations
impl<T> Any for Mutex<R, T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Mutex<R, T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Mutex<R, T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for Mutex<R, T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for Mutex<R, T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Mutex<R, T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Mutex<R, T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>