Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Relax atomic orderings #520

Closed
wants to merge 4 commits into from
Closed
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
27 changes: 12 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ const UNINITIALIZED: usize = 0;
const INITIALIZING: usize = 1;
const INITIALIZED: usize = 2;

static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(0);
static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(LevelFilter::Off as usize);

static LOG_LEVEL_NAMES: [&str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];

Expand Down Expand Up @@ -1405,24 +1405,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 {
while STATE.load(Ordering::Relaxed) == INITIALIZING {
std::hint::spin_loop();
}
Err(SetLoggerError(()))
Expand Down Expand Up @@ -1451,10 +1448,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 Expand Up @@ -1511,11 +1508,11 @@ pub fn logger() -> &'static dyn Log {
// initialized, observing it after `Acquire` load here makes both
// write to the `LOGGER` static and initialization of the logger
// internal state synchronized with current thread.
if STATE.load(Ordering::Acquire) != INITIALIZED {
if STATE.load(Ordering::Acquire) == INITIALIZED {
unsafe { LOGGER }
} else {
static NOP: NopLogger = NopLogger;
&NOP
} else {
unsafe { LOGGER }
}
}

Expand Down
Loading