2006-09-28 00:31:55 +00:00
|
|
|
//===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-09-28 00:31:55 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the ManagedStatic class and the llvm_shutdown() function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
|
|
|
|
#define LLVM_SUPPORT_MANAGEDSTATIC_H
|
2006-09-28 00:31:55 +00:00
|
|
|
|
2014-03-03 18:02:34 +00:00
|
|
|
#include "llvm/Support/Atomic.h"
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "llvm/Support/Threading.h"
|
2011-11-14 20:50:16 +00:00
|
|
|
#include "llvm/Support/Valgrind.h"
|
2009-05-20 00:39:20 +00:00
|
|
|
|
2006-09-28 00:31:55 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2009-05-20 00:39:20 +00:00
|
|
|
/// object_creator - Helper method for ManagedStatic.
|
|
|
|
template<class C>
|
|
|
|
void* object_creator() {
|
|
|
|
return new C();
|
|
|
|
}
|
|
|
|
|
2006-09-28 00:31:55 +00:00
|
|
|
/// object_deleter - Helper method for ManagedStatic.
|
|
|
|
///
|
2009-07-09 17:26:16 +00:00
|
|
|
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; }
|
|
|
|
};
|
2006-09-28 00:31:55 +00: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.
|
|
|
|
mutable void *Ptr;
|
|
|
|
mutable void (*DeleterFn)(void*);
|
|
|
|
mutable const ManagedStaticBase *Next;
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2009-05-20 00:39:20 +00:00
|
|
|
void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
|
2006-09-28 00:31:55 +00:00
|
|
|
public:
|
2009-02-20 22:51:36 +00:00
|
|
|
/// isConstructed - Return true if this object has not been created yet.
|
2014-04-07 04:17:22 +00:00
|
|
|
bool isConstructed() const { return Ptr != nullptr; }
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2006-09-28 00:31:55 +00: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.
|
|
|
|
///
|
|
|
|
template<class C>
|
|
|
|
class ManagedStatic : public ManagedStaticBase {
|
2009-05-15 08:22:07 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
// Accessors.
|
|
|
|
C &operator*() {
|
2009-05-20 00:39:20 +00:00
|
|
|
void* tmp = Ptr;
|
2014-03-03 18:02:34 +00:00
|
|
|
if (llvm_is_multithreaded()) sys::MemoryFence();
|
2009-07-09 17:26:16 +00:00
|
|
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
2011-11-14 20:50:16 +00:00
|
|
|
TsanHappensAfter(this);
|
2009-05-20 00:39:20 +00:00
|
|
|
|
2006-09-28 00:31:55 +00:00
|
|
|
return *static_cast<C*>(Ptr);
|
|
|
|
}
|
|
|
|
C *operator->() {
|
2009-05-20 00:39:20 +00:00
|
|
|
void* tmp = Ptr;
|
2014-03-03 18:02:34 +00:00
|
|
|
if (llvm_is_multithreaded()) sys::MemoryFence();
|
2009-07-09 17:26:16 +00:00
|
|
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
2011-11-14 20:50:16 +00:00
|
|
|
TsanHappensAfter(this);
|
2009-05-20 00:39:20 +00:00
|
|
|
|
2006-09-28 00:31:55 +00:00
|
|
|
return static_cast<C*>(Ptr);
|
|
|
|
}
|
|
|
|
const C &operator*() const {
|
2009-05-20 00:39:20 +00:00
|
|
|
void* tmp = Ptr;
|
2014-03-03 18:02:34 +00:00
|
|
|
if (llvm_is_multithreaded()) sys::MemoryFence();
|
2009-07-09 17:26:16 +00:00
|
|
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
2011-11-14 20:50:16 +00:00
|
|
|
TsanHappensAfter(this);
|
2009-05-20 00:39:20 +00:00
|
|
|
|
2006-09-28 00:31:55 +00:00
|
|
|
return *static_cast<C*>(Ptr);
|
|
|
|
}
|
|
|
|
const C *operator->() const {
|
2009-05-20 00:39:20 +00:00
|
|
|
void* tmp = Ptr;
|
2014-03-03 18:02:34 +00:00
|
|
|
if (llvm_is_multithreaded()) sys::MemoryFence();
|
2009-07-09 17:26:16 +00:00
|
|
|
if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
|
2011-11-14 20:50:16 +00:00
|
|
|
TsanHappensAfter(this);
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2009-05-20 00:39:20 +00:00
|
|
|
return static_cast<C*>(Ptr);
|
2006-09-28 00:31:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
|
|
|
|
void llvm_shutdown();
|
|
|
|
|
2006-12-06 01:01:14 +00:00
|
|
|
/// llvm_shutdown_obj - This is a simple helper class that calls
|
|
|
|
/// llvm_shutdown() when it is destroyed.
|
|
|
|
struct llvm_shutdown_obj {
|
2009-05-20 00:39:20 +00:00
|
|
|
llvm_shutdown_obj() { }
|
2006-12-06 01:01:14 +00:00
|
|
|
~llvm_shutdown_obj() { llvm_shutdown(); }
|
|
|
|
};
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2006-09-28 00:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|