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>

source

pub const fn new(t: T) -> Mutex<T>

Create a new mutex in an unlocked state

source

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.

source

pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>

Get an accessor to the mutex if the mutex is available

source

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

source

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<T: Default> Default for Mutex<T>

source§

fn default() -> Self

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

impl<'b, H> Handler for &'b Mutex<H>
where H: Handler,

Available on riot_module_gcoap only.

Blanket implementation for mutex wrapped resources

This is useful in combination with the defauilt implementation for Option as well.

§

type RequestData = Option<<H as Handler>::RequestData>

Type constructed in extract_request_data() passed on to build_response(). Read more
§

type ExtractRequestError = <H as Handler>::ExtractRequestError

§

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>

Process an incoming request. Read more
source§

fn estimate_length(&mut self, request: &Self::RequestData) -> usize

Estimate an upper bound for the number of bytes in the response Read more
source§

fn build_response<M: MutableWritableMessage>( &mut self, response: &mut M, request: Self::RequestData ) -> Result<(), Self::BuildResponseError<M>>

Build a response for the request Read more
source§

impl<'b, H> Handler for &'b Mutex<H>
where H: Handler,

Available on riot_module_gcoap only.

Blanket implementation for mutex wrapped resources

This is useful in combination with the defauilt implementation for Option as well.

§

type RequestData = Option<<H as Handler>::RequestData>

source§

fn extract_request_data<'a>( &mut self, request: &'a impl ReadableMessage ) -> Self::RequestData

source§

fn estimate_length(&mut self, request: &Self::RequestData) -> usize

source§

fn build_response( &mut self, response: &mut impl MutableWritableMessage, request: Self::RequestData )

source§

impl<T> Mutex for &Mutex<T>

§

type Data = T

Data protected by the mutex.
source§

fn lock<R>(&mut self, f: impl FnOnce(&mut Self::Data) -> R) -> R

Creates a critical section and grants temporary access to the protected data.
source§

impl<T: Send> Send for Mutex<T>

source§

impl<T: Send> Sync for Mutex<T>

Auto Trait Implementations§

§

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> 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<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> IntoSwitch for T

source§

fn into_switch<ActiveLevel>(self) -> Switch<T, ActiveLevel>

Consumes the IoPin returning a Switch of the appropriate ActiveLevel. Read more
source§

fn into_active_low_switch(self) -> Switch<Self, ActiveLow>
where Self: Sized,

Consumes the IoPin returning a Switch<IoPin, ActiveLow>. Read more
source§

fn into_active_high_switch(self) -> Switch<Self, ActiveHigh>
where Self: Sized,

Consumes the IoPin returning a Switch<IoPin, ActiveHigh>. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
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.