//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_SUPPORT_MANAGED_STATIC_H
-#define LLVM_SUPPORT_MANAGED_STATIC_H
+#ifndef LLVM_SUPPORT_MANAGEDSTATIC_H
+#define LLVM_SUPPORT_MANAGEDSTATIC_H
-#include "llvm/System/Atomic.h"
+#include "llvm/Support/Atomic.h"
+#include "llvm/Support/Threading.h"
+#include "llvm/Support/Valgrind.h"
namespace llvm {
-/// object_deleter - Helper method for ManagedStatic.
-///
+/// object_creator - Helper method for ManagedStatic.
template<class C>
-void object_deleter(void *Ptr) {
- delete (C*)Ptr;
+void* object_creator() {
+ return new C();
}
+/// object_deleter - Helper method for ManagedStatic.
+///
+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; }
+};
+
/// ManagedStaticBase - Common base class for ManagedStatic instances.
class ManagedStaticBase {
protected:
- mutable sys::cas_flag InitFlag;
-
// 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;
- void RegisterManagedStatic(void *ObjPtr, void (*deleter)(void*)) const;
+ void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const;
public:
/// isConstructed - Return true if this object has not been created yet.
- bool isConstructed() const { return Ptr != 0; }
+ bool isConstructed() const { return Ptr != nullptr; }
void destroy() const;
};
// Accessors.
C &operator*() {
- sys::cas_flag OldFlag = sys::CompareAndSwap(&InitFlag, 1, 0);
- if (OldFlag == 0) {
- LazyInit();
- sys::MemoryFence();
- InitFlag = 2;
- } else if (OldFlag == 1)
- while (OldFlag == 1) ;
-
+ 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->() {
- sys::cas_flag OldFlag = sys::CompareAndSwap(&InitFlag, 1, 0);
- if (OldFlag == 0) {
- LazyInit();
- sys::MemoryFence();
- InitFlag = 2;
- } else if (OldFlag == 1)
- while (OldFlag == 1) ;
-
+ 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 {
- sys::cas_flag OldFlag = sys::CompareAndSwap(&InitFlag, 1, 0);
- if (OldFlag == 0) {
- LazyInit();
- sys::MemoryFence();
- InitFlag = 2;
- } else if (OldFlag == 1)
- while (InitFlag == 1) ;
-
+ 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 {
- sys::cas_flag OldFlag = sys::CompareAndSwap(&InitFlag, 1, 0);
- if (OldFlag == 0) {
- LazyInit();
- sys::MemoryFence();
- InitFlag = 2;
- } else if (OldFlag == 1)
- while (OldFlag == 1) ;
-
- return static_cast<C*>(Ptr);
- }
+ void* tmp = Ptr;
+ if (llvm_is_multithreaded()) sys::MemoryFence();
+ if (!tmp) RegisterManagedStatic(object_creator<C>, object_deleter<C>::call);
+ TsanHappensAfter(this);
-public:
- void LazyInit() const {
- RegisterManagedStatic(new C(), object_deleter<C>);
+ return static_cast<C*>(Ptr);
}
};
-template<void (*CleanupFn)(void*)>
-class ManagedCleanup : public ManagedStaticBase {
-public:
- void Register() { RegisterManagedStatic(0, CleanupFn); }
-};
-
-
/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
void llvm_shutdown();
-
/// llvm_shutdown_obj - This is a simple helper class that calls
/// llvm_shutdown() when it is destroyed.
struct llvm_shutdown_obj {
- llvm_shutdown_obj() {}
+ llvm_shutdown_obj() { }
~llvm_shutdown_obj() { llvm_shutdown(); }
};