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;
34 // Add to list of managed statics.
39 llvm_release_global_lock();
41 assert(Ptr
== 0 && DeleterFn
== 0 && Next
== 0 &&
42 "Partially initialized ManagedStatic!?");
43 Ptr
= Creator
? Creator() : 0;
46 // Add to list of managed statics.
52 void ManagedStaticBase::destroy() const {
53 assert(DeleterFn
&& "ManagedStatic not initialized correctly!");
54 assert(StaticList
== this &&
55 "Not destroyed in reverse order of construction?");
68 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
69 void llvm::llvm_shutdown() {
71 StaticList
->destroy();
73 if (llvm_is_multithreaded()) llvm_stop_multithreaded();