log: prefer once_flag over an atomic<bool>

This commit is contained in:
Danny Robson 2020-08-03 11:47:45 +10:00
parent e8f23a349e
commit f178159fba

View File

@ -47,7 +47,7 @@ cruft::log::needs_break (level_t level)
///////////////////////////////////////////////////////////////////////////////
// mingw#xxx: MinGW doesn't have once_flag so we just use an atomic bool.
// Pray that multiple threads don't run into an issue here.
static std::atomic<bool> s_sink_init;
static std::once_flag s_sink_init;
static std::unique_ptr<cruft::log::sink::base> s_default_sink;
@ -64,7 +64,7 @@ cruft::log::default_sink (std::unique_ptr<sink::base> value)
cruft::log::sink::base&
cruft::log::default_sink ()
{
if (unlikely (!s_sink_init)) {
std::call_once (s_sink_init, [&] () {
static char const *DEFAULT_SINK = "CONSOLE";
char const *log_sink = getenv ("CRUFT_LOG_SINK") ?: DEFAULT_SINK;
@ -76,9 +76,7 @@ cruft::log::default_sink ()
std::clog << "Unknown logging sink. Falling back to console.\n";
default_sink (std::make_unique<sink::console> (PACKAGE_NAME));
}
s_sink_init = true;
}
});
return *s_default_sink;
}