mirror of
https://github.com/bertptrs/tracing-mutex.git
synced 2025-12-27 13:30:32 +01:00
Create type aliases for parking_lot::RwLock
This commit is contained in:
@@ -21,6 +21,7 @@ debug_variant!(
|
|||||||
parking_lot::RawFairMutex
|
parking_lot::RawFairMutex
|
||||||
);
|
);
|
||||||
debug_variant!(DebugRawMutex, TracingRawMutex, parking_lot::RawMutex);
|
debug_variant!(DebugRawMutex, TracingRawMutex, parking_lot::RawMutex);
|
||||||
|
debug_variant!(DebugRawRwLock, TracingRawRwLock, parking_lot::RawRwLock);
|
||||||
|
|
||||||
/// Dependency tracking fair mutex. See: [`parking_lot::FairMutex`].
|
/// Dependency tracking fair mutex. See: [`parking_lot::FairMutex`].
|
||||||
pub type TracingFairMutex<T> = lock_api::Mutex<TracingRawFairMutex, T>;
|
pub type TracingFairMutex<T> = lock_api::Mutex<TracingRawFairMutex, T>;
|
||||||
@@ -47,6 +48,10 @@ pub type DebugMutex<T> = lock_api::Mutex<DebugRawMutex, T>;
|
|||||||
pub type DebugMutexGuard<'a, T> = lock_api::MutexGuard<'a, DebugRawMutex, T>;
|
pub type DebugMutexGuard<'a, T> = lock_api::MutexGuard<'a, DebugRawMutex, T>;
|
||||||
|
|
||||||
/// Dependency tracking reentrant mutex. See: [`parking_lot::ReentrantMutex`].
|
/// Dependency tracking reentrant mutex. See: [`parking_lot::ReentrantMutex`].
|
||||||
|
///
|
||||||
|
/// **Note:** due to the way dependencies are tracked, this mutex can only be acquired directly
|
||||||
|
/// after itself. Acquiring any other mutex in between introduces a dependency cycle, and will
|
||||||
|
/// therefore be rejected.
|
||||||
pub type TracingReentrantMutex<T> =
|
pub type TracingReentrantMutex<T> =
|
||||||
lock_api::ReentrantMutex<TracingWrapper<parking_lot::RawMutex>, parking_lot::RawThreadId, T>;
|
lock_api::ReentrantMutex<TracingWrapper<parking_lot::RawMutex>, parking_lot::RawThreadId, T>;
|
||||||
/// Mutex guard for [`TracingReentrantMutex`].
|
/// Mutex guard for [`TracingReentrantMutex`].
|
||||||
@@ -67,6 +72,23 @@ pub type DebugReentrantMutex<T> =
|
|||||||
pub type DebugReentrantMutexGuard<'a, T> =
|
pub type DebugReentrantMutexGuard<'a, T> =
|
||||||
lock_api::ReentrantMutexGuard<'a, DebugRawMutex, parking_lot::RawThreadId, T>;
|
lock_api::ReentrantMutexGuard<'a, DebugRawMutex, parking_lot::RawThreadId, T>;
|
||||||
|
|
||||||
|
/// Dependency tracking RwLock. See: [`parking_lot::RwLock`].
|
||||||
|
pub type TracingRwLock<T> = lock_api::RwLock<TracingRawRwLock, T>;
|
||||||
|
/// Read guard for [`TracingRwLock`].
|
||||||
|
pub type TracingRwLockReadGuard<'a, T> = lock_api::RwLockReadGuard<'a, TracingRawRwLock, T>;
|
||||||
|
/// Write guard for [`TracingRwLock`].
|
||||||
|
pub type TracingRwLockWriteGuard<'a, T> = lock_api::RwLockWriteGuard<'a, TracingRawRwLock, T>;
|
||||||
|
|
||||||
|
/// Debug-only dependency tracking RwLock.
|
||||||
|
///
|
||||||
|
/// If debug assertions are enabled this resolved to [`TracingRwLock`] and to
|
||||||
|
/// [`parking_lot::RwLock`] otherwise.
|
||||||
|
pub type DebugRwLock<T> = lock_api::RwLock<DebugRawRwLock, T>;
|
||||||
|
/// Read guard for [`TracingRwLock`].
|
||||||
|
pub type DebugRwLockReadGuard<'a, T> = lock_api::RwLockReadGuard<'a, DebugRawRwLock, T>;
|
||||||
|
/// Write guard for [`TracingRwLock`].
|
||||||
|
pub type DebugRwLockWriteGuard<'a, T> = lock_api::RwLockWriteGuard<'a, DebugRawRwLock, T>;
|
||||||
|
|
||||||
/// A dependency-tracking wrapper for [`parking_lot::Once`].
|
/// A dependency-tracking wrapper for [`parking_lot::Once`].
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct TracingOnce {
|
pub struct TracingOnce {
|
||||||
@@ -154,6 +176,21 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rwlock_usage() {
|
||||||
|
let lock = Arc::new(TracingRwLock::new(()));
|
||||||
|
let lock2 = Arc::clone(&lock);
|
||||||
|
|
||||||
|
let _read_lock = lock.read();
|
||||||
|
|
||||||
|
// Should be able to acquire lock in the background
|
||||||
|
thread::spawn(move || {
|
||||||
|
let _read_lock = lock2.read();
|
||||||
|
})
|
||||||
|
.join()
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_once_usage() {
|
fn test_once_usage() {
|
||||||
let once = Arc::new(TracingOnce::new());
|
let once = Arc::new(TracingOnce::new());
|
||||||
|
|||||||
Reference in New Issue
Block a user