llvm.org GIT mirror llvm / f6003a6
Add a ThreadSafeRefCountedBase A version of RefCountedBase that uses std::atomic_int to store its reference count. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202984 91177308-0d34-0410-b5e6-96231b3b80d8 Ben Langmuir 6 years ago
1 changed file(s) with 26 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
2323 #include "llvm/Support/Casting.h"
2424 #include "llvm/Support/Compiler.h"
2525 #include
26 #include
2627
2728 namespace llvm {
2829
8788 static void retain(T *obj) { obj->Retain(); }
8889 static void release(T *obj) { obj->Release(); }
8990 };
91
92 /// \brief A thread-safe version of \c llvm::RefCountedBase.
93 ///
94 /// A generic base class for objects that wish to have their lifetimes managed
95 /// using reference counts. Classes subclass \c ThreadSafeRefCountedBase to
96 /// obtain such functionality, and are typically handled with
97 /// \c IntrusiveRefCntPtr "smart pointers" which automatically handle the
98 /// management of reference counts.
99 template
100 class ThreadSafeRefCountedBase {
101 mutable std::atomic_int RefCount;
102
103 protected:
104 ThreadSafeRefCountedBase() : RefCount(0) {}
105
106 public:
107 void Retain() const { ++RefCount; }
108
109 void Release() const {
110 int NewRefCount = --RefCount;
111 assert(NewRefCount >= 0 && "Reference count was already zero.");
112 if (NewRefCount == 0)
113 delete static_cast(this);
114 }
115 };
90116
91117 //===----------------------------------------------------------------------===//
92118 /// IntrusiveRefCntPtr - A template class that implements a "smart pointer"