LLVM API Documentation

ManagedStatic.cpp

Go to the documentation of this file.
00001 //===-- ManagedStatic.cpp - Static Global wrapper -------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the ManagedStatic class and llvm_shutdown().
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Support/ManagedStatic.h"
00015 #include "llvm/Config/config.h"
00016 #include "llvm/System/Atomic.h"
00017 #include <cassert>
00018 using namespace llvm;
00019 
00020 static const ManagedStaticBase *StaticList = 0;
00021 
00022 void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
00023                                               void (*Deleter)(void*)) const {
00024   if (llvm_is_multithreaded()) {
00025     llvm_acquire_global_lock();
00026 
00027     if (Ptr == 0) {
00028       void* tmp = Creator ? Creator() : 0;
00029 
00030       sys::MemoryFence();
00031       Ptr = tmp;
00032       DeleterFn = Deleter;
00033       
00034       // Add to list of managed statics.
00035       Next = StaticList;
00036       StaticList = this;
00037     }
00038 
00039     llvm_release_global_lock();
00040   } else {
00041     assert(Ptr == 0 && DeleterFn == 0 && Next == 0 &&
00042            "Partially initialized ManagedStatic!?");
00043     Ptr = Creator ? Creator() : 0;
00044     DeleterFn = Deleter;
00045   
00046     // Add to list of managed statics.
00047     Next = StaticList;
00048     StaticList = this;
00049   }
00050 }
00051 
00052 void ManagedStaticBase::destroy() const {
00053   assert(DeleterFn && "ManagedStatic not initialized correctly!");
00054   assert(StaticList == this &&
00055          "Not destroyed in reverse order of construction?");
00056   // Unlink from list.
00057   StaticList = Next;
00058   Next = 0;
00059 
00060   // Destroy memory.
00061   DeleterFn(Ptr);
00062   
00063   // Cleanup.
00064   Ptr = 0;
00065   DeleterFn = 0;
00066 }
00067 
00068 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
00069 void llvm::llvm_shutdown() {
00070   while (StaticList)
00071     StaticList->destroy();
00072 
00073   if (llvm_is_multithreaded()) llvm_stop_multithreaded();
00074 }
00075