log
log copied to clipboard
Is it necessary to use Ordering::SeqCst in the log! ??
pub fn logger() -> &'static dyn Log {
// Is it necessary to use Ordering::SeqCst in log!
if STATE.load(Ordering::SeqCst) != INITIALIZED {
static NOP: NopLogger = NopLogger;
&NOP
} else {
unsafe { LOGGER }
}
}
See the implementation of std::lazy::SyncOnceCell or parking_lot::Once
I tried to use a more relaxed order and wrote a test https://github.com/relufi/log/blob/use-relaxed-memory-barriers/src/lib.rs
However, when I tried to change all the memory order to Relaxed and tested on the arm platform, there was no visibility problem either.
I think the ordering there could probably be downgraded so long as we always see the store of STATE after the store of LOGGER. The orderings on initialization are also pretty conservative, but it's always hard to tell whether that's because they're necessary or because they haven't been optimized.
Reference std::sync::Once SeqCst is not necessary, I am concerned that using SeqCst in every log! will affect performance (log! is too common and SeqCst is too expensive on some platforms). If you're worried about problems you can just introduce std::sync::Once or parking_lot::Once to avoid them.
I am using a translation tool, so please bear with me if I make any mistakes.