Skip to content

Commit

Permalink
core: fix warnings when compiling without std (#2022)
Browse files Browse the repository at this point in the history
## 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 <eliza@buoyant.io>
  • Loading branch information
hawkw authored Mar 25, 2022
1 parent 5d33d38 commit df9666b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
17 changes: 13 additions & 4 deletions tracing-core/src/spin/mutex.rs
Original file line number Diff line number Diff line change
@@ -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<T: ?Sized> {
Expand Down Expand Up @@ -37,10 +38,14 @@ impl<T> Mutex<T> {

impl<T: ?Sized> Mutex<T> {
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();
}
}
}
Expand All @@ -60,7 +65,11 @@ impl<T: ?Sized> Mutex<T> {
/// 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<MutexGuard<'_, T>> {
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() },
Expand Down
3 changes: 1 addition & 2 deletions tracing-core/tests/dispatch.rs
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -28,7 +28,6 @@ fn set_default_dispatch() {
});
}

#[cfg(feature = "std")]
#[test]
fn nested_set_default() {
let _guard = set_default(&Dispatch::new(TestSubscriberA));
Expand Down

0 comments on commit df9666b

Please sign in to comment.