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/System/Atomic.h"
17 #include "llvm/System/Mutex.h"
21 static const ManagedStaticBase *StaticList = 0;
23 static sys::Mutex* ManagedStaticMutex = 0;
25 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
26 void (*Deleter)(void*)) const {
27 if (ManagedStaticMutex) {
28 ManagedStaticMutex->acquire();
31 void* tmp = Creator ? Creator() : 0;
37 // Add to list of managed statics.
42 ManagedStaticMutex->release();
44 assert(Ptr == 0 && DeleterFn == 0 && Next == 0 &&
45 "Partially initialized ManagedStatic!?");
46 Ptr = Creator ? Creator() : 0;
49 // Add to list of managed statics.
55 void ManagedStaticBase::destroy() const {
56 assert(DeleterFn && "ManagedStatic not initialized correctly!");
57 assert(StaticList == this &&
58 "Not destroyed in reverse order of construction?");
71 void llvm::llvm_start_multithreaded() {
72 #if LLVM_MULTITHREADED
73 assert(ManagedStaticMutex == 0 && "Multithreaded LLVM already initialized!");
74 ManagedStaticMutex = new sys::Mutex(true);
76 assert(0 && "LLVM built without multithreading support!");
80 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
81 void llvm::llvm_shutdown() {
83 StaticList->destroy();
85 if (ManagedStaticMutex) {
86 delete ManagedStaticMutex;
87 ManagedStaticMutex = 0;