Mutex

Struct Mutex 

Source
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>

Source

pub fn new(value: T) -> Self

Create a new Mutex with an automatically assigned ID

§Arguments
  • value - The initial value to store in the mutex
§Returns

A new Mutex containing the provided value

§Example
use deloxide::Mutex;

let mutex = Mutex::new(42);
Source

pub fn id(&self) -> LockId

Get the ID of this mutex

§Returns

The unique identifier assigned to this mutex

Source

pub fn creator_thread_id(&self) -> ThreadId

Get the ID of the thread that created this mutex

§Returns

The thread ID of the creator thread

Source

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 scope
Source

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");
}
Source

pub fn into_inner(self) -> T
where 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);
Source

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);

Trait Implementations§

Source§

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

Source§

fn default() -> Mutex<T>

Creates a Mutex<T>, with the Default value for T

Source§

impl<T> Drop for Mutex<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

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

Source§

fn from(t: T) -> Self

Creates a new mutex in an unlocked state ready for use This is equivalent to Mutex::new

Auto Trait Implementations§

§

impl<T> !Freeze for Mutex<T>

§

impl<T> !RefUnwindSafe for Mutex<T>

§

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

§

impl<T> Sync for Mutex<T>
where T: Send,

§

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

§

impl<T> UnwindSafe for Mutex<T>
where T: UnwindSafe,

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V