Struct embassy_sync::mutex::Mutex

source ·
pub struct Mutex<M, T>where
    M: RawMutex,
    T: ?Sized,{ /* private fields */ }
Expand description

Async mutex.

The mutex is generic over a blocking RawMutex. The raw mutex is used to guard access to the internal “is locked” flag. It is held for very short periods only, while locking and unlocking. It is not held for the entire time the async Mutex is locked.

Which implementation you select depends on the context in which you’re using the mutex.

Use CriticalSectionRawMutex when data can be shared between threads and interrupts.

Use NoopRawMutex 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 singleton.

Implementations§

source§

impl<M, T> Mutex<M, T>where M: RawMutex,

Async mutex.

source

pub const fn new(value: T) -> Self

Create a new mutex with the given value.

source§

impl<M, T> Mutex<M, T>where M: RawMutex, T: ?Sized,

source

pub async fn lock(&self) -> MutexGuard<'_, M, T>

Lock the mutex.

This will wait for the mutex to be unlocked if it’s already locked.

source

pub fn try_lock(&self) -> Result<MutexGuard<'_, M, T>, TryLockError>

Attempt to immediately lock the mutex.

If the mutex is already locked, this will return an error instead of waiting.

source

pub fn into_inner(self) -> Twhere T: Sized,

Consumes this mutex, returning the underlying data.

source

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.

Trait Implementations§

source§

impl<M: RawMutex + Send, T: ?Sized + Send> Send for Mutex<M, T>

source§

impl<M: RawMutex + Sync, T: ?Sized + Send> Sync for Mutex<M, T>

Auto Trait Implementations§

§

impl<M, T> !RefUnwindSafe for Mutex<M, T>

§

impl<M, T: ?Sized> Unpin for Mutex<M, T>where M: Unpin, T: Unpin,

§

impl<M, T: ?Sized> UnwindSafe for Mutex<M, T>where M: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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, U> TryFrom<U> for Twhere 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 Twhere 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.