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
This commit is contained in:
Owen Anderson
2009-05-20 00:39:20 +00:00
parent 513fae2db5
commit b4d97b78df
2 changed files with 78 additions and 19 deletions

View File

@@ -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;
}
}