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 // ManagedStatics can get created during execution of static constructors. As a
24 // result, we cannot use a global static std::mutex object for the lock since it
25 // may not have been constructed. Instead, we do a call-once initialization of
26 // a pointer to a mutex.
27 static std::once_flag MutexInitializationFlag;
28 static std::recursive_mutex* ManagedStaticMutex = nullptr;
30 // Not all supported platforms (in particular VS2012) have thread-safe function
31 // static initialization, so roll our own.
32 static std::recursive_mutex& GetManagedStaticMutex() {
33 std::call_once(MutexInitializationFlag,
34 []() { ManagedStaticMutex = new std::recursive_mutex(); } );
36 return *ManagedStaticMutex;
39 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
40 void (*Deleter)(void*)) const {
42 if (llvm_is_multithreaded()) {
43 std::lock_guard<std::recursive_mutex> Lock(GetManagedStaticMutex());
46 void* tmp = Creator();
48 TsanHappensBefore(this);
51 // This write is racy against the first read in the ManagedStatic
52 // accessors. The race is benign because it does a second read after a
53 // memory fence, at which point it isn't possible to get a partial value.
54 TsanIgnoreWritesBegin();
56 TsanIgnoreWritesEnd();
59 // Add to list of managed statics.
64 assert(!Ptr && !DeleterFn && !Next &&
65 "Partially initialized ManagedStatic!?");
69 // Add to list of managed statics.
75 void ManagedStaticBase::destroy() const {
76 assert(DeleterFn && "ManagedStatic not initialized correctly!");
77 assert(StaticList == this &&
78 "Not destroyed in reverse order of construction?");
91 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
92 void llvm::llvm_shutdown() {
93 std::lock_guard<std::recursive_mutex> Lock(GetManagedStaticMutex());
96 StaticList->destroy();
98 if (llvm_is_multithreaded()) llvm_stop_multithreaded();