Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:32

0001 //===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the ManagedStatic class and the llvm_shutdown() function.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
0014 #define LLVM_SUPPORT_MANAGEDSTATIC_H
0015 
0016 #include <atomic>
0017 #include <cstddef>
0018 
0019 namespace llvm {
0020 
0021 /// object_creator - Helper method for ManagedStatic.
0022 template <class C> struct object_creator {
0023   static void *call() { return new C(); }
0024 };
0025 
0026 /// object_deleter - Helper method for ManagedStatic.
0027 ///
0028 template <typename T> struct object_deleter {
0029   static void call(void *Ptr) { delete (T *)Ptr; }
0030 };
0031 template <typename T, size_t N> struct object_deleter<T[N]> {
0032   static void call(void *Ptr) { delete[](T *)Ptr; }
0033 };
0034 
0035 // ManagedStatic must be initialized to zero, and it must *not* have a dynamic
0036 // initializer because managed statics are often created while running other
0037 // dynamic initializers. In standard C++11, the best way to accomplish this is
0038 // with a constexpr default constructor. However, different versions of the
0039 // Visual C++ compiler have had bugs where, even though the constructor may be
0040 // constexpr, a dynamic initializer may be emitted depending on optimization
0041 // settings. For the affected versions of MSVC, use the old linker
0042 // initialization pattern of not providing a constructor and leaving the fields
0043 // uninitialized. See http://llvm.org/PR41367 for details.
0044 #if !defined(_MSC_VER) || (_MSC_VER >= 1925) || defined(__clang__)
0045 #define LLVM_USE_CONSTEXPR_CTOR
0046 #endif
0047 
0048 /// ManagedStaticBase - Common base class for ManagedStatic instances.
0049 class ManagedStaticBase {
0050 protected:
0051 #ifdef LLVM_USE_CONSTEXPR_CTOR
0052   mutable std::atomic<void *> Ptr{};
0053   mutable void (*DeleterFn)(void *) = nullptr;
0054   mutable const ManagedStaticBase *Next = nullptr;
0055 #else
0056   // This should only be used as a static variable, which guarantees that this
0057   // will be zero initialized.
0058   mutable std::atomic<void *> Ptr;
0059   mutable void (*DeleterFn)(void *);
0060   mutable const ManagedStaticBase *Next;
0061 #endif
0062 
0063   void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
0064 
0065 public:
0066 #ifdef LLVM_USE_CONSTEXPR_CTOR
0067   constexpr ManagedStaticBase() = default;
0068 #endif
0069 
0070   /// isConstructed - Return true if this object has not been created yet.
0071   bool isConstructed() const { return Ptr != nullptr; }
0072 
0073   void destroy() const;
0074 };
0075 
0076 /// ManagedStatic - This transparently changes the behavior of global statics to
0077 /// be lazily constructed on demand (good for reducing startup times of dynamic
0078 /// libraries that link in LLVM components) and for making destruction be
0079 /// explicit through the llvm_shutdown() function call.
0080 ///
0081 template <class C, class Creator = object_creator<C>,
0082           class Deleter = object_deleter<C>>
0083 class ManagedStatic : public ManagedStaticBase {
0084 public:
0085   // Accessors.
0086   C &operator*() {
0087     void *Tmp = Ptr.load(std::memory_order_acquire);
0088     if (!Tmp)
0089       RegisterManagedStatic(Creator::call, Deleter::call);
0090 
0091     return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
0092   }
0093 
0094   C *operator->() { return &**this; }
0095 
0096   const C &operator*() const {
0097     void *Tmp = Ptr.load(std::memory_order_acquire);
0098     if (!Tmp)
0099       RegisterManagedStatic(Creator::call, Deleter::call);
0100 
0101     return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
0102   }
0103 
0104   const C *operator->() const { return &**this; }
0105 
0106   // Extract the instance, leaving the ManagedStatic uninitialized. The
0107   // user is then responsible for the lifetime of the returned instance.
0108   C *claim() {
0109     return static_cast<C *>(Ptr.exchange(nullptr));
0110   }
0111 };
0112 
0113 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
0114 void llvm_shutdown();
0115 
0116 /// llvm_shutdown_obj - This is a simple helper class that calls
0117 /// llvm_shutdown() when it is destroyed.
0118 struct llvm_shutdown_obj {
0119   llvm_shutdown_obj() = default;
0120   ~llvm_shutdown_obj() { llvm_shutdown(); }
0121 };
0122 
0123 } // end namespace llvm
0124 
0125 #endif // LLVM_SUPPORT_MANAGEDSTATIC_H