#include "llvm/Support/ManagedStatic.h"
#include "llvm/Config/config.h"
#include "llvm/Support/Atomic.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/MutexGuard.h"
#include <cassert>
using namespace llvm;
static const ManagedStaticBase *StaticList = nullptr;
static sys::Mutex& getManagedStaticMutex() {
static sys::Mutex ManagedStaticMutex;
return ManagedStaticMutex;
}
void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
void (*Deleter)(void*)) const {
assert(Creator);
if (llvm_is_multithreaded()) {
MutexGuard Lock(getManagedStaticMutex());
if (!Ptr) {
void* tmp = Creator();
TsanHappensBefore(this);
sys::MemoryFence();
TsanIgnoreWritesBegin();
Ptr = tmp;
TsanIgnoreWritesEnd();
DeleterFn = Deleter;
Next = StaticList;
StaticList = this;
}
} else {
assert(!Ptr && !DeleterFn && !Next &&
"Partially initialized ManagedStatic!?");
Ptr = Creator();
DeleterFn = Deleter;
Next = StaticList;
StaticList = this;
}
}
void ManagedStaticBase::destroy() const {
assert(DeleterFn && "ManagedStatic not initialized correctly!");
assert(StaticList == this &&
"Not destroyed in reverse order of construction?");
StaticList = Next;
Next = nullptr;
DeleterFn(Ptr);
Ptr = nullptr;
DeleterFn = nullptr;
}
void llvm::llvm_shutdown() {
MutexGuard Lock(getManagedStaticMutex());
while (StaticList)
StaticList->destroy();
}