pub struct Mutex<T: ?Sized>(/* private fields */);Expand description
Custom synchronization primitive for managing shared mutable state.
This custom mutex implementation builds on std::sync::Mutex to enhance usability and safety
in concurrent environments. It provides ergonomic methods to safely access and modify inner
values while reducing the risk of deadlocks and panics. It is used throughout SRI applications
to managed shared state across multiple threads, such as tracking active mining sessions,
routing jobs, and managing connections safely and efficiently.
§Advantages
- Closure-Based Locking: The
safe_lockmethod encapsulates the locking process, ensuring the lock is automatically released after the closure completes. - Error Handling:
safe_lockenforces explicit handling of potentialPoisonErrorconditions, reducing the risk of panics caused by poisoned locks. - Panic-Safe Option: The
super_safe_lockmethod provides an alternative that unwraps the result ofsafe_lock, with optional runtime safeguards against panics. - Extensibility: Includes feature-gated functionality to customize behavior, such as
stricter runtime checks using external tools like
no-panic.
Implementations§
Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub fn safe_lock<F, Ret>(
&self,
thunk: F,
) -> Result<Ret, PoisonError<MutexGuard<'_, T>>>
pub fn safe_lock<F, Ret>( &self, thunk: F, ) -> Result<Ret, PoisonError<MutexGuard<'_, T>>>
Mutex safe lock.
Safely locks the Mutex and executes a closer (thunk) with a mutable reference to the
inner value. This ensures that the lock is automatically released after the closure
completes, preventing deadlocks. It explicitly returns a PoisonError containing a
MutexGuard to the inner value in cases where the lock is poisoned.
To prevent poison lock errors, unwraps should never be used within the closure. The result should always be returned and handled outside of the sage lock.
Sourcepub fn super_safe_lock<F, Ret>(&self, thunk: F) -> Ret
pub fn super_safe_lock<F, Ret>(&self, thunk: F) -> Ret
Mutex super safe lock.
Locks the Mutex and executes a closure (thunk) with a mutable reference to the inner
value, panicking if the lock is poisoned.
This is a convenience wrapper around safe_lock for cases where explicit error handling is
unnecessary or undesirable. Use with caution in production code.
Sourcepub fn to_remove(
&self,
) -> Result<MutexGuard<'_, T>, PoisonError<MutexGuard<'_, T>>>
pub fn to_remove( &self, ) -> Result<MutexGuard<'_, T>, PoisonError<MutexGuard<'_, T>>>
Removes lock for direct access.
Acquires a lock on the Mutex and returns a MutexGuard for direct access to the
inner value. Allows for manual lock handling and is useful in scenarios where closures are
not convenient.