pub struct Mutex<T> {
mutex: UnsafeCell<mutex_t>,
data: UnsafeCell<T>,
}
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 panicked (and thus locked) threads will lock the accessing thread as well. This is because RIOT threads 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.
Fields§
§mutex: UnsafeCell<mutex_t>
§data: UnsafeCell<T>
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 reference 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 optimize 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 riot_module_gcoap
only.
impl<'b, H> Handler for &'b Mutex<H>where
H: Handler,
riot_module_gcoap
only.Blanket implementation for mutex wrapped resources
This is useful in combination with the defauilt implementation for Option as well.
Source§type RequestData = Option<<H as Handler>::RequestData>
type RequestData = Option<<H as Handler>::RequestData>
Source§type ExtractRequestError = <H as Handler>::ExtractRequestError
type ExtractRequestError = <H as Handler>::ExtractRequestError
extract_request_data()
. Read moreSource§type BuildResponseError<M: MinimalWritableMessage> = <H as Handler>::BuildResponseError<M>
type BuildResponseError<M: MinimalWritableMessage> = <H as Handler>::BuildResponseError<M>
Source§fn extract_request_data<M: ReadableMessage>(
&mut self,
request: &M,
) -> Result<Self::RequestData, Self::ExtractRequestError>
fn extract_request_data<M: ReadableMessage>( &mut self, request: &M, ) -> Result<Self::RequestData, Self::ExtractRequestError>
Source§fn estimate_length(&mut self, request: &Self::RequestData) -> usize
fn estimate_length(&mut self, request: &Self::RequestData) -> usize
Source§fn build_response<M: MutableWritableMessage>(
&mut self,
response: &mut M,
request: Self::RequestData,
) -> Result<(), Self::BuildResponseError<M>>
fn build_response<M: MutableWritableMessage>( &mut self, response: &mut M, request: Self::RequestData, ) -> Result<(), Self::BuildResponseError<M>>
impl<T: Send> Send for Mutex<T>
impl<T: Send> Sync for Mutex<T>
Auto Trait Implementations§
impl<T> !Freeze for Mutex<T>
impl<T> !RefUnwindSafe for Mutex<T>
impl<T> Unpin for Mutex<T>where
T: Unpin,
impl<T> UnwindSafe for Mutex<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoSwitch for T
impl<T> IntoSwitch for T
Source§fn into_switch<ActiveLevel>(self) -> Switch<T, ActiveLevel>
fn into_switch<ActiveLevel>(self) -> Switch<T, ActiveLevel>
Source§fn into_active_high_switch(self) -> Switch<Self, ActiveHigh>where
Self: Sized,
fn into_active_high_switch(self) -> Switch<Self, ActiveHigh>where
Self: Sized,
Layout§
Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.