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 = nullptr;
22 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
23 void (*Deleter)(void*)) const {
25 if (llvm_is_multithreaded()) {
26 llvm_acquire_global_lock();
29 void* tmp = Creator();
31 TsanHappensBefore(this);
34 // This write is racy against the first read in the ManagedStatic
35 // accessors. The race is benign because it does a second read after a
36 // memory fence, at which point it isn't possible to get a partial value.
37 TsanIgnoreWritesBegin();
39 TsanIgnoreWritesEnd();
42 // Add to list of managed statics.
47 llvm_release_global_lock();
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();
81 if (llvm_is_multithreaded()) llvm_stop_multithreaded();