Restructure parking_lot wrappers

This commit is contained in:
2022-08-27 09:56:04 +02:00
parent 6e5516eaa7
commit 56b0604448
2 changed files with 118 additions and 178 deletions

View File

@@ -7,7 +7,7 @@ use criterion::BenchmarkId;
use criterion::Criterion; use criterion::Criterion;
use criterion::Throughput; use criterion::Throughput;
use rand::prelude::*; use rand::prelude::*;
use tracing_mutex::stdsync::TracingMutex; use tracing_mutex::stdsync::tracing::Mutex as TracingMutex;
const SAMPLE_SIZES: [usize; 5] = [10, 30, 100, 300, 1000]; const SAMPLE_SIZES: [usize; 5] = [10, 30, 100, 300, 1000];

View File

@@ -1,19 +1,20 @@
//! Wrapper types and type aliases for tracing [`parking_lot`] mutexes. //! Wrapper types and type aliases for tracing [`parking_lot`] mutexes.
//! //!
//! This module provides type aliases that use the [`lockapi`][crate::lockapi] module to provide //! This module provides type aliases that use the [`lockapi`][crate::lockapi] module to provide
//! tracing variants of the `parking_lot` primitives. Each of the `TracingX` type aliases wraps an //! tracing variants of the `parking_lot` primitives. The [`tracing`] module contains type aliases
//! `X` in the `parkint_lot` api with dependency tracking, and a `DebugX` will refer to a `TracingX` //! that use dependency tracking, while the main `parking_lot` primitives are reexported as [`raw`].
//! when `debug_assertions` are enabled and to `X` when they're not. This can be used to aid //!
//! debugging in development while enjoying maximum performance in production. //! This main module imports from [`tracing`] when `debug_assertions` are enabled, and from [`raw`]
//! when they're not. Note that primitives for which no tracing wrapper exists are not imported into
//! the main module.
//! //!
//! # Usage //! # Usage
//! //!
//! ``` //! ```
//! # use std::sync::Arc; //! # use std::sync::Arc;
//! # use std::thread; //! # use std::thread;
//! # use lock_api::Mutex; //! use tracing_mutex::parkinglot::Mutex;
//! # use tracing_mutex::parkinglot::TracingMutex; //! let mutex = Arc::new(Mutex::new(0));
//! let mutex = Arc::new(TracingMutex::new(0));
//! //!
//! let handles: Vec<_> = (0..10).map(|_| { //! let handles: Vec<_> = (0..10).map(|_| {
//! let mutex = Arc::clone(&mutex); //! let mutex = Arc::clone(&mutex);
@@ -37,191 +38,130 @@
//! In addition, the mutex guards returned by the tracing wrappers are `!Send`, regardless of //! In addition, the mutex guards returned by the tracing wrappers are `!Send`, regardless of
//! whether `parking_lot` is configured to have `Send` mutex guards. This is a limitation of the //! whether `parking_lot` is configured to have `Send` mutex guards. This is a limitation of the
//! current bookkeeping system. //! current bookkeeping system.
use parking_lot::Once;
use parking_lot::OnceState;
use crate::lockapi::TracingWrapper; pub use parking_lot as raw;
use crate::LazyMutexId;
macro_rules! debug_variant { #[cfg(debug_assertions)]
($debug_name:ident, $tracing_name:ident, $normal_name:ty) => { pub use tracing::{
type $tracing_name = TracingWrapper<$normal_name>; FairMutex, FairMutexGuard, MappedFairMutexGuard, MappedMutexGuard, MappedReentrantMutexGuard,
MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, MutexGuard, Once, OnceState,
ReentrantMutex, ReentrantMutexGuard, RwLock, RwLockReadGuard, RwLockUpgradableReadGuard,
RwLockWriteGuard,
};
#[cfg(debug_assertions)] #[cfg(not(debug_assertions))]
type $debug_name = TracingWrapper<$normal_name>; pub use parking_lot::{
#[cfg(not(debug_assertions))] FairMutex, FairMutexGuard, MappedFairMutexGuard, MappedMutexGuard, MappedReentrantMutexGuard,
type $debug_name = $normal_name; MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, MutexGuard, Once, OnceState,
}; ReentrantMutex, ReentrantMutexGuard, RwLock, RwLockReadGuard, RwLockUpgradableReadGuard,
} RwLockWriteGuard,
};
debug_variant!( /// Dependency tracing wrappers for [`parking_lot`].
DebugRawFairMutex, pub mod tracing {
TracingRawFairMutex, pub use parking_lot::OnceState;
parking_lot::RawFairMutex
);
debug_variant!(DebugRawMutex, TracingRawMutex, parking_lot::RawMutex);
debug_variant!(DebugRawRwLock, TracingRawRwLock, parking_lot::RawRwLock);
/// Dependency tracking fair mutex. See: [`parking_lot::FairMutex`]. use crate::lockapi::TracingWrapper;
pub type TracingFairMutex<T> = lock_api::Mutex<TracingRawFairMutex, T>; use crate::LazyMutexId;
/// Mutex guard for [`TracingFairMutex`].
pub type TracingFairMutexGuard<'a, T> = lock_api::MutexGuard<'a, TracingRawFairMutex, T>;
/// RAII guard for `TracingFairMutexGuard::map`.
pub type TracingMappedFairMutexGuard<'a, T> =
lock_api::MappedMutexGuard<'a, TracingRawFairMutex, T>;
/// Debug-only dependency tracking fair mutex.
///
/// If debug assertions are enabled this resolves to [`TracingFairMutex`] and to
/// [`parking_lot::FairMutex`] otherwise.
pub type DebugFairMutex<T> = lock_api::Mutex<DebugRawFairMutex, T>;
/// Mutex guard for [`DebugFairMutex`].
pub type DebugFairMutexGuard<'a, T> = lock_api::MutexGuard<'a, DebugRawFairMutex, T>;
/// RAII guard for `DebugFairMutexGuard::map`.
pub type DebugMappedFairMutexGuard<'a, T> = lock_api::MappedMutexGuard<'a, DebugRawFairMutex, T>;
/// Dependency tracking mutex. See: [`parking_lot::Mutex`]. type RawFairMutex = TracingWrapper<parking_lot::RawFairMutex>;
pub type TracingMutex<T> = lock_api::Mutex<TracingRawMutex, T>; type RawMutex = TracingWrapper<parking_lot::RawMutex>;
/// Mutex guard for [`TracingMutex`]. type RawRwLock = TracingWrapper<parking_lot::RawRwLock>;
pub type TracingMutexGuard<'a, T> = lock_api::MutexGuard<'a, TracingRawMutex, T>;
/// RAII guard for `TracingMutexGuard::map`.
pub type TracingMappedMutexGuard<'a, T> = lock_api::MappedMutexGuard<'a, TracingRawMutex, T>;
/// Debug-only dependency tracking mutex.
///
/// If debug assertions are enabled this resolves to [`TracingMutex`] and to [`parking_lot::Mutex`]
/// otherwise.
pub type DebugMutex<T> = lock_api::Mutex<DebugRawMutex, T>;
/// Mutex guard for [`DebugMutex`].
pub type DebugMutexGuard<'a, T> = lock_api::MutexGuard<'a, DebugRawMutex, T>;
/// RAII guard for `TracingMutexGuard::map`.
pub type DebugMappedMutexGuard<'a, T> = lock_api::MappedMutexGuard<'a, DebugRawMutex, T>;
/// Dependency tracking reentrant mutex. See: [`parking_lot::ReentrantMutex`]. /// Dependency tracking fair mutex. See: [`parking_lot::FairMutex`].
/// pub type FairMutex<T> = lock_api::Mutex<RawFairMutex, T>;
/// **Note:** due to the way dependencies are tracked, this mutex can only be acquired directly /// Mutex guard for [`FairMutex`].
/// after itself. Acquiring any other mutex in between introduces a dependency cycle, and will pub type FairMutexGuard<'a, T> = lock_api::MutexGuard<'a, RawFairMutex, T>;
/// therefore be rejected. /// RAII guard for [`FairMutexGuard::map`].
pub type TracingReentrantMutex<T> = pub type MappedFairMutexGuard<'a, T> = lock_api::MappedMutexGuard<'a, RawFairMutex, T>;
lock_api::ReentrantMutex<TracingWrapper<parking_lot::RawMutex>, parking_lot::RawThreadId, T>;
/// Mutex guard for [`TracingReentrantMutex`].
pub type TracingReentrantMutexGuard<'a, T> = lock_api::ReentrantMutexGuard<
'a,
TracingWrapper<parking_lot::RawMutex>,
parking_lot::RawThreadId,
T,
>;
/// RAII guard for `TracingReentrantMutexGuard::map`.
pub type TracingMappedReentrantMutexGuard<'a, T> =
lock_api::MappedReentrantMutexGuard<'a, TracingRawMutex, parking_lot::RawThreadId, T>;
/// Debug-only dependency tracking reentrant mutex. /// Dependency tracking mutex. See: [`parking_lot::Mutex`].
/// pub type Mutex<T> = lock_api::Mutex<RawMutex, T>;
/// If debug assertions are enabled this resolves to [`TracingReentrantMutex`] and to /// Mutex guard for [`Mutex`].
/// [`parking_lot::ReentrantMutex`] otherwise. pub type MutexGuard<'a, T> = lock_api::MutexGuard<'a, RawMutex, T>;
pub type DebugReentrantMutex<T> = /// RAII guard for [`MutexGuard::map`].
lock_api::ReentrantMutex<DebugRawMutex, parking_lot::RawThreadId, T>; pub type MappedMutexGuard<'a, T> = lock_api::MappedMutexGuard<'a, RawMutex, T>;
/// Mutex guard for [`DebugReentrantMutex`].
pub type DebugReentrantMutexGuard<'a, T> =
lock_api::ReentrantMutexGuard<'a, DebugRawMutex, parking_lot::RawThreadId, T>;
/// RAII guard for `DebugReentrantMutexGuard::map`.
pub type DebugMappedReentrantMutexGuard<'a, T> =
lock_api::MappedReentrantMutexGuard<'a, DebugRawMutex, parking_lot::RawThreadId, T>;
/// Dependency tracking RwLock. See: [`parking_lot::RwLock`]. /// Dependency tracking reentrant mutex. See: [`parking_lot::ReentrantMutex`].
pub type TracingRwLock<T> = lock_api::RwLock<TracingRawRwLock, T>; ///
/// Read guard for [`TracingRwLock`]. /// **Note:** due to the way dependencies are tracked, this mutex can only be acquired directly
pub type TracingRwLockReadGuard<'a, T> = lock_api::RwLockReadGuard<'a, TracingRawRwLock, T>; /// after itself. Acquiring any other mutex in between introduces a dependency cycle, and will
/// Upgradable Read guard for [`TracingRwLock`]. /// therefore be rejected.
pub type TracingRwLockUpgradableReadGuard<'a, T> = pub type ReentrantMutex<T> = lock_api::ReentrantMutex<RawMutex, parking_lot::RawThreadId, T>;
lock_api::RwLockUpgradableReadGuard<'a, TracingRawRwLock, T>; /// Mutex guard for [`ReentrantMutex`].
/// Write guard for [`TracingRwLock`]. pub type ReentrantMutexGuard<'a, T> =
pub type TracingRwLockWriteGuard<'a, T> = lock_api::RwLockWriteGuard<'a, TracingRawRwLock, T>; lock_api::ReentrantMutexGuard<'a, RawMutex, parking_lot::RawThreadId, T>;
/// RAII guard for `TracingRwLockReadGuard::map`. /// RAII guard for `ReentrantMutexGuard::map`.
pub type TracingMappedRwLockReadGuard<'a, T> = pub type MappedReentrantMutexGuard<'a, T> =
lock_api::MappedRwLockReadGuard<'a, TracingRawRwLock, T>; lock_api::MappedReentrantMutexGuard<'a, RawMutex, parking_lot::RawThreadId, T>;
/// RAII guard for `TracingRwLockWriteGuard::map`.
pub type TracingMappedRwLockWriteGuard<'a, T> =
lock_api::MappedRwLockWriteGuard<'a, TracingRawRwLock, T>;
/// Debug-only dependency tracking RwLock. /// Dependency tracking RwLock. See: [`parking_lot::RwLock`].
/// pub type RwLock<T> = lock_api::RwLock<RawRwLock, T>;
/// If debug assertions are enabled this resolved to [`TracingRwLock`] and to /// Read guard for [`RwLock`].
/// [`parking_lot::RwLock`] otherwise. pub type RwLockReadGuard<'a, T> = lock_api::RwLockReadGuard<'a, RawRwLock, T>;
pub type DebugRwLock<T> = lock_api::RwLock<DebugRawRwLock, T>; /// Upgradable Read guard for [`RwLock`].
/// Read guard for [`TracingRwLock`]. pub type RwLockUpgradableReadGuard<'a, T> =
pub type DebugRwLockReadGuard<'a, T> = lock_api::RwLockReadGuard<'a, DebugRawRwLock, T>; lock_api::RwLockUpgradableReadGuard<'a, RawRwLock, T>;
/// Upgradable Read guard for [`TracingRwLock`]. /// Write guard for [`RwLock`].
pub type DebugRwLockUpgradableReadGuard<'a, T> = pub type RwLockWriteGuard<'a, T> = lock_api::RwLockWriteGuard<'a, RawRwLock, T>;
lock_api::RwLockUpgradableReadGuard<'a, DebugRawRwLock, T>; /// RAII guard for `RwLockReadGuard::map`.
/// Write guard for [`TracingRwLock`]. pub type MappedRwLockReadGuard<'a, T> = lock_api::MappedRwLockReadGuard<'a, RawRwLock, T>;
pub type DebugRwLockWriteGuard<'a, T> = lock_api::RwLockWriteGuard<'a, DebugRawRwLock, T>; /// RAII guard for `RwLockWriteGuard::map`.
/// RAII guard for `DebugRwLockReadGuard::map`. pub type MappedRwLockWriteGuard<'a, T> = lock_api::MappedRwLockWriteGuard<'a, RawRwLock, T>;
pub type DebugMappedRwLockReadGuard<'a, T> = lock_api::MappedRwLockReadGuard<'a, DebugRawRwLock, T>;
/// RAII guard for `DebugRwLockWriteGuard::map`.
pub type DebugMappedRwLockWriteGuard<'a, T> =
lock_api::MappedRwLockWriteGuard<'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 Once {
inner: Once, inner: parking_lot::Once,
id: LazyMutexId, id: LazyMutexId,
} }
impl TracingOnce { impl Once {
/// Create a new `TracingOnce` value. /// Create a new `Once` value.
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
inner: Once::new(), inner: parking_lot::Once::new(),
id: LazyMutexId::new(), id: LazyMutexId::new(),
}
}
/// Returns the current state of this `Once`.
pub fn state(&self) -> OnceState {
self.inner.state()
}
/// This call is considered as "locking this `Once`" and it participates in dependency
/// tracking as such.
///
/// # Panics
///
/// This method will panic if `f` panics, poisoning this `Once`. In addition, this function
/// panics when the lock acquisition order is determined to be inconsistent.
pub fn call_once(&self, f: impl FnOnce()) {
let _borrow = self.id.get_borrowed();
self.inner.call_once(f);
}
/// Performs the given initialization routine once and only once.
///
/// This method is identical to [`Once::call_once`] except it ignores poisoning.
pub fn call_once_force(&self, f: impl FnOnce(OnceState)) {
let _borrow = self.id.get_borrowed();
self.inner.call_once_force(f);
} }
} }
/// Returns the current state of this `Once`.
pub fn state(&self) -> OnceState {
self.inner.state()
}
///
/// This call is considered as "locking this `TracingOnce`" and it participates in dependency
/// tracking as such.
///
/// # Panics
///
/// This method will panic if `f` panics, poisoning this `Once`. In addition, this function
/// panics when the lock acquisition order is determined to be inconsistent.
pub fn call_once(&self, f: impl FnOnce()) {
let _borrow = self.id.get_borrowed();
self.inner.call_once(f);
}
/// Performs the given initialization routine once and only once.
///
/// This method is identical to [`TracingOnce::call_once`] except it ignores poisoning.
pub fn call_once_force(&self, f: impl FnOnce(OnceState)) {
let _borrow = self.id.get_borrowed();
self.inner.call_once_force(f);
}
} }
/// Debug-only `Once`.
///
/// If debug assertions are enabled this resolves to [`TracingOnce`] and to [`parking_lot::Once`]
/// otherwise.
#[cfg(debug_assertions)]
pub type DebugOnce = TracingOnce;
#[cfg(not(debug_assertions))]
pub type DebugOnce = Once;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::sync::Arc; use std::sync::Arc;
use std::thread; use std::thread;
use super::*; use super::tracing;
#[test] #[test]
fn test_mutex_usage() { fn test_mutex_usage() {
let mutex = Arc::new(TracingMutex::new(())); let mutex = Arc::new(tracing::Mutex::new(()));
let local_lock = mutex.lock(); let local_lock = mutex.lock();
drop(local_lock); drop(local_lock);
@@ -236,9 +176,9 @@ mod tests {
#[should_panic] #[should_panic]
fn test_mutex_conflict() { fn test_mutex_conflict() {
let mutexes = [ let mutexes = [
TracingMutex::new(()), tracing::Mutex::new(()),
TracingMutex::new(()), tracing::Mutex::new(()),
TracingMutex::new(()), tracing::Mutex::new(()),
]; ];
for i in 0..3 { for i in 0..3 {
@@ -249,7 +189,7 @@ mod tests {
#[test] #[test]
fn test_rwlock_usage() { fn test_rwlock_usage() {
let lock = Arc::new(TracingRwLock::new(())); let lock = Arc::new(tracing::RwLock::new(()));
let lock2 = Arc::clone(&lock); let lock2 = Arc::clone(&lock);
let _read_lock = lock.read(); let _read_lock = lock.read();
@@ -264,19 +204,19 @@ mod tests {
#[test] #[test]
fn test_rwlock_upgradable_read_usage() { fn test_rwlock_upgradable_read_usage() {
let lock = TracingRwLock::new(()); let lock = tracing::RwLock::new(());
// Should be able to acquire an upgradable read lock. // Should be able to acquire an upgradable read lock.
let upgradable_guard: TracingRwLockUpgradableReadGuard<'_, _> = lock.upgradable_read(); let upgradable_guard: tracing::RwLockUpgradableReadGuard<'_, _> = lock.upgradable_read();
// Should be able to upgrade the guard. // Should be able to upgrade the guard.
let _write_guard: TracingRwLockWriteGuard<'_, _> = let _write_guard: tracing::RwLockWriteGuard<'_, _> =
TracingRwLockUpgradableReadGuard::upgrade(upgradable_guard); tracing::RwLockUpgradableReadGuard::upgrade(upgradable_guard);
} }
#[test] #[test]
fn test_once_usage() { fn test_once_usage() {
let once = Arc::new(TracingOnce::new()); let once = Arc::new(tracing::Once::new());
let once_clone = once.clone(); let once_clone = once.clone();
assert!(!once_clone.state().done()); assert!(!once_clone.state().done());