mirror of
https://github.com/bertptrs/tracing-mutex.git
synced 2025-12-27 21:40:32 +01:00
Minimal parking_lot support
This commit is contained in:
@@ -14,6 +14,7 @@ repository = "https://github.com/bertptrs/tracing-mutex"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
lazy_static = "1"
|
lazy_static = "1"
|
||||||
lock_api = { version = "0.4", optional = true }
|
lock_api = { version = "0.4", optional = true }
|
||||||
|
parking_lot = { version = "0.11", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
@@ -21,3 +22,4 @@ rand = "0.8"
|
|||||||
[features]
|
[features]
|
||||||
# Feature names do not match crate names pending namespaced features.
|
# Feature names do not match crate names pending namespaced features.
|
||||||
lockapi = ["lock_api"]
|
lockapi = ["lock_api"]
|
||||||
|
parkinglot = ["parking_lot", "lockapi"]
|
||||||
|
|||||||
@@ -62,12 +62,16 @@ use std::sync::PoisonError;
|
|||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
#[cfg(feature = "lockapi")]
|
#[cfg(feature = "lockapi")]
|
||||||
pub use lock_api;
|
pub use lock_api;
|
||||||
|
#[cfg(feature = "parkinglot")]
|
||||||
|
pub use parking_lot;
|
||||||
|
|
||||||
use crate::graph::DiGraph;
|
use crate::graph::DiGraph;
|
||||||
|
|
||||||
mod graph;
|
mod graph;
|
||||||
#[cfg(feature = "lockapi")]
|
#[cfg(feature = "lockapi")]
|
||||||
pub mod lockapi;
|
pub mod lockapi;
|
||||||
|
#[cfg(feature = "lockapi")]
|
||||||
|
pub mod parkinglot;
|
||||||
pub mod stdsync;
|
pub mod stdsync;
|
||||||
|
|
||||||
/// Counter for Mutex IDs. Atomic avoids the need for locking.
|
/// Counter for Mutex IDs. Atomic avoids the need for locking.
|
||||||
|
|||||||
58
src/parkinglot.rs
Normal file
58
src/parkinglot.rs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
use crate::lockapi::TracingWrapper;
|
||||||
|
|
||||||
|
macro_rules! create_mutex_wrapper {
|
||||||
|
($wrapped:ty, $tracing_name:ident, $debug_name:ident) => {
|
||||||
|
pub type $tracing_name<T> = lock_api::Mutex<TracingWrapper<$wrapped>, T>;
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
pub type $debug_name<T> = $tracing_name<T>;
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
pub type $debug_name<T> = lock_api::Mutex<$wrapped, T>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
create_mutex_wrapper!(parking_lot::RawFairMutex, TracingFairMutex, DebugFairMutex);
|
||||||
|
create_mutex_wrapper!(parking_lot::RawMutex, TracingMutex, DebugMutex);
|
||||||
|
|
||||||
|
pub type TracingReentrantMutex<T> =
|
||||||
|
lock_api::ReentrantMutex<TracingWrapper<parking_lot::RawMutex>, parking_lot::RawThreadId, T>;
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
pub type DebugReentrantMutex<T> = TracingReentrantMutex<T>;
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
pub type DebugReentrantMutex<T> = parking_lot::ReentrantMutex<T>;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_mutex_usage() {
|
||||||
|
let mutex = Arc::new(TracingMutex::new(()));
|
||||||
|
let local_lock = mutex.lock();
|
||||||
|
drop(local_lock);
|
||||||
|
|
||||||
|
thread::spawn(move || {
|
||||||
|
let _remote_lock = mutex.lock();
|
||||||
|
})
|
||||||
|
.join()
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn test_mutex_conflict() {
|
||||||
|
let mutexes = [
|
||||||
|
TracingMutex::new(()),
|
||||||
|
TracingMutex::new(()),
|
||||||
|
TracingMutex::new(()),
|
||||||
|
];
|
||||||
|
|
||||||
|
for i in 0..3 {
|
||||||
|
let _first_lock = mutexes[i].lock();
|
||||||
|
let _second_lock = mutexes[(i + 1) % 3].lock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user