19 #ifndef PAGESPEED_KERNEL_UTIL_CATEGORIZED_REFCOUNT_H_
20 #define PAGESPEED_KERNEL_UTIL_CATEGORIZED_REFCOUNT_H_
22 #include "base/logging.h"
28 namespace net_instaweb {
75 template<
typename ObjectType,
typename EnumType>
83 : total_refcount_(0), object_(object), mutex_(NULL) {
84 for (
int i = 0; i < ObjectType::kNumRefCategories; ++i) {
95 void AddRef(EnumType category) {
97 AddRefMutexHeld(category);
100 void AddRefMutexHeld(EnumType category) {
102 DCHECK_LE(0, category);
103 DCHECK_LT(category, ObjectType::kNumRefCategories);
104 ++ref_counts_[category];
108 void ReleaseRef(EnumType category) {
110 ReleaseRefMutexHeld(category);
113 void ReleaseRefMutexHeld(EnumType category) {
115 DCHECK_LE(0, category);
116 DCHECK_LT(category, ObjectType::kNumRefCategories);
117 --ref_counts_[category];
118 DCHECK_GE(ref_counts_[category], 0) << category;
120 if (total_refcount_ == 0) {
121 object_->LastRefRemoved();
129 return ref_counts_[category];
134 return DebugStringMutexHeld();
141 for (
int i = 0; i < ObjectType::kNumRefCategories; ++i) {
142 StrAppend(&out,
"\t", object_->RefCategoryName(static_cast<EnumType>(i)),
143 ": ", IntegerToString(ref_counts_[i]));
148 void DCheckAllCountsZero() {
150 DCheckAllCountsZeroMutexHeld();
153 void DCheckAllCountsZeroMutexHeld() {
155 DCHECK_EQ(0, total_refcount_);
156 for (
int i = 0; i < ObjectType::kNumRefCategories; ++i) {
157 DCHECK_EQ(0, ref_counts_[i]);
162 int ref_counts_[ObjectType::kNumRefCategories];
165 AbstractMutex* mutex_;
int QueryCountMutexHeld(EnumType category) const
Definition: categorized_refcount.h:128
virtual void DCheckLocked()
Abstract interface for implementing a mutex.
Definition: abstract_mutex.h:28
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
#define ScopedMutex(x)
Definition: abstract_mutex.h:69
Helper class for lexically scoped mutexing.
Definition: abstract_mutex.h:46
Definition: categorized_refcount.h:76
void set_mutex(AbstractMutex *mutex)
Definition: categorized_refcount.h:91
CategorizedRefcount(ObjectType *object)
Definition: categorized_refcount.h:82