mirror of
https://github.com/RPCSX/llvm.git
synced 2025-01-26 06:14:42 +00:00
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:
parent
513fae2db5
commit
b4d97b78df
@ -14,8 +14,16 @@
|
|||||||
#ifndef LLVM_SUPPORT_MANAGED_STATIC_H
|
#ifndef LLVM_SUPPORT_MANAGED_STATIC_H
|
||||||
#define LLVM_SUPPORT_MANAGED_STATIC_H
|
#define LLVM_SUPPORT_MANAGED_STATIC_H
|
||||||
|
|
||||||
|
#include "llvm/System/Atomic.h"
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
|
|
||||||
|
/// object_creator - Helper method for ManagedStatic.
|
||||||
|
template<class C>
|
||||||
|
void* object_creator() {
|
||||||
|
return new C();
|
||||||
|
}
|
||||||
|
|
||||||
/// object_deleter - Helper method for ManagedStatic.
|
/// object_deleter - Helper method for ManagedStatic.
|
||||||
///
|
///
|
||||||
template<class C>
|
template<class C>
|
||||||
@ -32,7 +40,7 @@ protected:
|
|||||||
mutable void (*DeleterFn)(void*);
|
mutable void (*DeleterFn)(void*);
|
||||||
mutable const ManagedStaticBase *Next;
|
mutable const ManagedStaticBase *Next;
|
||||||
|
|
||||||
void RegisterManagedStatic(void *ObjPtr, void (*deleter)(void*)) const;
|
void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
|
||||||
public:
|
public:
|
||||||
/// isConstructed - Return true if this object has not been created yet.
|
/// isConstructed - Return true if this object has not been created yet.
|
||||||
bool isConstructed() const { return Ptr != 0; }
|
bool isConstructed() const { return Ptr != 0; }
|
||||||
@ -51,25 +59,32 @@ public:
|
|||||||
|
|
||||||
// Accessors.
|
// Accessors.
|
||||||
C &operator*() {
|
C &operator*() {
|
||||||
if (!Ptr) LazyInit();
|
void* tmp = Ptr;
|
||||||
|
sys::MemoryFence();
|
||||||
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>);
|
||||||
|
|
||||||
return *static_cast<C*>(Ptr);
|
return *static_cast<C*>(Ptr);
|
||||||
}
|
}
|
||||||
C *operator->() {
|
C *operator->() {
|
||||||
if (!Ptr) LazyInit();
|
void* tmp = Ptr;
|
||||||
|
sys::MemoryFence();
|
||||||
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>);
|
||||||
|
|
||||||
return static_cast<C*>(Ptr);
|
return static_cast<C*>(Ptr);
|
||||||
}
|
}
|
||||||
const C &operator*() const {
|
const C &operator*() const {
|
||||||
if (!Ptr) LazyInit();
|
void* tmp = Ptr;
|
||||||
|
sys::MemoryFence();
|
||||||
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>);
|
||||||
|
|
||||||
return *static_cast<C*>(Ptr);
|
return *static_cast<C*>(Ptr);
|
||||||
}
|
}
|
||||||
const C *operator->() const {
|
const C *operator->() const {
|
||||||
if (!Ptr) LazyInit();
|
void* tmp = Ptr;
|
||||||
return static_cast<C*>(Ptr);
|
sys::MemoryFence();
|
||||||
}
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>);
|
||||||
|
|
||||||
public:
|
return static_cast<C*>(Ptr);
|
||||||
void LazyInit() const {
|
|
||||||
RegisterManagedStatic(new C(), object_deleter<C>);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -80,6 +95,10 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/// llvm_start_multithreaded - Allocate and initialize structures needed to
|
||||||
|
/// make LLVM safe for multithreading.
|
||||||
|
void llvm_start_multithreaded();
|
||||||
|
|
||||||
/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
|
/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
|
||||||
void llvm_shutdown();
|
void llvm_shutdown();
|
||||||
|
|
||||||
@ -87,7 +106,10 @@ void llvm_shutdown();
|
|||||||
/// llvm_shutdown_obj - This is a simple helper class that calls
|
/// llvm_shutdown_obj - This is a simple helper class that calls
|
||||||
/// llvm_shutdown() when it is destroyed.
|
/// llvm_shutdown() when it is destroyed.
|
||||||
struct llvm_shutdown_obj {
|
struct llvm_shutdown_obj {
|
||||||
llvm_shutdown_obj() {}
|
llvm_shutdown_obj() { }
|
||||||
|
explicit llvm_shutdown_obj(bool multithreaded) {
|
||||||
|
if (multithreaded) llvm_start_multithreaded();
|
||||||
|
}
|
||||||
~llvm_shutdown_obj() { llvm_shutdown(); }
|
~llvm_shutdown_obj() { llvm_shutdown(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -12,21 +12,44 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#include "llvm/Support/ManagedStatic.h"
|
#include "llvm/Support/ManagedStatic.h"
|
||||||
|
#include "llvm/Config/config.h"
|
||||||
|
#include "llvm/System/Atomic.h"
|
||||||
|
#include "llvm/System/Mutex.h"
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
static const ManagedStaticBase *StaticList = 0;
|
static const ManagedStaticBase *StaticList = 0;
|
||||||
|
|
||||||
void ManagedStaticBase::RegisterManagedStatic(void *ObjPtr,
|
static sys::Mutex* ManagedStaticMutex = 0;
|
||||||
|
|
||||||
|
void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
|
||||||
void (*Deleter)(void*)) const {
|
void (*Deleter)(void*)) const {
|
||||||
assert(Ptr == 0 && DeleterFn == 0 && Next == 0 &&
|
if (ManagedStaticMutex) {
|
||||||
"Partially init static?");
|
ManagedStaticMutex->acquire();
|
||||||
Ptr = ObjPtr;
|
|
||||||
DeleterFn = Deleter;
|
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.
|
// Add to list of managed statics.
|
||||||
Next = StaticList;
|
Next = StaticList;
|
||||||
StaticList = this;
|
StaticList = this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ManagedStaticBase::destroy() const {
|
void ManagedStaticBase::destroy() const {
|
||||||
@ -45,9 +68,23 @@ void ManagedStaticBase::destroy() const {
|
|||||||
DeleterFn = 0;
|
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.
|
/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
|
||||||
void llvm::llvm_shutdown() {
|
void llvm::llvm_shutdown() {
|
||||||
while (StaticList)
|
while (StaticList)
|
||||||
StaticList->destroy();
|
StaticList->destroy();
|
||||||
|
|
||||||
|
if (ManagedStaticMutex) {
|
||||||
|
delete ManagedStaticMutex;
|
||||||
|
ManagedStaticMutex = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user