From 507b672660288f0223edb6353d34f8733fa0a2f4 Mon Sep 17 00:00:00 2001 From: KodrAus Date: Tue, 25 Jun 2024 10:58:09 +1000 Subject: [PATCH] loosen orderings for logger initialization --- src/lib.rs | 25 +++++++++---------------- tests/Cargo.toml | 3 +++ 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 19f44188c..72f825d1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -341,10 +341,6 @@ #![warn(missing_docs)] #![deny(missing_debug_implementations, unconditional_recursion)] #![cfg_attr(all(not(feature = "std"), not(test)), no_std)] -// When compiled for the rustc compiler itself we want to make sure that this is -// an unstable crate -#![cfg_attr(rustbuild, feature(staged_api, rustc_private))] -#![cfg_attr(rustbuild, unstable(feature = "rustc_private", issue = "27812"))] #[cfg(any( all(feature = "max_level_off", feature = "max_level_error"), @@ -1405,24 +1401,21 @@ fn set_logger_inner(make_logger: F) -> Result<(), SetLoggerError> where F: FnOnce() -> &'static dyn Log, { - let old_state = match STATE.compare_exchange( + match STATE.compare_exchange( UNINITIALIZED, INITIALIZING, - Ordering::SeqCst, - Ordering::SeqCst, + Ordering::Acquire, + Ordering::Relaxed, ) { - Ok(s) | Err(s) => s, - }; - match old_state { - UNINITIALIZED => { + Ok(UNINITIALIZED) => { unsafe { LOGGER = make_logger(); } - STATE.store(INITIALIZED, Ordering::SeqCst); + STATE.store(INITIALIZED, Ordering::Release); Ok(()) } - INITIALIZING => { - while STATE.load(Ordering::SeqCst) == INITIALIZING { + Err(INITIALIZING) => { + while STATE.load(Ordering::Relaxed) == INITIALIZING { std::hint::spin_loop(); } Err(SetLoggerError(())) @@ -1451,10 +1444,10 @@ where /// /// [`set_logger`]: fn.set_logger.html pub unsafe fn set_logger_racy(logger: &'static dyn Log) -> Result<(), SetLoggerError> { - match STATE.load(Ordering::SeqCst) { + match STATE.load(Ordering::Acquire) { UNINITIALIZED => { LOGGER = logger; - STATE.store(INITIALIZED, Ordering::SeqCst); + STATE.store(INITIALIZED, Ordering::Release); Ok(()) } INITIALIZING => { diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 8754bb16a..bc8621558 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -5,6 +5,9 @@ edition = "2021" publish = false build = "src/build.rs" +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(lib_build)'] } + [features] std = ["log/std"] kv = ["log/kv"]