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 authored and Thomasdezeeuw committed Jan 10, 2024
1 parent c5ddd6f commit 7cb6a01
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lib.rs
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 7cb6a01

Please sign in to comment.