2006-09-28 02:31:55 +02:00
|
|
|
//===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-09-28 02:31:55 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the ManagedStatic class and the llvm_shutdown() function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 18:26:38 +02:00
|
|
|
#ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
|
|
|
|
#define LLVM_SUPPORT_MANAGEDSTATIC_H
|
2006-09-28 02:31:55 +02:00
|
|
|
|
2016-06-29 17:04:07 +02:00
|
|
|
#include <atomic>
|
|
|
|
#include <cstddef>
|
2009-05-20 02:39:20 +02:00
|
|
|
|
2006-09-28 02:31:55 +02:00
|
|
|
namespace llvm {
|
|
|
|
|
2009-05-20 02:39:20 +02:00
|
|
|
/// object_creator - Helper method for ManagedStatic.
|
2017-05-29 22:56:27 +02:00
|
|
|
template <class C> struct object_creator {
|
|
|
|
static void *call() { return new C(); }
|
|
|
|
};
|
2009-05-20 02:39:20 +02:00
|
|
|
|
2006-09-28 02:31:55 +02:00
|
|
|
/// object_deleter - Helper method for ManagedStatic.
|
|
|
|
///
|
2017-05-29 20:00:33 +02:00
|
|
|
template <typename T> struct object_deleter {
|
2016-02-25 23:05:19 +01:00
|
|
|
static void call(void *Ptr) { delete (T *)Ptr; }
|
2009-07-09 19:26:16 +02:00
|
|
|
};
|
2017-05-29 20:00:33 +02:00
|
|
|
template <typename T, size_t N> struct object_deleter<T[N]> {
|
2016-02-25 23:05:19 +01:00
|
|
|
static void call(void *Ptr) { delete[](T *)Ptr; }
|
2009-07-09 19:26:16 +02:00
|
|
|
};
|
2006-09-28 02:31:55 +02:00
|
|
|
|
|
|
|
/// ManagedStaticBase - Common base class for ManagedStatic instances.
|
|
|
|
class ManagedStaticBase {
|
|
|
|
protected:
|
|
|
|
// This should only be used as a static variable, which guarantees that this
|
|
|
|
// will be zero initialized.
|
2016-06-29 17:04:07 +02:00
|
|
|
mutable std::atomic<void *> Ptr;
|
2006-09-28 02:31:55 +02:00
|
|
|
mutable void (*DeleterFn)(void*);
|
|
|
|
mutable const ManagedStaticBase *Next;
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2009-05-20 02:39:20 +02:00
|
|
|
void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
|
2016-12-01 23:13:24 +01:00
|
|
|
|
2006-09-28 02:31:55 +02:00
|
|
|
public:
|
2009-02-20 23:51:36 +01:00
|
|
|
/// isConstructed - Return true if this object has not been created yet.
|
2014-04-07 06:17:22 +02:00
|
|
|
bool isConstructed() const { return Ptr != nullptr; }
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2006-09-28 02:31:55 +02:00
|
|
|
void destroy() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// ManagedStatic - This transparently changes the behavior of global statics to
|
|
|
|
/// be lazily constructed on demand (good for reducing startup times of dynamic
|
|
|
|
/// libraries that link in LLVM components) and for making destruction be
|
|
|
|
/// explicit through the llvm_shutdown() function call.
|
|
|
|
///
|
2017-05-29 22:56:27 +02:00
|
|
|
template <class C, class Creator = object_creator<C>,
|
|
|
|
class Deleter = object_deleter<C>>
|
2006-09-28 02:31:55 +02:00
|
|
|
class ManagedStatic : public ManagedStaticBase {
|
2009-05-15 10:22:07 +02:00
|
|
|
public:
|
|
|
|
// Accessors.
|
|
|
|
C &operator*() {
|
2016-06-29 17:04:07 +02:00
|
|
|
void *Tmp = Ptr.load(std::memory_order_acquire);
|
|
|
|
if (!Tmp)
|
2017-05-29 22:56:27 +02:00
|
|
|
RegisterManagedStatic(Creator::call, Deleter::call);
|
2009-05-20 02:39:20 +02:00
|
|
|
|
2016-06-29 17:04:07 +02:00
|
|
|
return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
|
2006-09-28 02:31:55 +02:00
|
|
|
}
|
2009-05-20 02:39:20 +02:00
|
|
|
|
2016-06-29 17:04:07 +02:00
|
|
|
C *operator->() { return &**this; }
|
|
|
|
|
2006-09-28 02:31:55 +02:00
|
|
|
const C &operator*() const {
|
2016-06-29 17:04:07 +02:00
|
|
|
void *Tmp = Ptr.load(std::memory_order_acquire);
|
|
|
|
if (!Tmp)
|
2017-05-29 22:56:27 +02:00
|
|
|
RegisterManagedStatic(Creator::call, Deleter::call);
|
2009-05-20 02:39:20 +02:00
|
|
|
|
2016-06-29 17:04:07 +02:00
|
|
|
return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
|
2006-09-28 02:31:55 +02:00
|
|
|
}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2016-06-29 17:04:07 +02:00
|
|
|
const C *operator->() const { return &**this; }
|
2006-09-28 02:31:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
|
|
|
|
void llvm_shutdown();
|
|
|
|
|
2006-12-06 02:01:14 +01:00
|
|
|
/// llvm_shutdown_obj - This is a simple helper class that calls
|
|
|
|
/// llvm_shutdown() when it is destroyed.
|
|
|
|
struct llvm_shutdown_obj {
|
2016-12-01 23:13:24 +01:00
|
|
|
llvm_shutdown_obj() = default;
|
2006-12-06 02:01:14 +01:00
|
|
|
~llvm_shutdown_obj() { llvm_shutdown(); }
|
|
|
|
};
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2016-12-01 23:13:24 +01:00
|
|
|
} // end namespace llvm
|
2006-09-28 02:31:55 +02:00
|
|
|
|
2016-12-01 23:13:24 +01:00
|
|
|
#endif // LLVM_SUPPORT_MANAGEDSTATIC_H
|