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/Compiler.h"
18 #include "llvm/Support/Mutex.h"
19 #include "llvm/Support/MutexGuard.h"
23 static const ManagedStaticBase *StaticList = nullptr;
25 static sys::Mutex& getManagedStaticMutex() {
26 // We need to use a function local static here, since this can get called
27 // during a static constructor and we need to guarantee that it's initialized
29 static sys::Mutex ManagedStaticMutex;
30 return ManagedStaticMutex;
33 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
34 void (*Deleter)(void*)) const {
36 if (llvm_is_multithreaded()) {
37 MutexGuard Lock(getManagedStaticMutex());
40 void* tmp = Creator();
42 TsanHappensBefore(this);
45 // This write is racy against the first read in the ManagedStatic
46 // accessors. The race is benign because it does a second read after a
47 // memory fence, at which point it isn't possible to get a partial value.
48 TsanIgnoreWritesBegin();
50 TsanIgnoreWritesEnd();
53 // Add to list of managed statics.
58 assert(!Ptr && !DeleterFn && !Next &&
59 "Partially initialized ManagedStatic!?");
63 // Add to list of managed statics.
69 void ManagedStaticBase::destroy() const {
70 assert(DeleterFn && "ManagedStatic not initialized correctly!");
71 assert(StaticList == this &&
72 "Not destroyed in reverse order of construction?");
85 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
86 void llvm::llvm_shutdown() {
87 MutexGuard Lock(getManagedStaticMutex());
90 StaticList->destroy();