forked from OSchip/llvm-project
Add a ThreadSafeRefCountedBase
A version of RefCountedBase that uses std::atomic_int to store its reference count. llvm-svn: 202984
This commit is contained in:
parent
921f5a529e
commit
7590be3cfa
|
@ -24,6 +24,7 @@
|
||||||
#include "llvm/Support/Casting.h"
|
#include "llvm/Support/Casting.h"
|
||||||
#include "llvm/Support/Compiler.h"
|
#include "llvm/Support/Compiler.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
|
|
||||||
|
@ -89,6 +90,31 @@ namespace llvm {
|
||||||
static void release(T *obj) { obj->Release(); }
|
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"
|
/// IntrusiveRefCntPtr - A template class that implements a "smart pointer"
|
||||||
/// that assumes the wrapped object has a reference count associated
|
/// that assumes the wrapped object has a reference count associated
|
||||||
|
|
Loading…
Reference in New Issue