pub struct Mutex<R, T: ?Sized> { /* private fields */ }
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 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§
source§impl<R: RawMutex, T> Mutex<R, T>
impl<R: RawMutex, T> Mutex<R, T>
sourcepub const fn new(val: T) -> Mutex<R, T>
pub const fn new(val: T) -> Mutex<R, T>
Creates a new mutex in an unlocked state ready for use.
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes this mutex, returning the underlying data.
source§impl<R: RawMutex, T: ?Sized> Mutex<R, T>
impl<R: RawMutex, T: ?Sized> Mutex<R, T>
sourcepub unsafe fn make_guard_unchecked(&self) -> MutexGuard<'_, R, T>
pub unsafe fn make_guard_unchecked(&self) -> MutexGuard<'_, R, T>
Creates a new MutexGuard
without 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
.
sourcepub fn lock(&self) -> MutexGuard<'_, R, T>
pub 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.
sourcepub fn try_lock(&self) -> Option<MutexGuard<'_, R, T>>
pub fn try_lock(&self) -> Option<MutexGuard<'_, R, T>>
Attempts to acquire this lock.
If the lock could not be acquired at this time, then None
is returned.
Otherwise, an RAII guard is returned. The lock will be unlocked when the
guard is dropped.
This function does not block.
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.
sourcepub unsafe fn force_unlock(&self)
pub unsafe fn force_unlock(&self)
Forcibly unlocks the mutex.
This is useful when combined with mem::forget
to hold a lock without
the need to maintain a MutexGuard
object alive, for example when
dealing with FFI.
Safety
This method must only be called if the current thread logically owns a
MutexGuard
but that guard has been discarded using mem::forget
.
Behavior is undefined if a mutex is unlocked when not locked.
sourcepub unsafe fn raw(&self) -> &R
pub unsafe fn raw(&self) -> &R
Returns the underlying raw mutex object.
Note that you will most likely need to import the RawMutex
trait from
lock_api
to 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
.
sourcepub fn data_ptr(&self) -> *mut T
pub fn data_ptr(&self) -> *mut T
Returns a raw pointer to the underlying data.
This is useful when combined with mem::forget
to hold a lock without
the need to maintain a MutexGuard
object 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 MutexGuard
but that guard has been discarded using mem::forget
.
sourcepub fn lock_arc(self: &Arc<Self>) -> ArcMutexGuard<R, T>
pub fn lock_arc(self: &Arc<Self>) -> ArcMutexGuard<R, T>
Acquires a lock through an Arc
.
This method is similar to the lock
method; however, it requires the Mutex
to be inside of an Arc
and the resulting mutex guard has no lifetime requirements.
sourcepub fn try_lock_arc(self: &Arc<Self>) -> Option<ArcMutexGuard<R, T>>
pub fn try_lock_arc(self: &Arc<Self>) -> Option<ArcMutexGuard<R, T>>
Attempts to acquire a lock through an Arc
.
This method is similar to the try_lock
method; however, it requires the Mutex
to be inside of an
Arc
and the resulting mutex guard has no lifetime requirements.
source§impl<R: RawMutexFair, T: ?Sized> Mutex<R, T>
impl<R: RawMutexFair, T: ?Sized> Mutex<R, T>
sourcepub unsafe fn force_unlock_fair(&self)
pub unsafe fn force_unlock_fair(&self)
Forcibly unlocks the mutex using a fair unlock procotol.
This is useful when combined with mem::forget
to hold a lock without
the need to maintain a MutexGuard
object alive, for example when
dealing with FFI.
Safety
This method must only be called if the current thread logically owns a
MutexGuard
but that guard has been discarded using mem::forget
.
Behavior is undefined if a mutex is unlocked when not locked.
source§impl<R: RawMutexTimed, T: ?Sized> Mutex<R, T>
impl<R: RawMutexTimed, T: ?Sized> Mutex<R, T>
sourcepub fn try_lock_for(&self, timeout: R::Duration) -> Option<MutexGuard<'_, R, T>>
pub 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
None
is returned. Otherwise, an RAII guard is returned. The lock will
be unlocked when the guard is dropped.
sourcepub fn try_lock_until(
&self,
timeout: R::Instant
) -> Option<MutexGuard<'_, R, T>>
pub 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
None
is returned. Otherwise, an RAII guard is returned. The lock will
be unlocked when the guard is dropped.
sourcepub fn try_lock_arc_for(
self: &Arc<Self>,
timeout: R::Duration
) -> Option<ArcMutexGuard<R, T>>
pub fn try_lock_arc_for( self: &Arc<Self>, timeout: R::Duration ) -> Option<ArcMutexGuard<R, T>>
Attempts to acquire this lock through an Arc
until a timeout is reached.
This method is similar to the try_lock_for
method; however, it requires the Mutex
to be inside of an
Arc
and the resulting mutex guard has no lifetime requirements.
sourcepub fn try_lock_arc_until(
self: &Arc<Self>,
timeout: R::Instant
) -> Option<ArcMutexGuard<R, T>>
pub fn try_lock_arc_until( self: &Arc<Self>, timeout: R::Instant ) -> Option<ArcMutexGuard<R, T>>
Attempts to acquire this lock through an Arc
until a timeout is reached.
This method is similar to the try_lock_until
method; however, it requires the Mutex
to be inside of
an Arc
and the resulting mutex guard has no lifetime requirements.