18 #ifndef ION_BASE_WEAKREFERENT_H_
19 #define ION_BASE_WEAKREFERENT_H_
23 #include "base/macros.h"
46 DCHECK(proxy_.load() == NULL);
82 SpinMutex* GetMutex()
const {
return &
mutex_; }
94 Proxy* GetProxy()
const {
96 Proxy* proxy = proxy_.load(std::memory_order_acquire);
103 proxy =
new(
GetAllocator()) Proxy(const_cast<WeakReferent*>(
this));
107 proxy->IncrementRefCount();
109 if (proxy_.compare_exchange_strong(null, proxy)) {
114 #if !defined(ION_COVERAGE) // COV_NF_START
115 proxy->DecrementRefCount();
121 DCHECK(proxy_.load() != NULL) <<
"proxy should not be NULL after CAS";
131 void OnZeroRefCount()
const override {
135 Proxy* proxy = proxy_.exchange(NULL, std::memory_order_acquire);
146 DCHECK(proxy->Get() != NULL) <<
"SetOrphaned() already called.";
149 proxy->SetOrphaned();
151 proxy->DecrementRefCount();
157 std::atomic<int>& GetRefCountRef() {
return ref_count_; }
160 mutable std::atomic<Proxy*> proxy_;
181 template <
typename T>
193 : proxy_(other.proxy_) {}
234 if (referent->GetRefCountRef()++) {
242 --referent->GetRefCountRef();
262 return p.proxy_ == proxy_;
267 return p.proxy_ != proxy_;
274 if (WeakReferent::Proxy* proxy = proxy_.Get()) {
275 if (
WeakReferent* referent = proxy->GetUnsynchronized()) {
276 return referent->GetRefCount();
284 void Reset(
T* new_referent) {
288 if (new_referent->GetRefCount() == 0)
289 LOG(
ERROR) <<
"Input pointer was not owned by a ReferentPtr and will "
292 proxy_ = ref_p->GetProxy();
296 mutable ReferentPtr<WeakReferent::Proxy>::Type proxy_;
302 #endif // ION_BASE_WEAKREFERENT_H_
int GetUnderlyingRefCountUnsynchronized() const
This function returns the reference count of the underlying referent that points to.
ReferentPtr< T >::Type ReferentPtrType
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
void Reset()
Set this WeakPointer to not refer to anything.
WeakReferentPtr(const ReferentPtrType &ref_ptr)
Shareable is an abstract base class for any object that can be shared via the SharedPtr class...
Thread-safe abstract base class.
GenericLockGuard< SpinMutex > SpinLockGuard
Convenient typedefs for SpinMutex.
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
A LockGuard locks a mutex when created, and unlocks it when destroyed.
Allocatable is an abstract base class for classes whose memory is managed by an Allocator.
bool operator==(const WeakReferentPtr &p) const
The equality operator returns true if the proxies are the same.
WeakReferentPtr< T > & operator=(const ReferentPtrType &p)
Allow assignment from a SharedPtr of the same type.
WeakReferentPtr(const WeakReferentPtr &other)
Copy constructor.
Copyright 2016 Google Inc.
Abstract base class that inherits from Referent, and adds the ability for instances to be referenced ...
#define DCHECK_EQ(val1, val2)
bool operator!=(const WeakReferentPtr &p) const
The inequality operator returns true if the proxies differ.
~WeakReferent() override
The destructor is protected to disallow creating instances on the stack.
ReferentPtrType Acquire() const
Attempts to construct a ReferentPtr of the reference.
port::Mutex mutex_
Protects shared access to the Allocator and FT_Library.
const AllocatorPtr & GetAllocator() const
Returns the Allocator that was used for the instance.
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
A WeakReferentPtr is a weak reference to an instance of some class derived from Referent.