Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

core: fix warnings when compiling without std #2022

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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