Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | List of all members
net_instaweb::CategorizedRefcount< ObjectType, EnumType > Class Template Reference

#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 ()
 

Detailed Description

template<typename ObjectType, typename EnumType>
class net_instaweb::CategorizedRefcount< ObjectType, EnumType >

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_;

};

Todo:
TODO(morlovich): Consider having a cap per kind, too? Some are meant to be 0-1 only.

Constructor & Destructor Documentation

template<typename ObjectType, typename EnumType>
net_instaweb::CategorizedRefcount< ObjectType, EnumType >::CategorizedRefcount ( ObjectType *  object)
inlineexplicit

Note: set_mutex must be called before calling any other method on this class.

Todo:
TODO(jud): Instead of holding the mutex in this class, pass in the mutex to each function so that thread safety annotation can be used.

Member Function Documentation

template<typename ObjectType, typename EnumType>
int net_instaweb::CategorizedRefcount< ObjectType, EnumType >::QueryCountMutexHeld ( EnumType  category) const
inline

QueryCount w/o mutex held externally makes no sense, since there would be no way of using the data.

template<typename ObjectType, typename EnumType>
void net_instaweb::CategorizedRefcount< ObjectType, EnumType >::set_mutex ( AbstractMutex mutex)
inline

Sets the mutex that should be held when manipulating reference count of this object. Does not take ownership.


The documentation for this class was generated from the following file: