Struct lock_api::Mutex

source ·
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>

source

pub const fn new(val: T) -> Mutex<R, T>

Creates a new mutex in an unlocked state ready for use.

source

pub fn into_inner(self) -> T

Consumes this mutex, returning the underlying data.

source§

impl<R, T> Mutex<R, T>

source

pub 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.

source§

impl<R: RawMutex, T: ?Sized> Mutex<R, T>

source

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.

source

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.

source

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.

source

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.

source

pub fn is_locked(&self) -> bool

Checks whether the mutex is currently locked.

source

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.

source

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.

source

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.

source§

impl<R: RawMutexFair, T: ?Sized> Mutex<R, T>

source

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>

source

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.

source

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.

Trait Implementations§

source§

impl<R: RawMutex, T: ?Sized + Debug> Debug for Mutex<R, T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<R: RawMutex, T: ?Sized + Default> Default for Mutex<R, T>

source§

fn default() -> Mutex<R, T>

Returns the “default value” for a type. Read more
source§

impl<R: RawMutex, T> From<T> for Mutex<R, T>

source§

fn from(t: T) -> Mutex<R, T>

Converts to this type from the input type.
source§

impl<R: RawMutex + Send, T: ?Sized + Send> Send for Mutex<R, T>

source§

impl<R: RawMutex + Sync, T: ?Sized + Send> Sync for 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, T: Unpin + ?Sized,

§

impl<R, T> UnwindSafe for Mutex<R, T>
where R: UnwindSafe, T: UnwindSafe + ?Sized,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.