OLD | NEW |
1 // Copyright (c) 2011 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/base_export.h" |
12 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
13 | 13 |
14 // 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 |
15 // safety of a class. | 15 // safety of a class. |
16 // | 16 // |
17 // Example: Queue implementation non thread-safe but still usable if clients | 17 // Example: Queue implementation non thread-safe but still usable if clients |
18 // are synchronized somehow. | 18 // are synchronized somehow. |
19 // | 19 // |
20 // In this case the macro DFAKE_SCOPED_LOCK has to be | 20 // In this case the macro DFAKE_SCOPED_LOCK has to be |
21 // 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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 #define DFAKE_SCOPED_LOCK_THREAD_LOCKED(obj) ((void)0) | 124 #define DFAKE_SCOPED_LOCK_THREAD_LOCKED(obj) ((void)0) |
125 | 125 |
126 #endif | 126 #endif |
127 | 127 |
128 namespace base { | 128 namespace base { |
129 | 129 |
130 // The class ThreadCollisionWarner uses an Asserter to notify the collision | 130 // The class ThreadCollisionWarner uses an Asserter to notify the collision |
131 // AsserterBase is the interfaces and DCheckAsserter is the default asserter | 131 // AsserterBase is the interfaces and DCheckAsserter is the default asserter |
132 // used. During the unit tests is used another class that doesn't "DCHECK" | 132 // used. During the unit tests is used another class that doesn't "DCHECK" |
133 // in case of collision (check thread_collision_warner_unittests.cc) | 133 // in case of collision (check thread_collision_warner_unittests.cc) |
134 struct BASE_API AsserterBase { | 134 struct BASE_EXPORT AsserterBase { |
135 virtual ~AsserterBase() {} | 135 virtual ~AsserterBase() {} |
136 virtual void warn() = 0; | 136 virtual void warn() = 0; |
137 }; | 137 }; |
138 | 138 |
139 struct BASE_API DCheckAsserter : public AsserterBase { | 139 struct BASE_EXPORT DCheckAsserter : public AsserterBase { |
140 virtual ~DCheckAsserter() {} | 140 virtual ~DCheckAsserter() {} |
141 virtual void warn(); | 141 virtual void warn(); |
142 }; | 142 }; |
143 | 143 |
144 class BASE_API ThreadCollisionWarner { | 144 class BASE_EXPORT ThreadCollisionWarner { |
145 public: | 145 public: |
146 // The parameter asserter is there only for test purpose | 146 // The parameter asserter is there only for test purpose |
147 ThreadCollisionWarner(AsserterBase* asserter = new DCheckAsserter()) | 147 ThreadCollisionWarner(AsserterBase* asserter = new DCheckAsserter()) |
148 : valid_thread_id_(0), | 148 : valid_thread_id_(0), |
149 counter_(0), | 149 counter_(0), |
150 asserter_(asserter) {} | 150 asserter_(asserter) {} |
151 | 151 |
152 ~ThreadCollisionWarner() { | 152 ~ThreadCollisionWarner() { |
153 delete asserter_; | 153 delete asserter_; |
154 } | 154 } |
155 | 155 |
156 // This class is meant to be used through the macro | 156 // This class is meant to be used through the macro |
157 // DFAKE_SCOPED_LOCK_THREAD_LOCKED | 157 // DFAKE_SCOPED_LOCK_THREAD_LOCKED |
158 // it doesn't leave the critical section, as opposed to ScopedCheck, | 158 // it doesn't leave the critical section, as opposed to ScopedCheck, |
159 // 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 |
160 // from one thread | 160 // from one thread |
161 class BASE_API Check { | 161 class BASE_EXPORT Check { |
162 public: | 162 public: |
163 explicit Check(ThreadCollisionWarner* warner) | 163 explicit Check(ThreadCollisionWarner* warner) |
164 : warner_(warner) { | 164 : warner_(warner) { |
165 warner_->EnterSelf(); | 165 warner_->EnterSelf(); |
166 } | 166 } |
167 | 167 |
168 ~Check() {} | 168 ~Check() {} |
169 | 169 |
170 private: | 170 private: |
171 ThreadCollisionWarner* warner_; | 171 ThreadCollisionWarner* warner_; |
172 | 172 |
173 DISALLOW_COPY_AND_ASSIGN(Check); | 173 DISALLOW_COPY_AND_ASSIGN(Check); |
174 }; | 174 }; |
175 | 175 |
176 // This class is meant to be used through the macro | 176 // This class is meant to be used through the macro |
177 // DFAKE_SCOPED_LOCK | 177 // DFAKE_SCOPED_LOCK |
178 class BASE_API ScopedCheck { | 178 class BASE_EXPORT ScopedCheck { |
179 public: | 179 public: |
180 explicit ScopedCheck(ThreadCollisionWarner* warner) | 180 explicit ScopedCheck(ThreadCollisionWarner* warner) |
181 : warner_(warner) { | 181 : warner_(warner) { |
182 warner_->Enter(); | 182 warner_->Enter(); |
183 } | 183 } |
184 | 184 |
185 ~ScopedCheck() { | 185 ~ScopedCheck() { |
186 warner_->Leave(); | 186 warner_->Leave(); |
187 } | 187 } |
188 | 188 |
189 private: | 189 private: |
190 ThreadCollisionWarner* warner_; | 190 ThreadCollisionWarner* warner_; |
191 | 191 |
192 DISALLOW_COPY_AND_ASSIGN(ScopedCheck); | 192 DISALLOW_COPY_AND_ASSIGN(ScopedCheck); |
193 }; | 193 }; |
194 | 194 |
195 // This class is meant to be used through the macro | 195 // This class is meant to be used through the macro |
196 // DFAKE_SCOPED_RECURSIVE_LOCK | 196 // DFAKE_SCOPED_RECURSIVE_LOCK |
197 class BASE_API ScopedRecursiveCheck { | 197 class BASE_EXPORT ScopedRecursiveCheck { |
198 public: | 198 public: |
199 explicit ScopedRecursiveCheck(ThreadCollisionWarner* warner) | 199 explicit ScopedRecursiveCheck(ThreadCollisionWarner* warner) |
200 : warner_(warner) { | 200 : warner_(warner) { |
201 warner_->EnterSelf(); | 201 warner_->EnterSelf(); |
202 } | 202 } |
203 | 203 |
204 ~ScopedRecursiveCheck() { | 204 ~ScopedRecursiveCheck() { |
205 warner_->Leave(); | 205 warner_->Leave(); |
206 } | 206 } |
207 | 207 |
(...skipping 27 matching lines...) Expand all Loading... |
235 // 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 |
236 // DCHECK but notify the collision with something else. | 236 // DCHECK but notify the collision with something else. |
237 AsserterBase* asserter_; | 237 AsserterBase* asserter_; |
238 | 238 |
239 DISALLOW_COPY_AND_ASSIGN(ThreadCollisionWarner); | 239 DISALLOW_COPY_AND_ASSIGN(ThreadCollisionWarner); |
240 }; | 240 }; |
241 | 241 |
242 } // namespace base | 242 } // namespace base |
243 | 243 |
244 #endif // BASE_THREADING_THREAD_COLLISION_WARNER_H_ | 244 #endif // BASE_THREADING_THREAD_COLLISION_WARNER_H_ |
OLD | NEW |