diff options
Diffstat (limited to 'lib/Support')
-rw-r--r-- | lib/Support/ManagedStatic.cpp | 28 | ||||
-rw-r--r-- | lib/Support/Threading.cpp | 41 | ||||
-rw-r--r-- | lib/Support/Timer.cpp | 5 |
3 files changed, 46 insertions, 28 deletions
diff --git a/lib/Support/ManagedStatic.cpp b/lib/Support/ManagedStatic.cpp index ced2b13b3a..6a1c2a545a 100644 --- a/lib/Support/ManagedStatic.cpp +++ b/lib/Support/ManagedStatic.cpp @@ -14,36 +14,17 @@ #include "llvm/Support/ManagedStatic.h" #include "llvm/Config/config.h" #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()) { - InitializeManagedStaticMutex(); + llvm_acquire_global_lock(); - std::lock_guard<std::recursive_mutex> Lock(*ManagedStaticMutex); if (!Ptr) { void* tmp = Creator(); @@ -62,6 +43,8 @@ void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), Next = StaticList; StaticList = this; } + + llvm_release_global_lock(); } else { assert(!Ptr && !DeleterFn && !Next && "Partially initialized ManagedStatic!?"); @@ -92,9 +75,8 @@ 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(); + + if (llvm_is_multithreaded()) llvm_stop_multithreaded(); } diff --git a/lib/Support/Threading.cpp b/lib/Support/Threading.cpp index 838a71b789..1acfa79b11 100644 --- a/lib/Support/Threading.cpp +++ b/lib/Support/Threading.cpp @@ -7,8 +7,7 @@ // //===----------------------------------------------------------------------===// // -// This file implements helper functions for running LLVM in a multi-threaded -// environment. +// This file implements llvm_start_multithreaded() and friends. // //===----------------------------------------------------------------------===// @@ -20,14 +19,50 @@ using namespace llvm; -bool llvm::llvm_is_multithreaded() { +static bool multithreaded_mode = false; + +static sys::Mutex* global_lock = nullptr; + +bool llvm::llvm_start_multithreaded() { #if LLVM_ENABLE_THREADS != 0 + assert(!multithreaded_mode && "Already multithreaded!"); + multithreaded_mode = true; + global_lock = new sys::Mutex(true); + + // We fence here to ensure that all initialization is complete BEFORE we + // return from llvm_start_multithreaded(). + sys::MemoryFence(); return true; #else return false; #endif } +void llvm::llvm_stop_multithreaded() { +#if LLVM_ENABLE_THREADS != 0 + assert(multithreaded_mode && "Not currently multithreaded!"); + + // We fence here to insure that all threaded operations are complete BEFORE we + // return from llvm_stop_multithreaded(). + sys::MemoryFence(); + + multithreaded_mode = false; + delete global_lock; +#endif +} + +bool llvm::llvm_is_multithreaded() { + return multithreaded_mode; +} + +void llvm::llvm_acquire_global_lock() { + if (multithreaded_mode) global_lock->acquire(); +} + +void llvm::llvm_release_global_lock() { + if (multithreaded_mode) global_lock->release(); +} + #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H) #include <pthread.h> diff --git a/lib/Support/Timer.cpp b/lib/Support/Timer.cpp index 7738283bad..61465ae5e8 100644 --- a/lib/Support/Timer.cpp +++ b/lib/Support/Timer.cpp @@ -18,7 +18,7 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" #include "llvm/Support/ManagedStatic.h" -#include "llvm/Support/MutexGuard.h" +#include "llvm/Support/Mutex.h" #include "llvm/Support/Process.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -84,13 +84,14 @@ static TimerGroup *getDefaultTimerGroup() { sys::MemoryFence(); if (tmp) return tmp; - sys::SmartScopedLock<true> Lock(*TimerLock); + llvm_acquire_global_lock(); tmp = DefaultTimerGroup; if (!tmp) { tmp = new TimerGroup("Miscellaneous Ungrouped Timers"); sys::MemoryFence(); DefaultTimerGroup = tmp; } + llvm_release_global_lock(); return tmp; } |