/src/hermes/external/llvh/include/llvh/Support/ManagedStatic.h
Line | Count | Source |
1 | | //===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | // |
10 | | // This file defines the ManagedStatic class and the llvm_shutdown() function. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #ifndef LLVM_SUPPORT_MANAGEDSTATIC_H |
15 | | #define LLVM_SUPPORT_MANAGEDSTATIC_H |
16 | | |
17 | | #include <atomic> |
18 | | #include <cstddef> |
19 | | |
20 | | namespace llvh { |
21 | | |
22 | | /// object_creator - Helper method for ManagedStatic. |
23 | | template <class C> struct object_creator { |
24 | 11 | static void *call() { return new C(); }llvh::object_creator<llvh::cl::SubCommand>::call() Line | Count | Source | 24 | 4 | static void *call() { return new C(); } |
Unexecuted instantiation: llvh::object_creator<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::call() llvh::object_creator<llvh::sys::SmartMutex<true> >::call() Line | Count | Source | 24 | 1 | static void *call() { return new C(); } |
Unexecuted instantiation: Signals.cpp:llvh::object_creator<(anonymous namespace)::FilesToRemoveCleanup>::call() Statistic.cpp:llvh::object_creator<(anonymous namespace)::StatisticInfo>::call() Line | Count | Source | 24 | 1 | static void *call() { return new C(); } |
llvh::object_creator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::call() Line | Count | Source | 24 | 2 | static void *call() { return new C(); } |
Timer.cpp:llvh::object_creator<(anonymous namespace)::Name2PairMap>::call() Line | Count | Source | 24 | 1 | static void *call() { return new C(); } |
CommandLine.cpp:llvh::object_creator<(anonymous namespace)::CommandLineParser>::call() Line | Count | Source | 24 | 2 | static void *call() { return new C(); } |
Unexecuted instantiation: Error.cpp:llvh::object_creator<(anonymous namespace)::ErrorErrorCategory>::call() |
25 | | }; |
26 | | |
27 | | /// object_deleter - Helper method for ManagedStatic. |
28 | | /// |
29 | | template <typename T> struct object_deleter { |
30 | 0 | static void call(void *Ptr) { delete (T *)Ptr; }Unexecuted instantiation: llvh::object_deleter<llvh::cl::SubCommand>::call(void*) Unexecuted instantiation: llvh::object_deleter<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::call(void*) Unexecuted instantiation: llvh::object_deleter<llvh::sys::SmartMutex<true> >::call(void*) Unexecuted instantiation: Signals.cpp:llvh::object_deleter<(anonymous namespace)::FilesToRemoveCleanup>::call(void*) Unexecuted instantiation: Statistic.cpp:llvh::object_deleter<(anonymous namespace)::StatisticInfo>::call(void*) Unexecuted instantiation: llvh::object_deleter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::call(void*) Unexecuted instantiation: llvh::object_deleter<llvh::TimerGroup>::call(void*) Unexecuted instantiation: Timer.cpp:llvh::object_deleter<(anonymous namespace)::Name2PairMap>::call(void*) Unexecuted instantiation: CommandLine.cpp:llvh::object_deleter<(anonymous namespace)::CommandLineParser>::call(void*) Unexecuted instantiation: Error.cpp:llvh::object_deleter<(anonymous namespace)::ErrorErrorCategory>::call(void*) |
31 | | }; |
32 | | template <typename T, size_t N> struct object_deleter<T[N]> { |
33 | | static void call(void *Ptr) { delete[](T *)Ptr; } |
34 | | }; |
35 | | |
36 | | // ManagedStatic must be initialized to zero, and it must *not* have a dynamic |
37 | | // initializer because managed statics are often created while running other |
38 | | // dynamic initializers. In standard C++11, the best way to accomplish this is |
39 | | // with a constexpr default constructor. However, different versions of the |
40 | | // Visual C++ compiler have had bugs where, even though the constructor may be |
41 | | // constexpr, a dynamic initializer may be emitted depending on optimization |
42 | | // settings. For the affected versions of MSVC, use the old linker |
43 | | // initialization pattern of not providing a constructor and leaving the fields |
44 | | // uninitialized. See http://llvm.org/PR41367 for details. |
45 | | #if !defined(_MSC_VER) || (_MSC_VER >= 1925) || defined(__clang__) |
46 | | #define LLVM_USE_CONSTEXPR_CTOR |
47 | | #endif |
48 | | |
49 | | /// ManagedStaticBase - Common base class for ManagedStatic instances. |
50 | | class ManagedStaticBase { |
51 | | protected: |
52 | | #ifdef LLVM_USE_CONSTEXPR_CTOR |
53 | | mutable std::atomic<void *> Ptr{}; |
54 | | mutable void (*DeleterFn)(void *) = nullptr; |
55 | | mutable const ManagedStaticBase *Next = nullptr; |
56 | | #else |
57 | | // This should only be used as a static variable, which guarantees that this |
58 | | // will be zero initialized. |
59 | | mutable std::atomic<void *> Ptr; |
60 | | mutable void (*DeleterFn)(void*); |
61 | | mutable const ManagedStaticBase *Next; |
62 | | #endif |
63 | | |
64 | | void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const; |
65 | | |
66 | | public: |
67 | | #ifdef LLVM_USE_CONSTEXPR_CTOR |
68 | | constexpr ManagedStaticBase() = default; |
69 | | #endif |
70 | | |
71 | | /// isConstructed - Return true if this object has not been created yet. |
72 | 0 | bool isConstructed() const { return Ptr != nullptr; } |
73 | | |
74 | | void destroy() const; |
75 | | }; |
76 | | |
77 | | /// ManagedStatic - This transparently changes the behavior of global statics to |
78 | | /// be lazily constructed on demand (good for reducing startup times of dynamic |
79 | | /// libraries that link in LLVM components) and for making destruction be |
80 | | /// explicit through the llvm_shutdown() function call. |
81 | | /// |
82 | | template <class C, class Creator = object_creator<C>, |
83 | | class Deleter = object_deleter<C>> |
84 | | class ManagedStatic : public ManagedStaticBase { |
85 | | public: |
86 | | // Accessors. |
87 | 161 | C &operator*() { |
88 | 161 | void *Tmp = Ptr.load(std::memory_order_acquire); |
89 | 161 | if (!Tmp) |
90 | 11 | RegisterManagedStatic(Creator::call, Deleter::call); |
91 | | |
92 | 161 | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); |
93 | 161 | } llvh::ManagedStatic<llvh::cl::SubCommand, llvh::object_creator<llvh::cl::SubCommand>, llvh::object_deleter<llvh::cl::SubCommand> >::operator*() Line | Count | Source | 87 | 82 | C &operator*() { | 88 | 82 | void *Tmp = Ptr.load(std::memory_order_acquire); | 89 | 82 | if (!Tmp) | 90 | 4 | RegisterManagedStatic(Creator::call, Deleter::call); | 91 | | | 92 | 82 | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); | 93 | 82 | } |
Unexecuted instantiation: llvh::ManagedStatic<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >, llvh::object_creator<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >, llvh::object_deleter<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > > >::operator*() llvh::ManagedStatic<llvh::sys::SmartMutex<true>, llvh::object_creator<llvh::sys::SmartMutex<true> >, llvh::object_deleter<llvh::sys::SmartMutex<true> > >::operator*() Line | Count | Source | 87 | 21 | C &operator*() { | 88 | 21 | void *Tmp = Ptr.load(std::memory_order_acquire); | 89 | 21 | if (!Tmp) | 90 | 1 | RegisterManagedStatic(Creator::call, Deleter::call); | 91 | | | 92 | 21 | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); | 93 | 21 | } |
Unexecuted instantiation: Signals.cpp:llvh::ManagedStatic<(anonymous namespace)::FilesToRemoveCleanup, llvh::object_creator<(anonymous namespace)::FilesToRemoveCleanup>, llvh::object_deleter<(anonymous namespace)::FilesToRemoveCleanup> >::operator*() Statistic.cpp:llvh::ManagedStatic<(anonymous namespace)::StatisticInfo, llvh::object_creator<(anonymous namespace)::StatisticInfo>, llvh::object_deleter<(anonymous namespace)::StatisticInfo> >::operator*() Line | Count | Source | 87 | 21 | C &operator*() { | 88 | 21 | void *Tmp = Ptr.load(std::memory_order_acquire); | 89 | 21 | if (!Tmp) | 90 | 1 | RegisterManagedStatic(Creator::call, Deleter::call); | 91 | | | 92 | 21 | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); | 93 | 21 | } |
llvh::ManagedStatic<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, llvh::object_creator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, llvh::object_deleter<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::operator*() Line | Count | Source | 87 | 2 | C &operator*() { | 88 | 2 | void *Tmp = Ptr.load(std::memory_order_acquire); | 89 | 2 | if (!Tmp) | 90 | 2 | RegisterManagedStatic(Creator::call, Deleter::call); | 91 | | | 92 | 2 | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); | 93 | 2 | } |
Unexecuted instantiation: Timer.cpp:llvh::ManagedStatic<llvh::TimerGroup, (anonymous namespace)::CreateDefaultTimerGroup, llvh::object_deleter<llvh::TimerGroup> >::operator*() Timer.cpp:llvh::ManagedStatic<(anonymous namespace)::Name2PairMap, llvh::object_creator<(anonymous namespace)::Name2PairMap>, llvh::object_deleter<(anonymous namespace)::Name2PairMap> >::operator*() Line | Count | Source | 87 | 1 | C &operator*() { | 88 | 1 | void *Tmp = Ptr.load(std::memory_order_acquire); | 89 | 1 | if (!Tmp) | 90 | 1 | RegisterManagedStatic(Creator::call, Deleter::call); | 91 | | | 92 | 1 | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); | 93 | 1 | } |
CommandLine.cpp:llvh::ManagedStatic<(anonymous namespace)::CommandLineParser, llvh::object_creator<(anonymous namespace)::CommandLineParser>, llvh::object_deleter<(anonymous namespace)::CommandLineParser> >::operator*() Line | Count | Source | 87 | 34 | C &operator*() { | 88 | 34 | void *Tmp = Ptr.load(std::memory_order_acquire); | 89 | 34 | if (!Tmp) | 90 | 2 | RegisterManagedStatic(Creator::call, Deleter::call); | 91 | | | 92 | 34 | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); | 93 | 34 | } |
Unexecuted instantiation: Error.cpp:llvh::ManagedStatic<(anonymous namespace)::ErrorErrorCategory, llvh::object_creator<(anonymous namespace)::ErrorErrorCategory>, llvh::object_deleter<(anonymous namespace)::ErrorErrorCategory> >::operator*() |
94 | | |
95 | 36 | C *operator->() { return &**this; }Unexecuted instantiation: llvh::ManagedStatic<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >, llvh::object_creator<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >, llvh::object_deleter<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > > >::operator->() Unexecuted instantiation: Statistic.cpp:llvh::ManagedStatic<(anonymous namespace)::StatisticInfo, llvh::object_creator<(anonymous namespace)::StatisticInfo>, llvh::object_deleter<(anonymous namespace)::StatisticInfo> >::operator->() Unexecuted instantiation: Timer.cpp:llvh::ManagedStatic<(anonymous namespace)::Name2PairMap, llvh::object_creator<(anonymous namespace)::Name2PairMap>, llvh::object_deleter<(anonymous namespace)::Name2PairMap> >::operator->() llvh::ManagedStatic<llvh::cl::SubCommand, llvh::object_creator<llvh::cl::SubCommand>, llvh::object_deleter<llvh::cl::SubCommand> >::operator->() Line | Count | Source | 95 | 2 | C *operator->() { return &**this; } |
CommandLine.cpp:llvh::ManagedStatic<(anonymous namespace)::CommandLineParser, llvh::object_creator<(anonymous namespace)::CommandLineParser>, llvh::object_deleter<(anonymous namespace)::CommandLineParser> >::operator->() Line | Count | Source | 95 | 34 | C *operator->() { return &**this; } |
|
96 | | |
97 | | const C &operator*() const { |
98 | | void *Tmp = Ptr.load(std::memory_order_acquire); |
99 | | if (!Tmp) |
100 | | RegisterManagedStatic(Creator::call, Deleter::call); |
101 | | |
102 | | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); |
103 | | } |
104 | | |
105 | | const C *operator->() const { return &**this; } |
106 | | }; |
107 | | |
108 | | /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables. |
109 | | void llvm_shutdown(); |
110 | | |
111 | | /// llvm_shutdown_obj - This is a simple helper class that calls |
112 | | /// llvm_shutdown() when it is destroyed. |
113 | | struct llvm_shutdown_obj { |
114 | | llvm_shutdown_obj() = default; |
115 | 0 | ~llvm_shutdown_obj() { llvm_shutdown(); } |
116 | | }; |
117 | | |
118 | | } // end namespace llvh |
119 | | |
120 | | #endif // LLVM_SUPPORT_MANAGEDSTATIC_H |