Skip to content

Commit

Permalink
sync: always enable tracing with cfg(loom)
Browse files Browse the repository at this point in the history
Loom depends on `tracing` anyway, so `tracing` is always in the
dependency tree when `cfg(loom)` is set. So, there isn't really any
sense in disabling `tracing` in `maitake-sync` when `cfg(loom)` is set,
even if the tracing feature isn't explicitly enabled.
  • Loading branch information
hawkw committed Aug 10, 2024
1 parent 99ce4e1 commit f370793
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 25 deletions.
1 change: 1 addition & 0 deletions maitake-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 11 additions & 21 deletions maitake-sync/src/blocking/default_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>);
Expand All @@ -178,33 +178,29 @@ mod loom_impl {
#[track_caller]
#[inline]
fn with_lock<R>(&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",
);

let result = f();
drop(guard);

#[cfg(any(feature = "tracing", test))]
tracing::debug!(
debug!(
target: "maitake_sync::blocking",
"DefaultMutex::with_lock() -> unlocked",
);
Expand All @@ -215,40 +211,34 @@ mod loom_impl {
#[track_caller]
#[inline]
fn try_with_lock<R>(&self, f: impl FnOnce() -> R) -> Option<R> {
#[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()",
);

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",
);

let result = f();
drop(guard);

#[cfg(any(feature = "tracing", test))]
tracing::debug!(
debug!(
target: "maitake_sync::blocking",
"DefaultMutex::try_with_lock() -> unlocked",
);

Some(result)
}
Err(_) => {
#[cfg(any(feature = "tracing", test))]
tracing::debug!(
debug!(
target: "maitake_sync::blocking",
%location,
"DefaultMutex::try_with_lock() -> already locked",
Expand Down
2 changes: 1 addition & 1 deletion maitake-sync/src/spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down
4 changes: 2 additions & 2 deletions maitake-sync/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)*) => {};
}
Expand Down
2 changes: 2 additions & 0 deletions maitake-sync/src/util/backoff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion maitake-sync/src/util/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Pointer>(value: T) -> FormatWith<T> {
Expand Down

0 comments on commit f370793

Please sign in to comment.