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) -> T
where 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, T> Debug for Mutex<M, T>
where M: RawMutex, T: ?Sized + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

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

source§

fn default() -> Self

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

impl<M: RawMutex, T> From<T> for Mutex<M, T>

source§

fn from(from: T) -> Self

Converts to this type from the input type.
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> !Freeze for Mutex<M, T>

§

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

§

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

§

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

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<!> for T

§

fn from(t: !) -> T

Converts to this type from the input type.
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.