Skip to content

Commit

Permalink
Merge pull request #287 from sunfishcode/wait-for-initialized
Browse files Browse the repository at this point in the history
Ensure that initialization is complete after `set_logger_inner`.
  • Loading branch information
sfackler authored Aug 2, 2018
2 parents 7a60286 + 7e443a0 commit ea901a1
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,13 +1151,18 @@ where
F: FnOnce() -> &'static Log,
{
unsafe {
if STATE.compare_and_swap(UNINITIALIZED, INITIALIZING, Ordering::SeqCst) != UNINITIALIZED {
return Err(SetLoggerError(()));
match STATE.compare_and_swap(UNINITIALIZED, INITIALIZING, Ordering::SeqCst) {
UNINITIALIZED => {
LOGGER = make_logger();
STATE.store(INITIALIZED, Ordering::SeqCst);
Ok(())
}
INITIALIZING => {
while STATE.load(Ordering::SeqCst) == INITIALIZING {}
Err(SetLoggerError(()))
}
_ => Err(SetLoggerError(())),
}

LOGGER = make_logger();
STATE.store(INITIALIZED, Ordering::SeqCst);
Ok(())
}
}

Expand Down

0 comments on commit ea901a1

Please sign in to comment.