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>
impl<T> ThreadCheckedMutex<T>
Source§impl<T: ?Sized> ThreadCheckedMutex<T>
impl<T: ?Sized> ThreadCheckedMutex<T>
Sourcepub fn lock(&self) -> LockResult<ThreadCheckedMutexGuard<'_, T>>
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.
Sourcepub fn try_lock(&self) -> TryLockResult<ThreadCheckedMutexGuard<'_, T>>
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.
Sourcepub fn locked_by_current_thread(&self) -> bool
pub fn locked_by_current_thread(&self) -> bool
Determines whether this mutex is currently held by the current thread.
Sourcepub fn is_poisoned(&self) -> bool
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.
Sourcepub fn clear_poison(&self)
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).
Sourcepub fn into_inner(self) -> AccessResult<T>where
T: Sized,
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.
Sourcepub fn get_mut(&mut self) -> AccessResult<&mut T>
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.