1 //===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the ManagedStatic class and the llvm_shutdown() function.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
14 #define LLVM_SUPPORT_MANAGEDSTATIC_H
21 /// object_creator - Helper method for ManagedStatic.
22 template <class C
> struct object_creator
{
23 static void *call() { return new C(); }
26 /// object_deleter - Helper method for ManagedStatic.
28 template <typename T
> struct object_deleter
{
29 static void call(void *Ptr
) { delete (T
*)Ptr
; }
31 template <typename T
, size_t N
> struct object_deleter
<T
[N
]> {
32 static void call(void *Ptr
) { delete[](T
*)Ptr
; }
35 // ManagedStatic must be initialized to zero, and it must *not* have a dynamic
36 // initializer because managed statics are often created while running other
37 // dynamic initializers. In standard C++11, the best way to accomplish this is
38 // with a constexpr default constructor. However, different versions of the
39 // Visual C++ compiler have had bugs where, even though the constructor may be
40 // constexpr, a dynamic initializer may be emitted depending on optimization
41 // settings. For the affected versions of MSVC, use the old linker
42 // initialization pattern of not providing a constructor and leaving the fields
44 #if !defined(_MSC_VER) || defined(__clang__)
45 #define LLVM_USE_CONSTEXPR_CTOR
48 /// ManagedStaticBase - Common base class for ManagedStatic instances.
49 class ManagedStaticBase
{
51 #ifdef LLVM_USE_CONSTEXPR_CTOR
52 mutable std::atomic
<void *> Ptr
{};
53 mutable void (*DeleterFn
)(void *) = nullptr;
54 mutable const ManagedStaticBase
*Next
= nullptr;
56 // This should only be used as a static variable, which guarantees that this
57 // will be zero initialized.
58 mutable std::atomic
<void *> Ptr
;
59 mutable void (*DeleterFn
)(void *);
60 mutable const ManagedStaticBase
*Next
;
63 void RegisterManagedStatic(void *(*creator
)(), void (*deleter
)(void*)) const;
66 #ifdef LLVM_USE_CONSTEXPR_CTOR
67 constexpr ManagedStaticBase() = default;
70 /// isConstructed - Return true if this object has not been created yet.
71 bool isConstructed() const { return Ptr
!= nullptr; }
76 /// ManagedStatic - This transparently changes the behavior of global statics to
77 /// be lazily constructed on demand (good for reducing startup times of dynamic
78 /// libraries that link in LLVM components) and for making destruction be
79 /// explicit through the llvm_shutdown() function call.
81 template <class C
, class Creator
= object_creator
<C
>,
82 class Deleter
= object_deleter
<C
>>
83 class ManagedStatic
: public ManagedStaticBase
{
87 void *Tmp
= Ptr
.load(std::memory_order_acquire
);
89 RegisterManagedStatic(Creator::call
, Deleter::call
);
91 return *static_cast<C
*>(Ptr
.load(std::memory_order_relaxed
));
94 C
*operator->() { return &**this; }
96 const C
&operator*() const {
97 void *Tmp
= Ptr
.load(std::memory_order_acquire
);
99 RegisterManagedStatic(Creator::call
, Deleter::call
);
101 return *static_cast<C
*>(Ptr
.load(std::memory_order_relaxed
));
104 const C
*operator->() const { return &**this; }
107 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
108 void llvm_shutdown();
110 /// llvm_shutdown_obj - This is a simple helper class that calls
111 /// llvm_shutdown() when it is destroyed.
112 struct llvm_shutdown_obj
{
113 llvm_shutdown_obj() = default;
114 ~llvm_shutdown_obj() { llvm_shutdown(); }
117 } // end namespace llvm
119 #endif // LLVM_SUPPORT_MANAGEDSTATIC_H