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/MutexGuard.h"
21 static const ManagedStaticBase *StaticList = nullptr;
23 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
24 void (*Deleter)(void*)) const {
26 if (llvm_is_multithreaded()) {
27 llvm::MutexGuard Lock(llvm::llvm_get_global_lock());
30 void* tmp = Creator();
32 TsanHappensBefore(this);
35 // This write is racy against the first read in the ManagedStatic
36 // accessors. The race is benign because it does a second read after a
37 // memory fence, at which point it isn't possible to get a partial value.
38 TsanIgnoreWritesBegin();
40 TsanIgnoreWritesEnd();
43 // Add to list of managed statics.
48 assert(!Ptr && !DeleterFn && !Next &&
49 "Partially initialized ManagedStatic!?");
53 // Add to list of managed statics.
59 void ManagedStaticBase::destroy() const {
60 assert(DeleterFn && "ManagedStatic not initialized correctly!");
61 assert(StaticList == this &&
62 "Not destroyed in reverse order of construction?");
75 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
76 void llvm::llvm_shutdown() {
78 StaticList->destroy();
80 if (llvm_is_multithreaded()) llvm_stop_multithreaded();