zephyr::sync

Struct Mutex

source
pub struct Mutex<T: ?Sized> { /* private fields */ }
Expand description

A mutual exclusion primitive useful for protecting shared data.

This mutex will block threads waiting for the lock to become available. This is modeled after std::sync::Mutex, and attempts to implement that API as closely as makes sense on Zephyr. Currently, it has the following differences:

  • Poisoning: This does not yet implement poisoning, as there is no way to recover from panic at this time on Zephyr.
  • Allocation: new is not yet provided, and will be provided once kernel object pools are implemented. Please use new_from which takes a reference to a statically allocated sys::Mutex.

Implementations§

source§

impl<T> Mutex<T>

source

pub const fn new_from(t: T, raw_mutex: Mutex) -> Mutex<T>

Construct a new wrapped Mutex, using the given underlying sys mutex. This is different that std::sync::Mutex in that in Zephyr, objects are frequently allocated statically, and the sys Mutex will be taken by this structure. It is safe to share the underlying Mutex between different items, but without careful use, it is easy to deadlock, so it is not recommended.

source

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

Construct a new Mutex, dynamically allocating the underlying sys Mutex.

source§

impl<T: ?Sized> Mutex<T>

source

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

Acquires a mutex, blocking the current thread until it is able to do so.

This function will block the local thread until it is available to acquire the mutex. Upon returning, the thread is the only thread with the lock held. An RAII guard is returned to allow scoped unlock of the lock. When the guard goes out of scope, the mutex will be unlocked.

In std, an attempt to lock a mutex by a thread that already holds the mutex is unspecified. Zephyr explicitly supports this behavior, by simply incrementing a lock count.

source

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

Attempts to acquire this lock.

If the lock could not be acquired at this time, then Err is returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.

This function does not block.

Trait Implementations§

source§

impl<T> Debug for Mutex<T>

source§

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

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

impl<T: ?Sized + Send> Send for Mutex<T>

source§

impl<T: ?Sized + 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 + ?Sized,

§

impl<T> UnwindSafe for Mutex<T>
where 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<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.