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/Atomic.h"
17 #include "llvm/Support/Mutex.h"
18 #include "llvm/Support/MutexGuard.h"
19 #include "llvm/Support/Threading.h"
23 static const ManagedStaticBase *StaticList = nullptr;
24 static sys::Mutex *ManagedStaticMutex = nullptr;
25 LLVM_DEFINE_ONCE_FLAG(mutex_init_flag);
27 static void initializeMutex() {
28 ManagedStaticMutex = new sys::Mutex();
31 static sys::Mutex* getManagedStaticMutex() {
32 // We need to use a function local static here, since this can get called
33 // during a static constructor and we need to guarantee that it's initialized
35 call_once(mutex_init_flag, initializeMutex);
36 return ManagedStaticMutex;
39 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
40 void (*Deleter)(void*)) const {
42 if (llvm_is_multithreaded()) {
43 MutexGuard Lock(*getManagedStaticMutex());
46 void* tmp = Creator();
48 TsanHappensBefore(this);
51 // This write is racy against the first read in the ManagedStatic
52 // accessors. The race is benign because it does a second read after a
53 // memory fence, at which point it isn't possible to get a partial value.
54 TsanIgnoreWritesBegin();
56 TsanIgnoreWritesEnd();
59 // Add to list of managed statics.
64 assert(!Ptr && !DeleterFn && !Next &&
65 "Partially initialized ManagedStatic!?");
69 // Add to list of managed statics.
75 void ManagedStaticBase::destroy() const {
76 assert(DeleterFn && "ManagedStatic not initialized correctly!");
77 assert(StaticList == this &&
78 "Not destroyed in reverse order of construction?");
91 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
92 void llvm::llvm_shutdown() {
93 MutexGuard Lock(*getManagedStaticMutex());
96 StaticList->destroy();