diff options
author | Owen Anderson <resistor@mac.com> | 2009-05-20 00:39:20 +0000 |
---|---|---|
committer | Owen Anderson <resistor@mac.com> | 2009-05-20 00:39:20 +0000 |
commit | b4d97b78dfd0d14a788fa3cb876f67a9e666b99b (patch) | |
tree | e42e951916b19287fa971f1dc6b771fcb4104685 /lib/Support/ManagedStatic.cpp | |
parent | 513fae2db51c7d22ec1253e89939d27732b21a5e (diff) |
Add llvm_start_multithreaded(), which starts up the LLVM internals in thread-safe mode. Provide double-check locking
initialization of ManagedStatic's when running in thread-safe mode.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@72151 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/ManagedStatic.cpp')
-rw-r--r-- | lib/Support/ManagedStatic.cpp | 53 |
1 files changed, 45 insertions, 8 deletions
diff --git a/lib/Support/ManagedStatic.cpp b/lib/Support/ManagedStatic.cpp index c365e013c6..056b6c06c1 100644 --- a/lib/Support/ManagedStatic.cpp +++ b/lib/Support/ManagedStatic.cpp @@ -12,21 +12,44 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/ManagedStatic.h" +#include "llvm/Config/config.h" +#include "llvm/System/Atomic.h" +#include "llvm/System/Mutex.h" #include <cassert> using namespace llvm; static const ManagedStaticBase *StaticList = 0; -void ManagedStaticBase::RegisterManagedStatic(void *ObjPtr, +static sys::Mutex* ManagedStaticMutex = 0; + +void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), void (*Deleter)(void*)) const { - assert(Ptr == 0 && DeleterFn == 0 && Next == 0 && - "Partially init static?"); - Ptr = ObjPtr; - DeleterFn = Deleter; + if (ManagedStaticMutex) { + ManagedStaticMutex->acquire(); + + if (Ptr == 0) { + void* tmp = Creator ? Creator() : 0; + + sys::MemoryFence(); + Ptr = tmp; + DeleterFn = Deleter; + + // Add to list of managed statics. + Next = StaticList; + StaticList = this; + } + + ManagedStaticMutex->release(); + } else { + assert(Ptr == 0 && DeleterFn == 0 && Next == 0 && + "Partially initialized ManagedStatic!?"); + Ptr = Creator ? Creator() : 0; + DeleterFn = Deleter; - // Add to list of managed statics. - Next = StaticList; - StaticList = this; + // Add to list of managed statics. + Next = StaticList; + StaticList = this; + } } void ManagedStaticBase::destroy() const { @@ -45,9 +68,23 @@ void ManagedStaticBase::destroy() const { DeleterFn = 0; } +void llvm::llvm_start_multithreaded() { +#if LLVM_MULTITHREADED + assert(ManagedStaticMutex == 0 && "Multithreaded LLVM already initialized!"); + ManagedStaticMutex = new sys::Mutex(true); +#else + assert(0 && "LLVM built without multithreading support!"); +#endif +} + /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables. void llvm::llvm_shutdown() { while (StaticList) StaticList->destroy(); + + if (ManagedStaticMutex) { + delete ManagedStaticMutex; + ManagedStaticMutex = 0; + } } |