[Lldb-commits] [lldb] r264346 - 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.

Greg Clayton via lldb-commits lldb-commits at lists.llvm.org
Thu Mar 24 14:46:47 PDT 2016


Author: gclayton
Date: Thu Mar 24 16:46:47 2016
New Revision: 264346

URL: http://llvm.org/viewvc/llvm-project?rev=264346&view=rev
Log:
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.


Modified:
    lldb/trunk/include/lldb/Core/Timer.h
    lldb/trunk/source/Core/Timer.cpp

Modified: lldb/trunk/include/lldb/Core/Timer.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/Timer.h?rev=264346&r1=264345&r2=264346&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/Timer.h (original)
+++ lldb/trunk/include/lldb/Core/Timer.h Thu Mar 24 16:46:47 2016
@@ -86,7 +86,6 @@ protected:
 
     static std::atomic<bool> g_quiet;
     static std::atomic<unsigned> g_display_depth;
-    static std::mutex g_file_mutex;
 
 private:
     Timer();

Modified: lldb/trunk/source/Core/Timer.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Timer.cpp?rev=264346&r1=264345&r2=264346&view=diff
==============================================================================
--- lldb/trunk/source/Core/Timer.cpp (original)
+++ lldb/trunk/source/Core/Timer.cpp Thu Mar 24 16:46:47 2016
@@ -39,7 +39,18 @@ namespace
 
 std::atomic<bool> Timer::g_quiet(true);
 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 &
@@ -97,7 +108,7 @@ Timer::Timer (const char *category, cons
     {
         if (g_quiet == false)
         {
-            std::lock_guard<std::mutex> lock(g_file_mutex);
+            std::lock_guard<std::mutex> lock(GetFileMutex());
 
             // Indent
             ::fprintf(stdout, "%*s", stack->m_depth * TIMER_INDENT_AMOUNT, "");
@@ -152,7 +163,7 @@ Timer::~Timer()
 
         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, "",
                       total_nsec / 1000000000.0, timer_nsec / 1000000000.0);
         }




More information about the lldb-commits mailing list