Struct ThreadCheckedMutex

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

A variant of std::sync::Mutex which gracefully returns an error when a thread attempts to acquire a ThreadCheckedMutex that it already holds.

In such a situation, Mutex::lock is guaranteed to either lock or panic, while Mutex::try_lock checks if any thread holds the lock (and cannot distinguish whether the current thread holds the lock). As such, attempting to lock the same Mutex twice on a thread is potentially a fatal error; ThreadCheckedMutex allows for recovery.

Implementations§

Source§

impl<T> ThreadCheckedMutex<T>

Source

pub fn new(t: T) -> Self

Creates a new mutex in an unlocked state.

Source§

impl<T: ?Sized> ThreadCheckedMutex<T>

Source

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

Attempts to acquire this mutex, blocking the current thread while the mutex is locked in other threads.

If the mutex is acquired (either completely successfully or with a poison error), a ThreadCheckedMutexGuard is returned. Only one thread at a time can hold the lock; at most one ThreadCheckedMutexGuard can exist at a time (across any thread); and the mutex is unlocked when the returned guard is dropped.

§Errors

If the mutex was already held by the current thread when this call was made, then a LockedByCurrentThread error is returned.

If another user of this mutex panicked while holding the mutex, then this call will still acquire the mutex but wrap the returned guard in a poison error. See the HandlePoisonResult trait for methods to ignore poison errors and treat them as successful, or to panic if a poison error was returned.

Source

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

Attempts to acquire this mutex without blocking.

If the mutex is acquired (either completely successfully or with a poison error), a ThreadCheckedMutexGuard is returned. Only one thread at a time can hold the lock; at most one ThreadCheckedMutexGuard can exist at a time (across any thread); and the mutex is unlocked when the returned guard is dropped.

§Errors

If the mutex was already held by the current thread when this call was made, then a LockedByCurrentThread error is returned. If the mutex was held by a different thread, then a WouldBlock error is returned.

If another user of this mutex panicked while holding the mutex, then this call will still acquire the mutex but wrap the returned guard in a poison error. See the HandlePoisonResult trait for methods to ignore poison errors and treat them as successful, or to panic if a poison error was returned.

Source

pub fn locked_by_current_thread(&self) -> bool

Determines whether this mutex is currently held by the current thread.

Source

pub fn is_poisoned(&self) -> bool

Determines whether this mutex is currently poisoned.

If another thread is active, the mutex could become poisoned or have its poison cleared at any time; as such, the return value of this function should generally not be depended on for program correctness.

Read more about poison.

Source

pub fn clear_poison(&self)

Clear any poison from this mutex.

When a ThreadCheckedMutexGuard is dropped in a thread which is panicking, its associated mutex becomes poisoned, and remains poisoned until this function is called (by any thread).

Read more about poison.

Source

pub fn into_inner(self) -> AccessResult<T>
where T: Sized,

Consumes this mutex and returns the underlying data.

§Errors

If another user of this mutex panicked while holding the mutex, then the inner data is still returned, but wrapped in a poison error.

Read more about poison.

Source

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

Returns a mutable reference to the underlying data, without locking.

§Errors

If another user of this mutex panicked while holding the mutex, then a mutable reference is still returned, but wrapped in a poison error.

Read more about poison.

Trait Implementations§

Source§

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

Source§

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

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

impl<T: Default> Default for ThreadCheckedMutex<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !Freeze for ThreadCheckedMutex<T>

§

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

§

impl<T> Send for ThreadCheckedMutex<T>
where T: Send + ?Sized,

§

impl<T> Sync for ThreadCheckedMutex<T>
where T: Send + ?Sized,

§

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

§

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