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"
22 static const ManagedStaticBase *StaticList = nullptr;
24 static sys::Mutex& getManagedStaticMutex() {
25 // We need to use a function local static here, since this can get called
26 // during a static constructor and we need to guarantee that it's initialized
28 static sys::Mutex ManagedStaticMutex;
29 return ManagedStaticMutex;
32 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
33 void (*Deleter)(void*)) const {
35 if (llvm_is_multithreaded()) {
36 MutexGuard Lock(getManagedStaticMutex());
39 void* tmp = Creator();
41 TsanHappensBefore(this);
44 // This write is racy against the first read in the ManagedStatic
45 // accessors. The race is benign because it does a second read after a
46 // memory fence, at which point it isn't possible to get a partial value.
47 TsanIgnoreWritesBegin();
49 TsanIgnoreWritesEnd();
52 // Add to list of managed statics.
57 assert(!Ptr && !DeleterFn && !Next &&
58 "Partially initialized ManagedStatic!?");
62 // Add to list of managed statics.
68 void ManagedStaticBase::destroy() const {
69 assert(DeleterFn && "ManagedStatic not initialized correctly!");
70 assert(StaticList == this &&
71 "Not destroyed in reverse order of construction?");
84 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
85 void llvm::llvm_shutdown() {
86 MutexGuard Lock(getManagedStaticMutex());
89 StaticList->destroy();