pub struct BlockingMutex<R, T: ?Sized> { /* private fields */ }
Expand description
Blocking mutex (not async)
Provides a blocking mutual exclusion primitive backed by an implementation of ScopedRawMutex
.
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 CriticalSectionRawMutex
when data can be shared between threads and interrupts.
Use LocalRawMutex
when data is only shared between tasks running on the same executor.
Use ThreadModeRawMutex
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.
Implementations§
Source§impl<R: ConstInit, T> BlockingMutex<R, T>
impl<R: ConstInit, T> BlockingMutex<R, T>
Sourcepub const fn new(val: T) -> BlockingMutex<R, T>
pub const fn new(val: T) -> BlockingMutex<R, T>
Creates a new mutex in an unlocked state ready for use.
Source§impl<R: ScopedRawMutex, T> BlockingMutex<R, T>
impl<R: ScopedRawMutex, T> BlockingMutex<R, T>
Sourcepub fn with_lock<U>(&self, f: impl FnOnce(&mut T) -> U) -> U
pub fn with_lock<U>(&self, f: impl FnOnce(&mut T) -> U) -> U
Locks the raw mutex and grants temporary access to the inner data
Behavior when the lock is already locked is dependent on the behavior
of the Raw mutex. See ScopedRawMutex::with_lock()
’s documentation for
more details
Sourcepub fn try_with_lock<U>(&self, f: impl FnOnce(&mut T) -> U) -> Option<U>
pub fn try_with_lock<U>(&self, f: impl FnOnce(&mut T) -> U) -> Option<U>
Locks the raw mutex and grants temporary access to the inner data
Returns Some(U)
if the lock was obtained. Returns None
if the lock
was already locked
Source§impl<R, T> BlockingMutex<R, T>
impl<R, T> BlockingMutex<R, T>
Sourcepub const fn const_new(raw_mutex: R, val: T) -> BlockingMutex<R, T>
pub const fn const_new(raw_mutex: R, val: T) -> BlockingMutex<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.
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.