OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_THREADING_THREAD_COLLISION_WARNER_H_ | 5 #ifndef BASE_THREADING_THREAD_COLLISION_WARNER_H_ |
6 #define BASE_THREADING_THREAD_COLLISION_WARNER_H_ | 6 #define BASE_THREADING_THREAD_COLLISION_WARNER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
| 11 #include "base/base_api.h" |
11 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
12 | 13 |
13 // A helper class alongside macros to be used to verify assumptions about thread | 14 // A helper class alongside macros to be used to verify assumptions about thread |
14 // safety of a class. | 15 // safety of a class. |
15 // | 16 // |
16 // Example: Queue implementation non thread-safe but still usable if clients | 17 // Example: Queue implementation non thread-safe but still usable if clients |
17 // are synchronized somehow. | 18 // are synchronized somehow. |
18 // | 19 // |
19 // In this case the macro DFAKE_SCOPED_LOCK has to be | 20 // In this case the macro DFAKE_SCOPED_LOCK has to be |
20 // used, it checks that if a thread is inside the push/pop then | 21 // used, it checks that if a thread is inside the push/pop then |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 struct AsserterBase { | 134 struct AsserterBase { |
134 virtual ~AsserterBase() {} | 135 virtual ~AsserterBase() {} |
135 virtual void warn() = 0; | 136 virtual void warn() = 0; |
136 }; | 137 }; |
137 | 138 |
138 struct DCheckAsserter : public AsserterBase { | 139 struct DCheckAsserter : public AsserterBase { |
139 virtual ~DCheckAsserter() {} | 140 virtual ~DCheckAsserter() {} |
140 virtual void warn(); | 141 virtual void warn(); |
141 }; | 142 }; |
142 | 143 |
143 class ThreadCollisionWarner { | 144 class BASE_API ThreadCollisionWarner { |
144 public: | 145 public: |
145 // The parameter asserter is there only for test purpose | 146 // The parameter asserter is there only for test purpose |
146 ThreadCollisionWarner(AsserterBase* asserter = new DCheckAsserter()) | 147 ThreadCollisionWarner(AsserterBase* asserter = new DCheckAsserter()) |
147 : valid_thread_id_(0), | 148 : valid_thread_id_(0), |
148 counter_(0), | 149 counter_(0), |
149 asserter_(asserter) {} | 150 asserter_(asserter) {} |
150 | 151 |
151 ~ThreadCollisionWarner() { | 152 ~ThreadCollisionWarner() { |
152 delete asserter_; | 153 delete asserter_; |
153 } | 154 } |
154 | 155 |
155 // This class is meant to be used through the macro | 156 // This class is meant to be used through the macro |
156 // DFAKE_SCOPED_LOCK_THREAD_LOCKED | 157 // DFAKE_SCOPED_LOCK_THREAD_LOCKED |
157 // it doesn't leave the critical section, as opposed to ScopedCheck, | 158 // it doesn't leave the critical section, as opposed to ScopedCheck, |
158 // because the critical section being pinned is allowed to be used only | 159 // because the critical section being pinned is allowed to be used only |
159 // from one thread | 160 // from one thread |
160 class Check { | 161 class BASE_API Check { |
161 public: | 162 public: |
162 explicit Check(ThreadCollisionWarner* warner) | 163 explicit Check(ThreadCollisionWarner* warner) |
163 : warner_(warner) { | 164 : warner_(warner) { |
164 warner_->EnterSelf(); | 165 warner_->EnterSelf(); |
165 } | 166 } |
166 | 167 |
167 ~Check() {} | 168 ~Check() {} |
168 | 169 |
169 private: | 170 private: |
170 ThreadCollisionWarner* warner_; | 171 ThreadCollisionWarner* warner_; |
171 | 172 |
172 DISALLOW_COPY_AND_ASSIGN(Check); | 173 DISALLOW_COPY_AND_ASSIGN(Check); |
173 }; | 174 }; |
174 | 175 |
175 // This class is meant to be used through the macro | 176 // This class is meant to be used through the macro |
176 // DFAKE_SCOPED_LOCK | 177 // DFAKE_SCOPED_LOCK |
177 class ScopedCheck { | 178 class BASE_API ScopedCheck { |
178 public: | 179 public: |
179 explicit ScopedCheck(ThreadCollisionWarner* warner) | 180 explicit ScopedCheck(ThreadCollisionWarner* warner) |
180 : warner_(warner) { | 181 : warner_(warner) { |
181 warner_->Enter(); | 182 warner_->Enter(); |
182 } | 183 } |
183 | 184 |
184 ~ScopedCheck() { | 185 ~ScopedCheck() { |
185 warner_->Leave(); | 186 warner_->Leave(); |
186 } | 187 } |
187 | 188 |
188 private: | 189 private: |
189 ThreadCollisionWarner* warner_; | 190 ThreadCollisionWarner* warner_; |
190 | 191 |
191 DISALLOW_COPY_AND_ASSIGN(ScopedCheck); | 192 DISALLOW_COPY_AND_ASSIGN(ScopedCheck); |
192 }; | 193 }; |
193 | 194 |
194 // This class is meant to be used through the macro | 195 // This class is meant to be used through the macro |
195 // DFAKE_SCOPED_RECURSIVE_LOCK | 196 // DFAKE_SCOPED_RECURSIVE_LOCK |
196 class ScopedRecursiveCheck { | 197 class BASE_API ScopedRecursiveCheck { |
197 public: | 198 public: |
198 explicit ScopedRecursiveCheck(ThreadCollisionWarner* warner) | 199 explicit ScopedRecursiveCheck(ThreadCollisionWarner* warner) |
199 : warner_(warner) { | 200 : warner_(warner) { |
200 warner_->EnterSelf(); | 201 warner_->EnterSelf(); |
201 } | 202 } |
202 | 203 |
203 ~ScopedRecursiveCheck() { | 204 ~ScopedRecursiveCheck() { |
204 warner_->Leave(); | 205 warner_->Leave(); |
205 } | 206 } |
206 | 207 |
(...skipping 27 matching lines...) Expand all Loading... |
234 // Here only for class unit tests purpose, during the test I need to not | 235 // Here only for class unit tests purpose, during the test I need to not |
235 // DCHECK but notify the collision with something else. | 236 // DCHECK but notify the collision with something else. |
236 AsserterBase* asserter_; | 237 AsserterBase* asserter_; |
237 | 238 |
238 DISALLOW_COPY_AND_ASSIGN(ThreadCollisionWarner); | 239 DISALLOW_COPY_AND_ASSIGN(ThreadCollisionWarner); |
239 }; | 240 }; |
240 | 241 |
241 } // namespace base | 242 } // namespace base |
242 | 243 |
243 #endif // BASE_THREADING_THREAD_COLLISION_WARNER_H_ | 244 #endif // BASE_THREADING_THREAD_COLLISION_WARNER_H_ |
OLD | NEW |