Struct riot_wrappers::mutex::Mutex
source · pub struct Mutex<T> { /* private fields */ }
Expand description
A mutual exclusion primitive useful for protecting shared data
Unlike the std::sync::Mutex, this has no concept of poisoning, so waiting for mutexes in paniced (and thus locked) threads will lock the accessing thread as well. This is because RIOT threds don’t unwind Rust code. As a consequence, the mutex interface is different from the standard library’s.
Several methods (into_inner, get_mut) are not implemented until they’re actually needed.
Implementations§
source§impl<T> Mutex<T>
impl<T> Mutex<T>
sourcepub fn lock(&self) -> MutexGuard<'_, T>
pub fn lock(&self) -> MutexGuard<'_, T>
Get an accessor to the mutex when the mutex is available
Panics
This function checks at runtime whether it is called in a thread context, and panics
otherwise. Consider promoting a reference with an InThread
token’s .promote(&my_mutex)
to gain access to a
better .lock()
method, which suffers
neither the panic condition nor the runtime overhead.
sourcepub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
Get an accessor to the mutex if the mutex is available
sourcepub fn try_leak(&'static self) -> Option<&'static mut T>
pub fn try_leak(&'static self) -> Option<&'static mut T>
Lock the mutex and throw away the key
Try to lock the mutex (returning None if it is locked). When successful, a mutable reference for the complete lifetime of the mutex is produced, without the usual mechanisms that’d free the mutex later.
This is an easy way to get a &’static mut refence in RIOT. Its downsides (compared to cortex-m-rt’s entry mechanisms) are:
- It has runtime storage cost (one mutex_t)
- It has runtime processing cost (primarily the accompanying unwrap which the compiler can’t know to optimze out)
- It needs a good default value (can be mitigated with MaybeUninit)
but then again, it’s easy.
API rationale
This requires access to the original mutex and not just an acquired guard that’d be leaked in the process: The latter could also be done on a more short-lived mutex, which would then be dropped (or even leaked-and-pushed-off-the-stack) even in a locked state. (A possibility that is fine – we sure don’t want to limit mutex usage to require a Pin reference.)
The function could be generalized to some generic lifetime, but there doesn’t seem to be a point to it.
source§impl<'a, T> ValueInThread<&'a Mutex<T>>
impl<'a, T> ValueInThread<&'a Mutex<T>>
sourcepub fn lock(self) -> MutexGuard<'a, T>
pub fn lock(self) -> MutexGuard<'a, T>
Get an accessor to the mutex when the mutex is available
Through the crate::thread::ValueInThread, this is already guaranteed to run in a thread context, so no additional check is performed.
Trait Implementations§
source§impl<'b, H> Handler for &'b Mutex<H>where
H: Handler,
Available on crate feature with_coap_handler
only.
impl<'b, H> Handler for &'b Mutex<H>where H: Handler,
with_coap_handler
only.Blanket implementation for mutex wrapped resources
This is useful in combination with the defauilt implementation for Option as well.