Make the lazy initialization of DefaultTimerGroup threadsafe.
authorOwen Anderson <resistor@mac.com>
Tue, 23 Jun 2009 17:33:37 +0000 (17:33 +0000)
committerOwen Anderson <resistor@mac.com>
Tue, 23 Jun 2009 17:33:37 +0000 (17:33 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73963 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Timer.cpp

index 3c8879bd06e35da6aad9377c189034c4d7ee79d3..bcb27a4ab4f37bf0e3159a24bfd5a4681f77a157 100644 (file)
@@ -52,8 +52,20 @@ namespace {
 
 static TimerGroup *DefaultTimerGroup = 0;
 static TimerGroup *getDefaultTimerGroup() {
-  if (DefaultTimerGroup) return DefaultTimerGroup;
-  return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
+  TimerGroup* tmp = DefaultTimerGroup;
+  sys::MemoryFence();
+  if (!tmp) {
+    llvm_acquire_global_lock();
+    tmp = DefaultTimerGroup;
+    if (!tmp) {
+      tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
+      sys::MemoryFence();
+      DefaultTimerGroup = tmp;
+    }
+    llvm_release_global_lock();
+  }
+  
+  return tmp;
 }
 
 Timer::Timer(const std::string &N)
@@ -377,11 +389,5 @@ void TimerGroup::removeTimer() {
     if (OutStream != cerr.stream() && OutStream != cout.stream())
       delete OutStream;   // Close the file...
   }
-
-  // Delete default timer group!
-  if (NumTimers == 0 && this == DefaultTimerGroup) {
-    delete DefaultTimerGroup;
-    DefaultTimerGroup = 0;
-  }
 }