[Lldb-commits] [PATCH] D13940: Fix race conditions in Core/Timer

Tamas Berghammer via lldb-commits lldb-commits at lists.llvm.org
Wed Oct 21 06:36:57 PDT 2015


tberghammer created this revision.
tberghammer added reviewers: labath, clayborg.
tberghammer added a subscriber: lldb-commits.

Fix race conditions in Core/Timer

The Timer class already had some support for multi-threaded access
but it still contained several race conditions. This CL fixes them
in preparation of adding multi-threaded dwarf parsing (and other
multi-threaded parts later).

http://reviews.llvm.org/D13940

Files:
  include/lldb/Core/Timer.h
  source/Core/Timer.cpp

Index: source/Core/Timer.cpp
===================================================================
--- source/Core/Timer.cpp
+++ source/Core/Timer.cpp
@@ -21,12 +21,16 @@
 using namespace lldb_private;
 
 #define TIMER_INDENT_AMOUNT 2
-static bool g_quiet = true;
-uint32_t Timer::g_depth = 0;
-uint32_t Timer::g_display_depth = 0;
-FILE * Timer::g_file = NULL;
 typedef std::vector<Timer *> TimerStack;
 typedef std::map<const char *, uint64_t> TimerCategoryMap;
+
+thread_local unsigned Timer::g_depth(0);
+
+std::atomic_bool Timer::g_quiet(true);
+std::atomic_uint Timer::g_display_depth(0);
+std::mutex Timer::g_file_mutex;
+FILE* Timer::g_file = nullptr;
+
 static lldb::thread_key_t g_key;
 
 static Mutex &
@@ -86,6 +90,8 @@
     {
         if (g_quiet == false)
         {
+            std::lock_guard<std::mutex> lock(g_file_mutex);
+
             // Indent
             ::fprintf (g_file, "%*s", g_depth * TIMER_INDENT_AMOUNT, "");
             // Print formatted string
@@ -143,7 +149,7 @@
 
         if (g_quiet == false)
         {
-
+            std::lock_guard<std::mutex> lock(g_file_mutex);
             ::fprintf (g_file,
                        "%*s%.9f sec (%.9f sec)\n",
                        (g_depth - 1) *TIMER_INDENT_AMOUNT, "",
Index: include/lldb/Core/Timer.h
===================================================================
--- include/lldb/Core/Timer.h
+++ include/lldb/Core/Timer.h
@@ -14,6 +14,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include <string>
+#include <mutex>
 #include "lldb/lldb-private.h"
 #include "lldb/Host/TimeValue.h"
 
@@ -84,9 +85,14 @@
     TimeValue m_timer_start;
     uint64_t m_total_ticks; // Total running time for this timer including when other timers below this are running
     uint64_t m_timer_ticks; // Ticks for this timer that do not include when other timers below this one are running
-    static uint32_t g_depth;
-    static uint32_t g_display_depth;
-    static FILE * g_file;
+
+    static thread_local unsigned g_depth;
+
+    static std::atomic_bool g_quiet;
+    static std::atomic_uint g_display_depth;
+    static std::mutex g_file_mutex;
+    static FILE* g_file;
+
 private:
     Timer();
     DISALLOW_COPY_AND_ASSIGN (Timer);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13940.38005.patch
Type: text/x-patch
Size: 2217 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20151021/245b6a49/attachment.bin>


More information about the lldb-commits mailing list