1 //===-- ManagedStatic.cpp - Static Global wrapper -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ManagedStatic class and llvm_shutdown().
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/ManagedStatic.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Mutex.h"
17 #include "llvm/Support/MutexGuard.h"
18 #include "llvm/Support/Threading.h"
22 static const ManagedStaticBase *StaticList = nullptr;
23 static sys::Mutex *ManagedStaticMutex = nullptr;
24 LLVM_DEFINE_ONCE_FLAG(mutex_init_flag);
26 static void initializeMutex() {
27 ManagedStaticMutex = new sys::Mutex();
30 static sys::Mutex* getManagedStaticMutex() {
31 // We need to use a function local static here, since this can get called
32 // during a static constructor and we need to guarantee that it's initialized
34 llvm::call_once(mutex_init_flag, initializeMutex);
35 return ManagedStaticMutex;
38 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
39 void (*Deleter)(void*)) const {
41 if (llvm_is_multithreaded()) {
42 MutexGuard Lock(*getManagedStaticMutex());
44 if (!Ptr.load(std::memory_order_relaxed)) {
45 void *Tmp = Creator();
47 Ptr.store(Tmp, std::memory_order_release);
50 // Add to list of managed statics.
55 assert(!Ptr && !DeleterFn && !Next &&
56 "Partially initialized ManagedStatic!?");
60 // Add to list of managed statics.
66 void ManagedStaticBase::destroy() const {
67 assert(DeleterFn && "ManagedStatic not initialized correctly!");
68 assert(StaticList == this &&
69 "Not destroyed in reverse order of construction?");
82 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
83 void llvm::llvm_shutdown() {
84 MutexGuard Lock(*getManagedStaticMutex());
87 StaticList->destroy();