diff --git a/tracing-core/src/spin/mutex.rs b/tracing-core/src/spin/mutex.rs index 383a2cd839..c261a61910 100644 --- a/tracing-core/src/spin/mutex.rs +++ b/tracing-core/src/spin/mutex.rs @@ -1,10 +1,11 @@ use core::cell::UnsafeCell; use core::default::Default; use core::fmt; +use core::hint; use core::marker::Sync; use core::ops::{Deref, DerefMut, Drop}; use core::option::Option::{self, None, Some}; -use core::sync::atomic::{spin_loop_hint as cpu_relax, AtomicBool, Ordering}; +use core::sync::atomic::{AtomicBool, Ordering}; /// This type provides MUTual EXclusion based on spinning. pub(crate) struct Mutex { @@ -37,10 +38,14 @@ impl Mutex { impl Mutex { fn obtain_lock(&self) { - while self.lock.compare_and_swap(false, true, Ordering::Acquire) != false { + while self + .lock + .compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed) + .is_err() + { // Wait until the lock looks unlocked before retrying while self.lock.load(Ordering::Relaxed) { - cpu_relax(); + hint::spin_loop(); } } } @@ -60,7 +65,11 @@ impl Mutex { /// Tries to lock the mutex. If it is already locked, it will return None. Otherwise it returns /// a guard within Some. pub(crate) fn try_lock(&self) -> Option> { - if self.lock.compare_and_swap(false, true, Ordering::Acquire) == false { + if self + .lock + .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) + .is_ok() + { Some(MutexGuard { lock: &self.lock, data: unsafe { &mut *self.data.get() }, diff --git a/tracing-core/tests/dispatch.rs b/tracing-core/tests/dispatch.rs index 7184fc9f33..3820692a86 100644 --- a/tracing-core/tests/dispatch.rs +++ b/tracing-core/tests/dispatch.rs @@ -1,9 +1,9 @@ +#![cfg(feature = "std")] mod common; use common::*; use tracing_core::dispatcher::*; -#[cfg(feature = "std")] #[test] fn set_default_dispatch() { set_global_default(Dispatch::new(TestSubscriberA)).expect("global dispatch set failed"); @@ -28,7 +28,6 @@ fn set_default_dispatch() { }); } -#[cfg(feature = "std")] #[test] fn nested_set_default() { let _guard = set_default(&Dispatch::new(TestSubscriberA));