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"
21 static const ManagedStaticBase *StaticList = nullptr;
23 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
24 void (*Deleter)(void*)) const {
26 if (llvm_is_multithreaded()) {
27 std::lock_guard<llvm::recursive_mutex> Lock(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.
49 assert(!Ptr && !DeleterFn && !Next &&
50 "Partially initialized ManagedStatic!?");
54 // Add to list of managed statics.
60 void ManagedStaticBase::destroy() const {
61 assert(DeleterFn && "ManagedStatic not initialized correctly!");
62 assert(StaticList == this &&
63 "Not destroyed in reverse order of construction?");
76 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
77 void llvm::llvm_shutdown() {
79 StaticList->destroy();