Skip to content

Commit 7cb6a01

Browse files
AngelicosPhosphorosThomasdezeeuw
authored andcommitted
Use Acquire ordering for initialization check
Reasoning is provided in the code comment.
1 parent c5ddd6f commit 7cb6a01

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,15 @@ impl error::Error for ParseLevelError {}
14551455
///
14561456
/// If a logger has not been set, a no-op implementation is returned.
14571457
pub fn logger() -> &'static dyn Log {
1458-
if STATE.load(Ordering::SeqCst) != INITIALIZED {
1458+
// Acquire memory ordering guarantees that current thread would see any
1459+
// memory writes that happened before store of the value
1460+
// into `STATE` with memory ordering `Release` or stronger.
1461+
//
1462+
// Since the value `INITIALIZED` is written only after `LOGGER` was
1463+
// initialized, observing it after `Acquire` load here makes both
1464+
// write to the `LOGGER` static and initialization of the logger
1465+
// internal state synchronized with current thread.
1466+
if STATE.load(Ordering::Acquire) != INITIALIZED {
14591467
static NOP: NopLogger = NopLogger;
14601468
&NOP
14611469
} else {

0 commit comments

Comments
 (0)