Skip to main content

try_mutex_lock_with_timeout

Function try_mutex_lock_with_timeout 

Source
pub async fn try_mutex_lock_with_timeout<'a, T>(
    name: impl Into<String>,
    lock: &'a Arc<Mutex<T>>,
    timeout: Duration,
) -> Result<(CheckResult, MutexGuard<'a, T>), CheckResult>
Expand description

Acquire a tokio::sync::Mutex lock or fail with a deadlock-suspected verdict.

On success, returns Ok((CheckResult::pass, MutexGuard)). On timeout, returns Err(CheckResult::fail) and the lock is left alone.

ยงExample

use dev_async::deadlock::try_mutex_lock_with_timeout;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;

let m = Arc::new(Mutex::new(0));
match try_mutex_lock_with_timeout("counter", &m, Duration::from_millis(50)).await {
    Ok((check, mut guard)) => {
        *guard += 1;
        drop(guard);
        assert!(check.has_tag("async"));
    }
    Err(check) => {
        assert!(check.has_tag("deadlock_suspected"));
    }
};