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"
20 static const ManagedStaticBase *StaticList = 0;
22 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
23 void (*Deleter)(void*)) const {
24 if (llvm_is_multithreaded()) {
25 llvm_acquire_global_lock();
28 void* tmp = Creator ? Creator() : 0;
30 TsanHappensBefore(this);
33 // This write is racy against the first read in the ManagedStatic
34 // accessors. The race is benign because it does a second read after a
35 // memory fence, at which point it isn't possible to get a partial value.
36 TsanIgnoreWritesBegin();
38 TsanIgnoreWritesEnd();
41 // Add to list of managed statics.
46 llvm_release_global_lock();
48 assert(Ptr == 0 && DeleterFn == 0 && Next == 0 &&
49 "Partially initialized ManagedStatic!?");
50 Ptr = Creator ? Creator() : 0;
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();