Add ARM big endian Target (armeb, thumbeb)
[oota-llvm.git] / include / llvm / ADT / IntrusiveRefCntPtr.h
index 6ab6d1b0e58324f11de479b463f588bec078a206..aad139296b0059ad780c133eb0387f5fbe015316 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/Compiler.h"
+#include <atomic>
 #include <memory>
 
 namespace llvm {
@@ -88,6 +89,31 @@ namespace llvm {
     static void retain(T *obj) { obj->Retain(); }
     static void release(T *obj) { obj->Release(); }
   };
+
+/// \brief A thread-safe version of \c llvm::RefCountedBase.
+///
+/// A generic base class for objects that wish to have their lifetimes managed
+/// using reference counts. Classes subclass \c ThreadSafeRefCountedBase to
+/// obtain such functionality, and are typically handled with
+/// \c IntrusiveRefCntPtr "smart pointers" which automatically handle the
+/// management of reference counts.
+template <class Derived>
+class ThreadSafeRefCountedBase {
+  mutable std::atomic<int> RefCount;
+
+protected:
+  ThreadSafeRefCountedBase() : RefCount(0) {}
+
+public:
+  void Retain() const { ++RefCount; }
+
+  void Release() const {
+    int NewRefCount = --RefCount;
+    assert(NewRefCount >= 0 && "Reference count was already zero.");
+    if (NewRefCount == 0)
+      delete static_cast<const Derived*>(this);
+  }
+};
   
 //===----------------------------------------------------------------------===//
 /// IntrusiveRefCntPtr - A template class that implements a "smart pointer"