mirror of
https://github.com/tokio-rs/tracing.git
synced 2025-09-28 13:31:52 +00:00
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 <eliza@buoyant.io>
This commit is contained in:
parent
5d33d38021
commit
df9666bdeb
@ -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> {
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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() },
|
||||
|
@ -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));
|
||||
|
Loading…
x
Reference in New Issue
Block a user