summaryrefslogtreecommitdiff
path: root/lib/Support/ManagedStatic.cpp
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2014-06-16 22:40:42 +0000
committerZachary Turner <zturner@google.com>2014-06-16 22:40:42 +0000
commit9020dc35dc63ad1b3cffcdfaeb2d8396d23223db (patch)
treeb18548586f7a64d7abddf4c31b8e4487d516b732 /lib/Support/ManagedStatic.cpp
parent497b7bb3e32d3d8cbc92dbe4dde6d56615f7a15f (diff)
downloadllvm-9020dc35dc63ad1b3cffcdfaeb2d8396d23223db.tar.gz
llvm-9020dc35dc63ad1b3cffcdfaeb2d8396d23223db.tar.bz2
llvm-9020dc35dc63ad1b3cffcdfaeb2d8396d23223db.tar.xz
Kill the LLVM global lock.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211069 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/ManagedStatic.cpp')
-rw-r--r--lib/Support/ManagedStatic.cpp23
1 files changed, 22 insertions, 1 deletions
diff --git a/lib/Support/ManagedStatic.cpp b/lib/Support/ManagedStatic.cpp
index 1117190888..ced2b13b3a 100644
--- a/lib/Support/ManagedStatic.cpp
+++ b/lib/Support/ManagedStatic.cpp
@@ -16,16 +16,34 @@
#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();
}