From 77cd603363f3a1da358307f11e342ac076b7575f Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 27 May 2021 22:00:37 +0200 Subject: [PATCH] Implement minimal mutexes for parking_lot. --- src/parkinglot.rs | 64 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/src/parkinglot.rs b/src/parkinglot.rs index c02e0ea..25b258c 100644 --- a/src/parkinglot.rs +++ b/src/parkinglot.rs @@ -1,25 +1,67 @@ use crate::lockapi::TracingWrapper; -macro_rules! create_mutex_wrapper { - ($wrapped:ty, $tracing_name:ident, $debug_name:ident) => { - pub type $tracing_name = lock_api::Mutex, T>; +macro_rules! debug_variant { + ($debug_name:ident, $tracing_name:ident, $normal_name:ty) => { + type $tracing_name = TracingWrapper<$normal_name>; #[cfg(debug_assertions)] - pub type $debug_name = $tracing_name; + type $debug_name = TracingWrapper<$normal_name>; #[cfg(not(debug_assertions))] - pub type $debug_name = lock_api::Mutex<$wrapped, T>; + type $debug_name = $normal_name; }; } -create_mutex_wrapper!(parking_lot::RawFairMutex, TracingFairMutex, DebugFairMutex); -create_mutex_wrapper!(parking_lot::RawMutex, TracingMutex, DebugMutex); +debug_variant!( + DebugRawFairMutex, + TracingRawFairMutex, + parking_lot::RawFairMutex +); +debug_variant!(DebugRawMutex, TracingRawMutex, parking_lot::RawMutex); +/// Dependency tracking fair mutex. See: [`parking_lot::FairMutex`]. +pub type TracingFairMutex = lock_api::Mutex; +/// Mutex guard for [`TracingFairMutex`]. +pub type TracingFairMutexGuard<'a, T> = lock_api::MutexGuard<'a, TracingRawFairMutex, T>; +/// Debug-only dependency tracking fair mutex. +/// +/// If debug assertions are enabled this resolves to [`TracingFairMutex`] and to +/// [`parking_lot::FairMutex`] if it's not. +pub type DebugFairMutex = lock_api::Mutex; +/// Mutex guard for [`DebugFairMutex`]. +pub type DebugFairMutexGuard<'a, T> = lock_api::MutexGuard<'a, DebugRawFairMutex, T>; + +/// Dependency tracking mutex. See: [`parking_lot::Mutex`]. +pub type TracingMutex = lock_api::Mutex; +/// Mutex guard for [`TracingMutex`]. +pub type TracingMutexGuard<'a, T> = lock_api::MutexGuard<'a, TracingRawMutex, T>; +/// Debug-only dependency tracking mutex. +/// +/// If debug assertions are enabled this resolves to [`TracingMutex`] and to [`parking_lot::Mutex`] +/// if it's not. +pub type DebugMutex = lock_api::Mutex; +/// Mutex guard for [`DebugMutex`]. +pub type DebugMutexGuard<'a, T> = lock_api::MutexGuard<'a, DebugRawMutex, T>; + +/// Dependency tracking reentrant mutex. See: [`parking_lot::ReentrantMutex`]. pub type TracingReentrantMutex = lock_api::ReentrantMutex, parking_lot::RawThreadId, T>; -#[cfg(debug_assertions)] -pub type DebugReentrantMutex = TracingReentrantMutex; -#[cfg(not(debug_assertions))] -pub type DebugReentrantMutex = parking_lot::ReentrantMutex; +/// Mutex guard for [`TracingReentrantMutex`]. +pub type TracingReentrantMutexGuard<'a, T> = lock_api::ReentrantMutexGuard< + 'a, + TracingWrapper, + parking_lot::RawThreadId, + T, +>; + +/// Debug-only dependency tracking reentrant mutex. +/// +/// If debug assertions are enabled this resolves to [`TracingReentrantMutex`] and to +/// [`parking_lot::ReentrantMutex`] if it's not. +pub type DebugReentrantMutex = + lock_api::ReentrantMutex; +/// Mutex guard for [`DebugReentrantMutex`]. +pub type DebugReentrantMutexGuard<'a, T> = + lock_api::ReentrantMutexGuard<'a, DebugRawMutex, parking_lot::RawThreadId, T>; #[cfg(test)] mod tests {