LLVM API Documentation

ManagedStatic.h

Go to the documentation of this file.
00001 //===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===//
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 defines the ManagedStatic class and the llvm_shutdown() function.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SUPPORT_MANAGED_STATIC_H
00015 #define LLVM_SUPPORT_MANAGED_STATIC_H
00016 
00017 #include "llvm/System/Atomic.h"
00018 #include "llvm/System/Threading.h"
00019 
00020 namespace llvm {
00021 
00022 /// object_creator - Helper method for ManagedStatic.
00023 template<class C>
00024 void* object_creator() {
00025   return new C();
00026 }
00027 
00028 /// object_deleter - Helper method for ManagedStatic.
00029 ///
00030 template<typename T> struct object_deleter {
00031   static void call(void * Ptr) { delete (T*)Ptr; }
00032 };
00033 template<typename T, size_t N> struct object_deleter<T[N]> {
00034   static void call(void * Ptr) { delete[] (T*)Ptr; }
00035 };
00036 
00037 /// ManagedStaticBase - Common base class for ManagedStatic instances.
00038 class ManagedStaticBase {
00039 protected:
00040   // This should only be used as a static variable, which guarantees that this
00041   // will be zero initialized.
00042   mutable void *Ptr;
00043   mutable void (*DeleterFn)(void*);
00044   mutable const ManagedStaticBase *Next;
00045 
00046   void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
00047 public:
00048   /// isConstructed - Return true if this object has not been created yet.
00049   bool isConstructed() const { return Ptr != 0; }
00050 
00051   void destroy() const;
00052 };
00053 
00054 /// ManagedStatic - This transparently changes the behavior of global statics to
00055 /// be lazily constructed on demand (good for reducing startup times of dynamic
00056 /// libraries that link in LLVM components) and for making destruction be
00057 /// explicit through the llvm_shutdown() function call.
00058 ///
00059 template<class C>
00060 class ManagedStatic : public ManagedStaticBase {
00061 public:
00062 
00063   // Accessors.
00064   C &operator*() {
00065     void* tmp = Ptr;
00066     if (llvm_is_multithreaded()) sys::MemoryFence();
00067     if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
00068 
00069     return *static_cast<C*>(Ptr);
00070   }
00071   C *operator->() {
00072     void* tmp = Ptr;
00073     if (llvm_is_multithreaded()) sys::MemoryFence();
00074     if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
00075 
00076     return static_cast<C*>(Ptr);
00077   }
00078   const C &operator*() const {
00079     void* tmp = Ptr;
00080     if (llvm_is_multithreaded()) sys::MemoryFence();
00081     if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
00082 
00083     return *static_cast<C*>(Ptr);
00084   }
00085   const C *operator->() const {
00086     void* tmp = Ptr;
00087     if (llvm_is_multithreaded()) sys::MemoryFence();
00088     if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
00089 
00090     return static_cast<C*>(Ptr);
00091   }
00092 };
00093 
00094 template<void (*CleanupFn)(void*)>
00095 class ManagedCleanup : public ManagedStaticBase {
00096 public:
00097   void Register() { RegisterManagedStatic(0, CleanupFn); }
00098 };
00099 
00100 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
00101 void llvm_shutdown();
00102 
00103 
00104 /// llvm_shutdown_obj - This is a simple helper class that calls
00105 /// llvm_shutdown() when it is destroyed.
00106 struct llvm_shutdown_obj {
00107   llvm_shutdown_obj() { }
00108   explicit llvm_shutdown_obj(bool multithreaded) {
00109     if (multithreaded) llvm_start_multithreaded();
00110   }
00111   ~llvm_shutdown_obj() { llvm_shutdown(); }
00112 };
00113 
00114 }
00115 
00116 #endif