diff --git a/maitake-sync/Cargo.toml b/maitake-sync/Cargo.toml index 66b03f19..eadcdb39 100644 --- a/maitake-sync/Cargo.toml +++ b/maitake-sync/Cargo.toml @@ -42,6 +42,7 @@ tracing = { version = "0.1", default_features = false, optional = true } # the cfg is enabled). [target.'cfg(loom)'.dependencies] loom = { version = "0.7", default_features = false } +tracing = { version = "0.1", default_features = false } [dev-dependencies] futures-util = "0.3" diff --git a/maitake-sync/src/blocking/default_mutex.rs b/maitake-sync/src/blocking/default_mutex.rs index 30dbcbd3..deac18aa 100644 --- a/maitake-sync/src/blocking/default_mutex.rs +++ b/maitake-sync/src/blocking/default_mutex.rs @@ -158,8 +158,8 @@ use spin_impl::SpinDefaultMutex as Inner; #[cfg(loom)] mod loom_impl { use super::ScopedRawMutex; - #[cfg(any(feature = "tracing", test))] use core::panic::Location; + use tracing::{debug, debug_span}; #[derive(Debug)] pub(super) struct LoomDefaultMutex(loom::sync::Mutex<()>); @@ -178,24 +178,21 @@ mod loom_impl { #[track_caller] #[inline] fn with_lock(&self, f: impl FnOnce() -> R) -> R { - #[cfg(any(feature = "tracing", test))] let location = Location::caller(); - #[cfg(any(feature = "tracing", test))] - tracing::trace!( + trace!( target: "maitake_sync::blocking", %location, "DefaultMutex::with_lock()", ); let guard = self.0.lock(); - let _span = tracing::debug_span!( + let _span = debug_span!( target: "maitake_sync::blocking", "locked", %location, ) .entered(); - #[cfg(any(feature = "tracing", test))] - tracing::debug!( + debug!( target: "maitake_sync::blocking", "DefaultMutex::with_lock() -> locked", ); @@ -203,8 +200,7 @@ mod loom_impl { let result = f(); drop(guard); - #[cfg(any(feature = "tracing", test))] - tracing::debug!( + debug!( target: "maitake_sync::blocking", "DefaultMutex::with_lock() -> unlocked", ); @@ -215,10 +211,8 @@ mod loom_impl { #[track_caller] #[inline] fn try_with_lock(&self, f: impl FnOnce() -> R) -> Option { - #[cfg(any(feature = "tracing", test))] let location = Location::caller(); - #[cfg(any(feature = "tracing", test))] - tracing::trace!( + trace!( target: "maitake_sync::blocking", %location, "DefaultMutex::try_with_lock()", @@ -226,11 +220,9 @@ mod loom_impl { match self.0.try_lock() { Ok(guard) => { - let _span = - tracing::debug_span!(target: "maitake_sync::blocking", "locked", %location) - .entered(); - #[cfg(any(feature = "tracing", test))] - tracing::debug!( + let _span = debug_span!(target: "maitake_sync::blocking", "locked", %location) + .entered(); + debug!( target: "maitake_sync::blocking", "DefaultMutex::try_with_lock() -> locked", ); @@ -238,8 +230,7 @@ mod loom_impl { let result = f(); drop(guard); - #[cfg(any(feature = "tracing", test))] - tracing::debug!( + debug!( target: "maitake_sync::blocking", "DefaultMutex::try_with_lock() -> unlocked", ); @@ -247,8 +238,7 @@ mod loom_impl { Some(result) } Err(_) => { - #[cfg(any(feature = "tracing", test))] - tracing::debug!( + debug!( target: "maitake_sync::blocking", %location, "DefaultMutex::try_with_lock() -> already locked", diff --git a/maitake-sync/src/spin.rs b/maitake-sync/src/spin.rs index 20003aa2..6153a683 100644 --- a/maitake-sync/src/spin.rs +++ b/maitake-sync/src/spin.rs @@ -37,7 +37,7 @@ pub mod once; pub use self::once::{InitOnce, Lazy}; use crate::{ - blocking::{self, RawMutex, RawRwLock}, + blocking::{RawMutex, RawRwLock}, loom::sync::atomic::{AtomicBool, AtomicUsize, Ordering::*}, util::{fmt, Backoff}, }; diff --git a/maitake-sync/src/util.rs b/maitake-sync/src/util.rs index 8b442759..bb15dc3a 100644 --- a/maitake-sync/src/util.rs +++ b/maitake-sync/src/util.rs @@ -10,12 +10,12 @@ //! - [`Backoff`]: exponential backoff for spin loops //! - [`CachePadded`]: pads and aligns a value to the size of a cache line -#[cfg(any(test, feature = "tracing"))] +#[cfg(any(test, feature = "tracing", loom))] macro_rules! trace { ($($t:tt)*) => { tracing::trace!($($t)*) } } -#[cfg(not(any(test, feature = "tracing")))] +#[cfg(not(any(test, feature = "tracing", loom)))] macro_rules! trace { ($($t:tt)*) => {}; } diff --git a/maitake-sync/src/util/backoff.rs b/maitake-sync/src/util/backoff.rs index 4a512d92..0208099a 100644 --- a/maitake-sync/src/util/backoff.rs +++ b/maitake-sync/src/util/backoff.rs @@ -51,7 +51,9 @@ impl Backoff { #[inline(always)] pub fn spin(&mut self) { // Issue 2^exp pause instructions. + #[cfg_attr(loom, allow(unused_variables))] let spins = 1 << self.exp; + #[cfg(not(loom))] for _ in 0..spins { crate::loom::hint::spin_loop(); diff --git a/maitake-sync/src/util/fmt.rs b/maitake-sync/src/util/fmt.rs index 59a7bffb..1b0fcf2a 100644 --- a/maitake-sync/src/util/fmt.rs +++ b/maitake-sync/src/util/fmt.rs @@ -19,7 +19,7 @@ pub(crate) struct FmtOption<'a, T> { // === impl FormatWith === -#[cfg(any(test, feature = "tracing"))] +#[cfg(any(test, feature = "tracing", loom))] #[inline] #[must_use] pub(crate) fn ptr(value: T) -> FormatWith {