|
@@ -10,42 +10,77 @@
|
|
// This file defines the ManagedStatic class and the llvm_shutdown() function.
|
|
// This file defines the ManagedStatic class and the llvm_shutdown() function.
|
|
//
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
+//
|
|
|
|
+// The following changes have been backported from upstream llvm:
|
|
|
|
+//
|
|
|
|
+// 56349e8b6d85 Fix for memory leak reported by Valgrind
|
|
|
|
+// 928071ae4ef5 [Support] Replace sys::Mutex with their standard equivalents.
|
|
|
|
+// 3d5360a4398b Replace llvm::MutexGuard/UniqueLock with their standard equivalents
|
|
|
|
+// c06a470fc843 Try once more to ensure constant initializaton of ManagedStatics
|
|
|
|
+// 41fe3a54c261 Ensure that ManagedStatic is constant initialized in MSVC 2017 & 2019
|
|
|
|
+// 74de08031f5d [ManagedStatic] Avoid putting function pointers in template args.
|
|
|
|
+// f65e4ce2c48a [ADT, Support, TableGen] Fix some Clang-tidy modernize-use-default and Include What You Use warnings; other minor fixes (NFC).
|
|
|
|
+// 832d04207810 [ManagedStatic] Reimplement double-checked locking with std::atomic.
|
|
|
|
+// dd1463823a0c This is yet another attempt to re-instate r220932 as discussed in D19271.
|
|
|
|
+//
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
|
|
#ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
|
|
#define LLVM_SUPPORT_MANAGEDSTATIC_H
|
|
#define LLVM_SUPPORT_MANAGEDSTATIC_H
|
|
|
|
|
|
-#include "llvm/Support/Atomic.h"
|
|
|
|
-#include "llvm/Support/Threading.h"
|
|
|
|
-#include "llvm/Support/Valgrind.h"
|
|
|
|
|
|
+#include <atomic>
|
|
|
|
+#include <cstddef>
|
|
|
|
|
|
namespace llvm {
|
|
namespace llvm {
|
|
|
|
|
|
/// object_creator - Helper method for ManagedStatic.
|
|
/// object_creator - Helper method for ManagedStatic.
|
|
-template<class C>
|
|
|
|
-void* object_creator() {
|
|
|
|
- return new C();
|
|
|
|
-}
|
|
|
|
|
|
+template <class C> struct object_creator {
|
|
|
|
+ static void *call() { return new C(); }
|
|
|
|
+};
|
|
|
|
|
|
/// object_deleter - Helper method for ManagedStatic.
|
|
/// object_deleter - Helper method for ManagedStatic.
|
|
///
|
|
///
|
|
-template<typename T> struct object_deleter {
|
|
|
|
- static void call(void * Ptr) { delete (T*)Ptr; }
|
|
|
|
|
|
+template <typename T> struct object_deleter {
|
|
|
|
+ static void call(void *Ptr) { delete (T *)Ptr; }
|
|
};
|
|
};
|
|
-template<typename T, size_t N> struct object_deleter<T[N]> {
|
|
|
|
- static void call(void * Ptr) { delete[] (T*)Ptr; }
|
|
|
|
|
|
+template <typename T, size_t N> struct object_deleter<T[N]> {
|
|
|
|
+ static void call(void *Ptr) { delete[](T *)Ptr; }
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+// ManagedStatic must be initialized to zero, and it must *not* have a dynamic
|
|
|
|
+// initializer because managed statics are often created while running other
|
|
|
|
+// dynamic initializers. In standard C++11, the best way to accomplish this is
|
|
|
|
+// with a constexpr default constructor. However, different versions of the
|
|
|
|
+// Visual C++ compiler have had bugs where, even though the constructor may be
|
|
|
|
+// constexpr, a dynamic initializer may be emitted depending on optimization
|
|
|
|
+// settings. For the affected versions of MSVC, use the old linker
|
|
|
|
+// initialization pattern of not providing a constructor and leaving the fields
|
|
|
|
+// uninitialized. See http://llvm.org/PR41367 for details.
|
|
|
|
+#if !defined(_MSC_VER) || (_MSC_VER >= 1925) || defined(__clang__)
|
|
|
|
+#define LLVM_USE_CONSTEXPR_CTOR
|
|
|
|
+#endif
|
|
|
|
+
|
|
/// ManagedStaticBase - Common base class for ManagedStatic instances.
|
|
/// ManagedStaticBase - Common base class for ManagedStatic instances.
|
|
class ManagedStaticBase {
|
|
class ManagedStaticBase {
|
|
protected:
|
|
protected:
|
|
|
|
+#ifdef LLVM_USE_CONSTEXPR_CTOR
|
|
|
|
+ mutable std::atomic<void *> Ptr{};
|
|
|
|
+ mutable void (*DeleterFn)(void *) = nullptr;
|
|
|
|
+ mutable const ManagedStaticBase *Next = nullptr;
|
|
|
|
+#else
|
|
// This should only be used as a static variable, which guarantees that this
|
|
// This should only be used as a static variable, which guarantees that this
|
|
// will be zero initialized.
|
|
// will be zero initialized.
|
|
- mutable void *Ptr;
|
|
|
|
- mutable void (*DeleterFn)(void*);
|
|
|
|
|
|
+ mutable std::atomic<void *> Ptr;
|
|
|
|
+ mutable void (*DeleterFn)(void *);
|
|
mutable const ManagedStaticBase *Next;
|
|
mutable const ManagedStaticBase *Next;
|
|
|
|
+#endif
|
|
|
|
|
|
void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
|
|
void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
|
|
|
|
+
|
|
public:
|
|
public:
|
|
|
|
+#ifdef LLVM_USE_CONSTEXPR_CTOR
|
|
|
|
+ constexpr ManagedStaticBase() = default;
|
|
|
|
+#endif
|
|
|
|
+
|
|
/// isConstructed - Return true if this object has not been created yet.
|
|
/// isConstructed - Return true if this object has not been created yet.
|
|
bool isConstructed() const { return Ptr != nullptr; }
|
|
bool isConstructed() const { return Ptr != nullptr; }
|
|
|
|
|
|
@@ -57,42 +92,35 @@ public:
|
|
/// libraries that link in LLVM components) and for making destruction be
|
|
/// libraries that link in LLVM components) and for making destruction be
|
|
/// explicit through the llvm_shutdown() function call.
|
|
/// explicit through the llvm_shutdown() function call.
|
|
///
|
|
///
|
|
-template<class C>
|
|
|
|
|
|
+template <class C, class Creator = object_creator<C>,
|
|
|
|
+ class Deleter = object_deleter<C>>
|
|
class ManagedStatic : public ManagedStaticBase {
|
|
class ManagedStatic : public ManagedStaticBase {
|
|
public:
|
|
public:
|
|
-
|
|
|
|
// Accessors.
|
|
// Accessors.
|
|
C &operator*() {
|
|
C &operator*() {
|
|
- void* tmp = Ptr;
|
|
|
|
- if (llvm_is_multithreaded()) sys::MemoryFence();
|
|
|
|
- if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
|
|
|
- TsanHappensAfter(this);
|
|
|
|
|
|
+ void *Tmp = Ptr.load(std::memory_order_acquire);
|
|
|
|
+ if (!Tmp)
|
|
|
|
+ RegisterManagedStatic(Creator::call, Deleter::call);
|
|
|
|
|
|
- return *static_cast<C*>(Ptr);
|
|
|
|
|
|
+ return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
|
|
}
|
|
}
|
|
- C *operator->() {
|
|
|
|
- void* tmp = Ptr;
|
|
|
|
- if (llvm_is_multithreaded()) sys::MemoryFence();
|
|
|
|
- if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
|
|
|
- TsanHappensAfter(this);
|
|
|
|
|
|
|
|
- return static_cast<C*>(Ptr);
|
|
|
|
- }
|
|
|
|
|
|
+ C *operator->() { return &**this; }
|
|
|
|
+
|
|
const C &operator*() const {
|
|
const C &operator*() const {
|
|
- void* tmp = Ptr;
|
|
|
|
- if (llvm_is_multithreaded()) sys::MemoryFence();
|
|
|
|
- if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
|
|
|
- TsanHappensAfter(this);
|
|
|
|
|
|
+ void *Tmp = Ptr.load(std::memory_order_acquire);
|
|
|
|
+ if (!Tmp)
|
|
|
|
+ RegisterManagedStatic(Creator::call, Deleter::call);
|
|
|
|
|
|
- return *static_cast<C*>(Ptr);
|
|
|
|
|
|
+ return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
|
|
}
|
|
}
|
|
- const C *operator->() const {
|
|
|
|
- void* tmp = Ptr;
|
|
|
|
- if (llvm_is_multithreaded()) sys::MemoryFence();
|
|
|
|
- if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
|
|
|
- TsanHappensAfter(this);
|
|
|
|
|
|
|
|
- return static_cast<C*>(Ptr);
|
|
|
|
|
|
+ const C *operator->() const { return &**this; }
|
|
|
|
+
|
|
|
|
+ // Extract the instance, leaving the ManagedStatic uninitialized. The
|
|
|
|
+ // user is then responsible for the lifetime of the returned instance.
|
|
|
|
+ C *claim() {
|
|
|
|
+ return static_cast<C *>(Ptr.exchange(nullptr));
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
@@ -102,10 +130,10 @@ void llvm_shutdown();
|
|
/// llvm_shutdown_obj - This is a simple helper class that calls
|
|
/// llvm_shutdown_obj - This is a simple helper class that calls
|
|
/// llvm_shutdown() when it is destroyed.
|
|
/// llvm_shutdown() when it is destroyed.
|
|
struct llvm_shutdown_obj {
|
|
struct llvm_shutdown_obj {
|
|
- llvm_shutdown_obj() { }
|
|
|
|
|
|
+ llvm_shutdown_obj() = default;
|
|
~llvm_shutdown_obj() { llvm_shutdown(); }
|
|
~llvm_shutdown_obj() { llvm_shutdown(); }
|
|
};
|
|
};
|
|
|
|
|
|
-}
|
|
|
|
|
|
+} // end namespace llvm
|
|
|
|
|
|
-#endif
|
|
|
|
|
|
+#endif // LLVM_SUPPORT_MANAGEDSTATIC_H
|