LLVM  9.0.0svn
ManagedStatic.h
Go to the documentation of this file.
1 //===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the ManagedStatic class and the llvm_shutdown() function.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
14 #define LLVM_SUPPORT_MANAGEDSTATIC_H
15 
16 #include <atomic>
17 #include <cstddef>
18 
19 namespace llvm {
20 
21 /// object_creator - Helper method for ManagedStatic.
22 template <class C> struct object_creator {
23  static void *call() { return new C(); }
24 };
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 
35 // If the current compiler is MSVC 2017 or earlier, then we have to work around
36 // a bug where MSVC emits code to perform dynamic initialization even if the
37 // class has a constexpr constructor. Instead, fall back to the C++98 strategy
38 // where there are no constructors or member initializers. We can remove this
39 // when MSVC 2019 (19.20+) is our minimum supported version.
40 #if !defined(__clang__) && defined(_MSC_VER) && _MSC_VER < 1920
41 #define LLVM_AVOID_CONSTEXPR_CTOR
42 #endif
43 
44 /// ManagedStaticBase - Common base class for ManagedStatic instances.
46 protected:
47 #ifndef LLVM_AVOID_CONSTEXPR_CTOR
48  mutable std::atomic<void *> Ptr{nullptr};
49  mutable void (*DeleterFn)(void *) = nullptr;
50  mutable const ManagedStaticBase *Next = nullptr;
51 #else
52  // This should only be used as a static variable, which guarantees that this
53  // will be zero initialized.
54  mutable std::atomic<void *> Ptr;
55  mutable void (*DeleterFn)(void *);
56  mutable const ManagedStaticBase *Next;
57 #endif
58 
59  void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
60 
61 public:
62 #ifndef LLVM_AVOID_CONSTEXPR_CTOR
63  constexpr ManagedStaticBase() = default;
64 #endif
65 
66  /// isConstructed - Return true if this object has not been created yet.
67  bool isConstructed() const { return Ptr != nullptr; }
68 
69  void destroy() const;
70 };
71 
72 /// ManagedStatic - This transparently changes the behavior of global statics to
73 /// be lazily constructed on demand (good for reducing startup times of dynamic
74 /// libraries that link in LLVM components) and for making destruction be
75 /// explicit through the llvm_shutdown() function call.
76 ///
77 template <class C, class Creator = object_creator<C>,
78  class Deleter = object_deleter<C>>
80 public:
81  // Accessors.
82  C &operator*() {
83  void *Tmp = Ptr.load(std::memory_order_acquire);
84  if (!Tmp)
85  RegisterManagedStatic(Creator::call, Deleter::call);
86 
87  return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
88  }
89 
90  C *operator->() { return &**this; }
91 
92  const C &operator*() const {
93  void *Tmp = Ptr.load(std::memory_order_acquire);
94  if (!Tmp)
95  RegisterManagedStatic(Creator::call, Deleter::call);
96 
97  return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
98  }
99 
100  const C *operator->() const { return &**this; }
101 };
102 
103 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
104 void llvm_shutdown();
105 
106 /// llvm_shutdown_obj - This is a simple helper class that calls
107 /// llvm_shutdown() when it is destroyed.
109  llvm_shutdown_obj() = default;
111 };
112 
113 } // end namespace llvm
114 
115 #endif // LLVM_SUPPORT_MANAGEDSTATIC_H
uint64_t CallInst * C
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static void call(void *Ptr)
Definition: ManagedStatic.h:29
object_deleter - Helper method for ManagedStatic.
Definition: ManagedStatic.h:28
void llvm_shutdown()
llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
static void * call()
Definition: ManagedStatic.h:23
const C & operator*() const
Definition: ManagedStatic.h:92
const C * operator->() const
ManagedStaticBase - Common base class for ManagedStatic instances.
Definition: ManagedStatic.h:45
bool isConstructed() const
isConstructed - Return true if this object has not been created yet.
Definition: ManagedStatic.h:67
static void call(void *Ptr)
Definition: ManagedStatic.h:32
llvm_shutdown_obj - This is a simple helper class that calls llvm_shutdown() when it is destroyed...
#define N
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:79
void deleter(T *Ptr)
Definition: STLExtras.h:153
object_creator - Helper method for ManagedStatic.
Definition: ManagedStatic.h:22