Minimal parking_lot support

This commit is contained in:
2021-05-27 21:04:49 +02:00
parent b21a63e74b
commit 73b4c8b1af
3 changed files with 64 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ repository = "https://github.com/bertptrs/tracing-mutex"
[dependencies]
lazy_static = "1"
lock_api = { version = "0.4", optional = true }
parking_lot = { version = "0.11", optional = true }
[dev-dependencies]
rand = "0.8"
@@ -21,3 +22,4 @@ rand = "0.8"
[features]
# Feature names do not match crate names pending namespaced features.
lockapi = ["lock_api"]
parkinglot = ["parking_lot", "lockapi"]

View File

@@ -62,12 +62,16 @@ use std::sync::PoisonError;
use lazy_static::lazy_static;
#[cfg(feature = "lockapi")]
pub use lock_api;
#[cfg(feature = "parkinglot")]
pub use parking_lot;
use crate::graph::DiGraph;
mod graph;
#[cfg(feature = "lockapi")]
pub mod lockapi;
#[cfg(feature = "lockapi")]
pub mod parkinglot;
pub mod stdsync;
/// Counter for Mutex IDs. Atomic avoids the need for locking.

58
src/parkinglot.rs Normal file
View 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();
}
}
}