Skip to content

Commit

Permalink
Merge pull request #632 from rust-lang/feat/loosen-atomics
Browse files Browse the repository at this point in the history
Loosen orderings for logger initialization
  • Loading branch information
KodrAus committed Jun 26, 2024
2 parents c879b01 + 507b672 commit e0d389c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -1405,24 +1401,21 @@ fn set_logger_inner<F>(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(()))
Expand Down Expand Up @@ -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 => {
Expand Down
3 changes: 3 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit e0d389c

Please sign in to comment.