pub struct Mutex<T> { /* private fields */ }Expand description
A wrapper around a mutex that tracks lock operations for deadlock detection
The Mutex provides the same interface as a standard mutex but adds deadlock detection by tracking lock acquisition and release operations. It’s a drop-in replacement for std::sync::Mutex that enables deadlock detection.
§Example
use deloxide::Mutex;
use std::sync::Arc;
use std::thread;
// Initialize detectors (not shown here)
// Create a tracked mutex
let mutex = Arc::new(Mutex::new(42));
let mutex_clone = Arc::clone(&mutex);
// Use it just like a regular mutex
thread::spawn(move || {
let mut data = mutex.lock();
*data += 1;
});
// In another thread
let mut data = mutex_clone.lock();
*data += 10;Implementations§
Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub fn creator_thread_id(&self) -> ThreadId
pub fn creator_thread_id(&self) -> ThreadId
Sourcepub fn lock(&self) -> MutexGuard<'_, T>
pub fn lock(&self) -> MutexGuard<'_, T>
Acquire the lock, blocking if necessary
Uses atomic deadlock detection to prevent race conditions.
Uses the Optimistic Fast Path: attempts to acquire the lock cheaply first. Only interacts with the global deadlock detector if the lock is contented.
§Example
use deloxide::Mutex;
let mutex = Mutex::new(42);
{
let guard = mutex.lock();
assert_eq!(*guard, 42);
} // lock is automatically released when guard goes out of scopeSourcepub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
Try to acquire the lock without blocking
Returns Some(guard) if successful, None if the lock is held.
§Example
use deloxide::Mutex;
let mutex = Mutex::new(42);
// Non-blocking attempt to acquire the lock
if let Some(guard) = mutex.try_lock() {
// Lock was acquired
assert_eq!(*guard, 42);
} else {
// Lock was already held by another thread
println!("Lock already held by another thread");
}Sourcepub fn into_inner(self) -> Twhere
T: Sized,
pub fn into_inner(self) -> Twhere
T: Sized,
Consumes this mutex, returning the underlying data
§Example
use deloxide::Mutex;
let mutex = Mutex::new(42);
let value = mutex.into_inner();
assert_eq!(value, 42);Sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the underlying data
Since this call borrows the Mutex mutably, no actual locking needs to take place – the mutable borrow statically guarantees no locks exist.
§Example
use deloxide::Mutex;
let mut mutex = Mutex::new(0);
*mutex.get_mut() = 10;
assert_eq!(*mutex.lock(), 10);