ManagedStatic.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the ManagedStatic class and the llvm_shutdown() function.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
  18. #define LLVM_SUPPORT_MANAGEDSTATIC_H
  19. #include <atomic>
  20. #include <cstddef>
  21. namespace llvm {
  22. /// object_creator - Helper method for ManagedStatic.
  23. template <class C> struct object_creator {
  24. static void *call() { return new C(); }
  25. };
  26. /// object_deleter - Helper method for ManagedStatic.
  27. ///
  28. template <typename T> struct object_deleter {
  29. static void call(void *Ptr) { delete (T *)Ptr; }
  30. };
  31. template <typename T, size_t N> struct object_deleter<T[N]> {
  32. static void call(void *Ptr) { delete[](T *)Ptr; }
  33. };
  34. // ManagedStatic must be initialized to zero, and it must *not* have a dynamic
  35. // initializer because managed statics are often created while running other
  36. // dynamic initializers. In standard C++11, the best way to accomplish this is
  37. // with a constexpr default constructor. However, different versions of the
  38. // Visual C++ compiler have had bugs where, even though the constructor may be
  39. // constexpr, a dynamic initializer may be emitted depending on optimization
  40. // settings. For the affected versions of MSVC, use the old linker
  41. // initialization pattern of not providing a constructor and leaving the fields
  42. // uninitialized. See http://llvm.org/PR41367 for details.
  43. #if !defined(_MSC_VER) || (_MSC_VER >= 1925) || defined(__clang__)
  44. #define LLVM_USE_CONSTEXPR_CTOR
  45. #endif
  46. /// ManagedStaticBase - Common base class for ManagedStatic instances.
  47. class ManagedStaticBase {
  48. protected:
  49. #ifdef LLVM_USE_CONSTEXPR_CTOR
  50. mutable std::atomic<void *> Ptr{};
  51. mutable void (*DeleterFn)(void *) = nullptr;
  52. mutable const ManagedStaticBase *Next = nullptr;
  53. #else
  54. // This should only be used as a static variable, which guarantees that this
  55. // will be zero initialized.
  56. mutable std::atomic<void *> Ptr;
  57. mutable void (*DeleterFn)(void *);
  58. mutable const ManagedStaticBase *Next;
  59. #endif
  60. void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
  61. public:
  62. #ifdef LLVM_USE_CONSTEXPR_CTOR
  63. constexpr ManagedStaticBase() = default;
  64. #endif
  65. /// isConstructed - Return true if this object has not been created yet.
  66. bool isConstructed() const { return Ptr != nullptr; }
  67. void destroy() const;
  68. };
  69. /// ManagedStatic - This transparently changes the behavior of global statics to
  70. /// be lazily constructed on demand (good for reducing startup times of dynamic
  71. /// libraries that link in LLVM components) and for making destruction be
  72. /// explicit through the llvm_shutdown() function call.
  73. ///
  74. template <class C, class Creator = object_creator<C>,
  75. class Deleter = object_deleter<C>>
  76. class ManagedStatic : public ManagedStaticBase {
  77. public:
  78. // Accessors.
  79. C &operator*() {
  80. void *Tmp = Ptr.load(std::memory_order_acquire);
  81. if (!Tmp)
  82. RegisterManagedStatic(Creator::call, Deleter::call);
  83. return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
  84. }
  85. C *operator->() { return &**this; }
  86. const C &operator*() const {
  87. void *Tmp = Ptr.load(std::memory_order_acquire);
  88. if (!Tmp)
  89. RegisterManagedStatic(Creator::call, Deleter::call);
  90. return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
  91. }
  92. const C *operator->() const { return &**this; }
  93. // Extract the instance, leaving the ManagedStatic uninitialized. The
  94. // user is then responsible for the lifetime of the returned instance.
  95. C *claim() {
  96. return static_cast<C *>(Ptr.exchange(nullptr));
  97. }
  98. };
  99. /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
  100. void llvm_shutdown();
  101. /// llvm_shutdown_obj - This is a simple helper class that calls
  102. /// llvm_shutdown() when it is destroyed.
  103. struct llvm_shutdown_obj {
  104. llvm_shutdown_obj() = default;
  105. ~llvm_shutdown_obj() { llvm_shutdown(); }
  106. };
  107. } // end namespace llvm
  108. #endif // LLVM_SUPPORT_MANAGEDSTATIC_H
  109. #ifdef __GNUC__
  110. #pragma GCC diagnostic pop
  111. #endif