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"
19 static const ManagedStaticBase *StaticList = 0;
21 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
22 void (*Deleter)(void*)) const {
23 if (llvm_is_multithreaded()) {
24 llvm_acquire_global_lock();
27 void* tmp = Creator ? Creator() : 0;
29 TsanHappensBefore(this);
30 std::atomic_thread_fence(std::memory_order_seq_cst);
32 // This write is racy against the first read in the ManagedStatic
33 // accessors. The race is benign because it does a second read after a
34 // memory fence, at which point it isn't possible to get a partial value.
35 TsanIgnoreWritesBegin();
37 TsanIgnoreWritesEnd();
40 // Add to list of managed statics.
45 llvm_release_global_lock();
47 assert(Ptr == 0 && DeleterFn == 0 && Next == 0 &&
48 "Partially initialized ManagedStatic!?");
49 Ptr = Creator ? Creator() : 0;
52 // Add to list of managed statics.
58 void ManagedStaticBase::destroy() const {
59 assert(DeleterFn && "ManagedStatic not initialized correctly!");
60 assert(StaticList == this &&
61 "Not destroyed in reverse order of construction?");
74 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
75 void llvm::llvm_shutdown() {
77 StaticList->destroy();
79 if (llvm_is_multithreaded()) llvm_stop_multithreaded();