pub struct Mutex<T> { /* private fields */ }Expand description
A tracked mutex that automatically records contention metrics and integrates with deadlock detection.
This is a drop-in replacement for tokio::sync::Mutex with additional
observability features.
§Example
use async_inspect::sync::Mutex;
#[tokio::main]
async fn main() {
let mutex = Mutex::new(0, "counter");
// Spawn multiple tasks that contend for the lock
let mutex = std::sync::Arc::new(mutex);
let mut handles = vec![];
for i in 0..10 {
let m = mutex.clone();
handles.push(tokio::spawn(async move {
let mut guard = m.lock().await;
*guard += 1;
}));
}
for h in handles {
h.await.unwrap();
}
// Check contention metrics
let metrics = mutex.metrics();
println!("Total acquisitions: {}", metrics.acquisitions);
println!("Contentions: {}", metrics.contentions);
println!("Contention rate: {:.1}%", metrics.contention_rate() * 100.0);
}Implementations§
Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub async fn lock(&self) -> MutexGuard<'_, T>
pub async fn lock(&self) -> MutexGuard<'_, T>
Acquire the lock, blocking until it’s available.
This method automatically:
- Records wait time if there’s contention
- Notifies the deadlock detector
- Tracks acquisition metrics
§Example
use async_inspect::sync::Mutex;
let mutex = Mutex::new(42, "my_value");
let guard = mutex.lock().await;
println!("Value: {}", *guard);Sourcepub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
Try to acquire the lock immediately.
Returns None if the lock is already held.
§Example
use async_inspect::sync::Mutex;
let mutex = Mutex::new(42, "my_value");
let result = mutex.try_lock();
if let Some(guard) = result {
println!("Got the lock: {}", *guard);
} else {
println!("Lock is held by another task");
}Sourcepub fn metrics(&self) -> LockMetrics
pub fn metrics(&self) -> LockMetrics
Get the current contention metrics for this mutex.
§Example
use async_inspect::sync::Mutex;
let mutex = Mutex::new(42, "my_value");
// ... some operations ...
let metrics = mutex.metrics();
println!("Acquisitions: {}", metrics.acquisitions);
println!("Contention rate: {:.1}%", metrics.contention_rate() * 100.0);Sourcepub fn reset_metrics(&self)
pub fn reset_metrics(&self)
Reset the contention metrics.
Sourcepub fn resource_id(&self) -> ResourceId
pub fn resource_id(&self) -> ResourceId
Get the resource ID for deadlock detection.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consume the mutex and return the inner value.
§Panics
This method will panic if the mutex is poisoned.
Trait Implementations§
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>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more