Skip to content

Commit

Permalink
Use Acquire ordering for initialization check
Browse files Browse the repository at this point in the history
Reasoning is provided in the code comment.
  • Loading branch information
AngelicosPhosphoros committed Jan 2, 2024
1 parent c5ddd6f commit 0c956e9
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,15 @@ impl error::Error for ParseLevelError {}
///
/// If a logger has not been set, a no-op implementation is returned.
pub fn logger() -> &'static dyn Log {
if STATE.load(Ordering::SeqCst) != INITIALIZED {
// Acquire memory ordering guarantees that current thread would see any
// memory writes that happened before store of the value
// into `STATE` with memory ordering `Release` or stronger.
//
// Since the value `INITIALIZED` is written only after `LOGGER` was
// 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 {
static NOP: NopLogger = NopLogger;
&NOP
} else {
Expand Down

0 comments on commit 0c956e9

Please sign in to comment.