riot_wrappers::mutex

Struct Mutex

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

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

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.

Source§

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

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

type ExtractRequestError = <H as Handler>::ExtractRequestError

Source§

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<T> Mutex for &Mutex<T>

Source§

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> !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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.

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.