Page Speed Optimization Libraries
1.13.35.1
|
#include "categorized_refcount.h"
Public Member Functions | |
CategorizedRefcount (ObjectType *object) | |
void | set_mutex (AbstractMutex *mutex) |
void | AddRef (EnumType category) |
void | AddRefMutexHeld (EnumType category) |
void | ReleaseRef (EnumType category) |
void | ReleaseRefMutexHeld (EnumType category) |
int | QueryCountMutexHeld (EnumType category) const |
GoogleString | DebugString () const |
GoogleString | DebugStringMutexHeld () const |
void | DCheckAllCountsZero () |
void | DCheckAllCountsZeroMutexHeld () |
This class helps manage a reference count stored in an object where references can be classified into separate types, to further check their use and help in debugging. You would normally store an instance of CategorizedRefcount in the object being managed.
There are the following requirements on ObjectType: void LastRefRemoved(); ///< called when refcount goes to 0, with /// mutex_ held. StringPiece RefCategoryName(EnumType); const int kNumRefCategories which bounds the EnumType
For example, you might have something like this (omitting RefCategoryName implementation, which is only needed for DebugString()):
class AsyncDoerOfThings() { public: AsyncDoerOfThings() : ref_counts_(this) { ... ref_counts_.set_mutex(mutex_.get()); }
void ref() { ref_counts_.AddRef(kRefExternal); } void deref() { ref_counts_.ReleaseRef(kRefExternal); } void AsyncOp() { ref_counts_.AddRef(kRefInternal); DoSomeRpcOp(this, &AsyncDoerOfThings::AsyncOpComplete); }
private: void AsyncOpComplete() { ref_counts_.ReleaseRef(kRefInternal); } void LastRefRemoved() { delete this; }
enum RefCategory { kRefExternal, kRefInternal, kNumRefCategories }; friend class CategorizedRefcount<AsyncDoerOfThings, RefCategory>; CategorizedRefcount<AsyncDoerOfThings, RefCategory> ref_counts_;
};
|
inlineexplicit |
Note: set_mutex must be called before calling any other method on this class.
|
inline |
QueryCount w/o mutex held externally makes no sense, since there would be no way of using the data.
|
inline |
Sets the mutex that should be held when manipulating reference count of this object. Does not take ownership.