From df9666bdeb8da3e120af15e3c86f4655cb6b29de Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 24 Mar 2022 17:27:20 -0700 Subject: [PATCH] core: fix warnings when compiling without `std` (#2022) ## Motivation Currently, compiling `tracing-core` with `default-features = false` (i.e. for `no_std` targets) emits a few warnings. This is due to the spinlock implementation's use of the deprecated `atomic::spin_loop_hint` function (renamed to `hint::spin_loop`), and the use of deprecated `compare_and_swap` instead of `compare_exchange` methods. Now that our MSRV is 1.49 (the version in which `hint::spin_loop` was stabilized), we can fix these warnings. ## Solution This branch replaces the deprecated APIs. Also, I noticed that one of the tests emits unused-imports warnings with `--no-default-features`. This is because the actual tests are feature flagged to require `std`, but the module itself doesn't, so the imports are just hanging out and not getting used for anything. I went ahead and fixed that as well. Signed-off-by: Eliza Weisman --- tracing-core/src/spin/mutex.rs | 17 +++++++++++++---- tracing-core/tests/dispatch.rs | 3 +-- 2 files changed, 14 insertions(+), 6 deletions(-) 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));