Skip to main content

Mutex

Struct Mutex 

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

A mutual exclusion lock that this module’s condvars can wait on.

It wraps std’s mutex, keeping its locking and poisoning.

Implementations§

Source§

impl<T> Mutex<T>

Source

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

Available on not (loom).

Creates an unlocked mutex holding value.

Source

pub fn into_inner(self) -> LockResult<T>

Consumes the mutex and returns its value, inside an error if the mutex is poisoned.

Source§

impl<T: ?Sized> Mutex<T>

Source

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

Blocks until the mutex is free, then locks it.

If a thread panicked while holding the mutex, the guard comes back inside an error.

Source

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

Locks the mutex if it is free, without blocking.

Fails with WouldBlock while the mutex is locked. If a thread panicked while holding the mutex, the guard comes back inside Poisoned.

Source

pub fn is_poisoned(&self) -> bool

Available on not (loom).

Reports whether a thread panicked while holding the mutex.

Source

pub fn clear_poison(&self)

Available on not (loom).

Clears the poisoned state, marking the value as recovered.

Source

pub fn get_mut(&mut self) -> LockResult<&mut T>

Borrows the value mutably, inside an error if the mutex is poisoned.

The mutable borrow of the mutex rules out other users, so this takes no lock.

Trait Implementations§

Source§

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

Source§

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

Shows the value if the mutex is free, never blocking, as std does.

Source§

impl<T: Default> Default for Mutex<T>

Source§

fn default() -> Self

Creates an unlocked mutex holding the value type’s default.

Source§

impl<T> From<T> for Mutex<T>

Source§

fn from(value: T) -> Self

Creates an unlocked mutex holding value.

Auto Trait Implementations§

§

impl<T> !Freeze for Mutex<T>

§

impl<T> RefUnwindSafe for Mutex<T>
where Mutex<T>: RefUnwindSafe, T: ?Sized,

§

impl<T> Send for Mutex<T>
where Mutex<T>: Send, T: ?Sized,

§

impl<T> Sync for Mutex<T>
where Mutex<T>: Sync, T: ?Sized,

§

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

§

impl<T> UnsafeUnpin for Mutex<T>
where Mutex<T>: UnsafeUnpin, T: ?Sized,

§

impl<T> UnwindSafe for Mutex<T>
where Mutex<T>: UnwindSafe, T: ?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 = !

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

fn try_from(value: U) -> Result<T, !>

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.