diff options
| author | Ben Langmuir <blangmuir@apple.com> | 2014-03-05 15:24:33 +0000 |
|---|---|---|
| committer | Ben Langmuir <blangmuir@apple.com> | 2014-03-05 15:24:33 +0000 |
| commit | 7590be3cfa505775989633fe64d92fa2939f4641 (patch) | |
| tree | c02f5a1d56d4979a5ece4297aa6954cda09af87f /llvm | |
| parent | 921f5a529e568cf77679f2645a0c22c8b4780365 (diff) | |
| download | bcm5719-llvm-7590be3cfa505775989633fe64d92fa2939f4641.tar.gz bcm5719-llvm-7590be3cfa505775989633fe64d92fa2939f4641.zip | |
Add a ThreadSafeRefCountedBase
A version of RefCountedBase that uses std::atomic_int to store its
reference count.
llvm-svn: 202984
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/include/llvm/ADT/IntrusiveRefCntPtr.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h index 6ab6d1b0e58..aa7ccdcf9c7 100644 --- a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h +++ b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h @@ -24,6 +24,7 @@ #include "llvm/Support/Casting.h" #include "llvm/Support/Compiler.h" #include <memory> +#include <atomic> 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" |

