pub struct Mutex<R, T: ?Sized> { /* private fields */ }
Expand description
Blocking mutex (not async)
Provides a blocking mutual exclusion primitive backed by an implementation of raw::RawMutex
.
Which implementation you select depends on the context in which you’re using the mutex, and you can choose which kind of interior mutability fits your use case.
Use CriticalSectionMutex
when data can be shared between threads and interrupts.
Use NoopMutex
when data is only shared between tasks running on the same executor.
Use ThreadModeMutex
when data is shared between tasks running on the same executor but you want a global singleton.
In all cases, the blocking mutex is intended to be short lived and not held across await points.
Use the async Mutex
if you need a lock that is held across await points.
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 lock<U>(&self, f: impl FnOnce(&T) -> U) -> U
pub fn lock<U>(&self, f: impl FnOnce(&T) -> U) -> U
Creates a critical section and grants temporary access to the protected data.
Sourcepub unsafe fn lock_mut<U>(&self, f: impl FnOnce(&mut T) -> U) -> U
pub unsafe fn lock_mut<U>(&self, f: impl FnOnce(&mut T) -> U) -> U
Creates a critical section and grants temporary mutable access to the protected data.
§Safety
This method is marked unsafe because calling this method re-entrantly, i.e. within
another lock_mut
or lock
closure, violates Rust’s aliasing rules. Calling this
method at the same time from different tasks is safe. For a safe alternative with
mutable access that never causes UB, use a RefCell
in a Mutex
.
Source§impl<R, T> Mutex<R, T>
impl<R, T> Mutex<R, T>
Sourcepub const fn const_new(raw_mutex: R, val: T) -> Mutex<R, T>
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.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes this mutex, returning the underlying data.
Source§impl<T> Mutex<CriticalSectionRawMutex, T>
impl<T> Mutex<CriticalSectionRawMutex, T>
Sourcepub fn borrow<'cs>(&'cs self, _cs: CriticalSection<'cs>) -> &'cs T
pub fn borrow<'cs>(&'cs self, _cs: CriticalSection<'cs>) -> &'cs T
Borrows the data for the duration of the critical section