Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
categorized_refcount.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_UTIL_CATEGORIZED_REFCOUNT_H_
20 #define PAGESPEED_KERNEL_UTIL_CATEGORIZED_REFCOUNT_H_
21 
22 #include "base/logging.h"
27 
28 namespace net_instaweb {
29 
75 template<typename ObjectType, typename EnumType>
77  public:
82  explicit CategorizedRefcount(ObjectType* object)
83  : total_refcount_(0), object_(object), mutex_(NULL) {
84  for (int i = 0; i < ObjectType::kNumRefCategories; ++i) {
85  ref_counts_[i] = 0;
86  }
87  }
88 
91  void set_mutex(AbstractMutex* mutex) {
92  mutex_ = mutex;
93  }
94 
95  void AddRef(EnumType category) {
96  ScopedMutex hold(mutex_);
97  AddRefMutexHeld(category);
98  }
99 
100  void AddRefMutexHeld(EnumType category) {
101  mutex_->DCheckLocked();
102  DCHECK_LE(0, category);
103  DCHECK_LT(category, ObjectType::kNumRefCategories);
104  ++ref_counts_[category];
105  ++total_refcount_;
106  }
107 
108  void ReleaseRef(EnumType category) {
109  ScopedMutex hold(mutex_);
110  ReleaseRefMutexHeld(category);
111  }
112 
113  void ReleaseRefMutexHeld(EnumType category) {
114  mutex_->DCheckLocked();
115  DCHECK_LE(0, category);
116  DCHECK_LT(category, ObjectType::kNumRefCategories);
117  --ref_counts_[category];
118  DCHECK_GE(ref_counts_[category], 0) << category;
119  --total_refcount_;
120  if (total_refcount_ == 0) {
121  object_->LastRefRemoved();
122  }
123  }
124 
127 
128  int QueryCountMutexHeld(EnumType category) const {
129  return ref_counts_[category];
130  }
131 
132  GoogleString DebugString() const {
133  ScopedMutex hold(mutex_);
134  return DebugStringMutexHeld();
135  }
136 
137  GoogleString DebugStringMutexHeld() const {
138  mutex_->DCheckLocked();
139 
140  GoogleString out;
141  for (int i = 0; i < ObjectType::kNumRefCategories; ++i) {
142  StrAppend(&out, "\t", object_->RefCategoryName(static_cast<EnumType>(i)),
143  ": ", IntegerToString(ref_counts_[i]));
144  }
145  return out;
146  }
147 
148  void DCheckAllCountsZero() {
149  ScopedMutex hold(mutex_);
150  DCheckAllCountsZeroMutexHeld();
151  }
152 
153  void DCheckAllCountsZeroMutexHeld() {
154  mutex_->DCheckLocked();
155  DCHECK_EQ(0, total_refcount_);
156  for (int i = 0; i < ObjectType::kNumRefCategories; ++i) {
157  DCHECK_EQ(0, ref_counts_[i]);
158  }
159  }
160 
161  private:
162  int ref_counts_[ObjectType::kNumRefCategories];
163  int total_refcount_;
164  ObjectType* object_;
165  AbstractMutex* mutex_;
166 
167 };
168 
169 }
170 
171 #endif
int QueryCountMutexHeld(EnumType category) const
Definition: categorized_refcount.h:128
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