Kill the LLVM global lock.
authorZachary Turner <zturner@google.com>
Mon, 16 Jun 2014 22:40:42 +0000 (22:40 +0000)
committerZachary Turner <zturner@google.com>
Mon, 16 Jun 2014 22:40:42 +0000 (22:40 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211069 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/Threading.h
lib/Support/ManagedStatic.cpp
lib/Support/Threading.cpp
lib/Support/Timer.cpp

index bfcf1be37d34e226e9e3f04b6152b46faef1d6a2..8f87cbc7f4106ae4d988fc591c9cbc1ff9bf1441 100644 (file)
@@ -18,9 +18,6 @@
 #include "llvm/Support/Mutex.h"
 
 namespace llvm {
-  /// llvm_get_global_lock - returns the llvm global lock object.
-  sys::Mutex &llvm_get_global_lock();
-
   /// llvm_is_multithreaded - returns true if LLVM is compiled with support
   /// for multiple threads, and false otherwise.
   bool llvm_is_multithreaded();
index 1117190888baa58e8d82055fb756abbe6560bbe5..ced2b13b3a14ed14f65e9c7fa897a2e787665d82 100644 (file)
 #include "llvm/Support/Atomic.h"
 #include "llvm/Support/MutexGuard.h"
 #include <cassert>
+#include <mutex>
 using namespace llvm;
 
 static const ManagedStaticBase *StaticList = nullptr;
 
+// ManagedStatics can get created during execution of static constructors.  As a
+// result, we cannot use a global static std::mutex object for the lock since it
+// may not have been constructed.  Instead, we do a call-once initialization of
+// a pointer to a mutex.  This also means that we must not "initialize" the
+// mutex with nullptr, otherwise it might get reset to nullptr after being
+// initialized by std::call_once.
+static std::once_flag MutexInitializationFlag;
+static std::recursive_mutex *ManagedStaticMutex;
+
+namespace {
+  void InitializeManagedStaticMutex() {
+    std::call_once(MutexInitializationFlag,
+        []() { ManagedStaticMutex = new std::recursive_mutex(); });
+  }
+}
+
 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
                                               void (*Deleter)(void*)) const {
   assert(Creator);
   if (llvm_is_multithreaded()) {
-    llvm::MutexGuard Lock(llvm::llvm_get_global_lock());
+    InitializeManagedStaticMutex();
 
+    std::lock_guard<std::recursive_mutex> Lock(*ManagedStaticMutex);
     if (!Ptr) {
       void* tmp = Creator();
 
@@ -74,6 +92,9 @@ void ManagedStaticBase::destroy() const {
 
 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
 void llvm::llvm_shutdown() {
+  InitializeManagedStaticMutex();
+  std::lock_guard<std::recursive_mutex> Lock(*ManagedStaticMutex);
+
   while (StaticList)
     StaticList->destroy();
 }
index ff591aaa1d8fb08dc62f991a273df50e077d5718..838a71b789d9ee4594c7dd89715100c7efca8b01 100644 (file)
 
 using namespace llvm;
 
-sys::Mutex& llvm::llvm_get_global_lock() {
-  static sys::Mutex global_lock;
-  return global_lock;
-}
-
 bool llvm::llvm_is_multithreaded() {
 #if LLVM_ENABLE_THREADS != 0
   return true;
index c7427260bff7518ccbfd216637380fe2a0b0459f..7738283bad086719c1a3ec8561889436c036cc09 100644 (file)
@@ -84,7 +84,7 @@ static TimerGroup *getDefaultTimerGroup() {
   sys::MemoryFence();
   if (tmp) return tmp;
   
-  llvm::MutexGuard Lock(llvm::llvm_get_global_lock());
+  sys::SmartScopedLock<true> Lock(*TimerLock);
   tmp = DefaultTimerGroup;
   if (!tmp) {
     tmp = new TimerGroup("Miscellaneous Ungrouped Timers");