embassy-sync

Crates

git

Versions

default

Flavors

embassy_sync::mutex

Struct 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: 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§

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

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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, 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.