Get rid of a global constructor that was causing a warning on MacOSX and make the Timer safe to use after the main threads global destructor chain is called.

llvm-svn: 264346
This commit is contained in:
Greg Clayton 2016-03-24 21:46:47 +00:00
parent ae81c29352
commit 8e1232af42
2 changed files with 14 additions and 4 deletions

View File

@ -86,7 +86,6 @@ protected:
static std::atomic<bool> g_quiet; static std::atomic<bool> g_quiet;
static std::atomic<unsigned> g_display_depth; static std::atomic<unsigned> g_display_depth;
static std::mutex g_file_mutex;
private: private:
Timer(); Timer();

View File

@ -39,7 +39,18 @@ namespace
std::atomic<bool> Timer::g_quiet(true); std::atomic<bool> Timer::g_quiet(true);
std::atomic<unsigned> Timer::g_display_depth(0); std::atomic<unsigned> Timer::g_display_depth(0);
std::mutex Timer::g_file_mutex; static std::mutex &
GetFileMutex()
{
static std::mutex *g_file_mutex_ptr = nullptr;
static std::once_flag g_once_flag;
std::call_once(g_once_flag, []() {
// leaked on purpose to ensure this mutex works after main thread has run
// global C++ destructor chain
g_file_mutex_ptr = new std::mutex();
});
return *g_file_mutex_ptr;
}
static Mutex & static Mutex &
@ -97,7 +108,7 @@ Timer::Timer (const char *category, const char *format, ...) :
{ {
if (g_quiet == false) if (g_quiet == false)
{ {
std::lock_guard<std::mutex> lock(g_file_mutex); std::lock_guard<std::mutex> lock(GetFileMutex());
// Indent // Indent
::fprintf(stdout, "%*s", stack->m_depth * TIMER_INDENT_AMOUNT, ""); ::fprintf(stdout, "%*s", stack->m_depth * TIMER_INDENT_AMOUNT, "");
@ -152,7 +163,7 @@ Timer::~Timer()
if (g_quiet == false) if (g_quiet == false)
{ {
std::lock_guard<std::mutex> lock(g_file_mutex); std::lock_guard<std::mutex> lock(GetFileMutex());
::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n", (stack->m_depth - 1) * TIMER_INDENT_AMOUNT, "", ::fprintf(stdout, "%*s%.9f sec (%.9f sec)\n", (stack->m_depth - 1) * TIMER_INDENT_AMOUNT, "",
total_nsec / 1000000000.0, timer_nsec / 1000000000.0); total_nsec / 1000000000.0, timer_nsec / 1000000000.0);
} }