OLD | NEW |
| (Empty) |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_THREAD_COLLISION_WARNER_H_ | |
6 #define BASE_THREAD_COLLISION_WARNER_H_ | |
7 #pragma once | |
8 | |
9 #include <memory> | |
10 | |
11 #include "base/atomicops.h" | |
12 | |
13 // A helper class alongside macros to be used to verify assumptions about thread | |
14 // safety of a class. | |
15 // | |
16 // Example: Queue implementation non thread-safe but still usable if clients | |
17 // are synchronized somehow. | |
18 // | |
19 // 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 // noone else is still inside the pop/push | |
22 // | |
23 // class NonThreadSafeQueue { | |
24 // public: | |
25 // ... | |
26 // void push(int) { DFAKE_SCOPED_LOCK(push_pop_); ... } | |
27 // int pop() { DFAKE_SCOPED_LOCK(push_pop_); ... } | |
28 // ... | |
29 // private: | |
30 // DFAKE_MUTEX(push_pop_); | |
31 // }; | |
32 // | |
33 // | |
34 // Example: Queue implementation non thread-safe but still usable if clients | |
35 // are synchronized somehow, it calls a method to "protect" from | |
36 // a "protected" method | |
37 // | |
38 // In this case the macro DFAKE_SCOPED_RECURSIVE_LOCK | |
39 // has to be used, it checks that if a thread is inside the push/pop | |
40 // then noone else is still inside the pop/push | |
41 // | |
42 // class NonThreadSafeQueue { | |
43 // public: | |
44 // void push(int) { | |
45 // DFAKE_SCOPED_LOCK(push_pop_); | |
46 // ... | |
47 // } | |
48 // int pop() { | |
49 // DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_); | |
50 // bar(); | |
51 // ... | |
52 // } | |
53 // void bar() { DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_); ... } | |
54 // ... | |
55 // private: | |
56 // DFAKE_MUTEX(push_pop_); | |
57 // }; | |
58 // | |
59 // | |
60 // Example: Queue implementation not usable even if clients are synchronized, | |
61 // so only one thread in the class life cycle can use the two members | |
62 // push/pop. | |
63 // | |
64 // In this case the macro DFAKE_SCOPED_LOCK_THREAD_LOCKED pins the | |
65 // specified | |
66 // critical section the first time a thread enters push or pop, from | |
67 // that time on only that thread is allowed to execute push or pop. | |
68 // | |
69 // class NonThreadSafeQueue { | |
70 // public: | |
71 // ... | |
72 // void push(int) { DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_); ... } | |
73 // int pop() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_); ... } | |
74 // ... | |
75 // private: | |
76 // DFAKE_MUTEX(push_pop_); | |
77 // }; | |
78 // | |
79 // | |
80 // Example: Class that has to be contructed/destroyed on same thread, it has | |
81 // a "shareable" method (with external syncronization) and a not | |
82 // shareable method (even with external synchronization). | |
83 // | |
84 // In this case 3 Critical sections have to be defined | |
85 // | |
86 // class ExoticClass { | |
87 // public: | |
88 // ExoticClass() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... } | |
89 // ~ExoticClass() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... } | |
90 // | |
91 // void Shareable() { DFAKE_SCOPED_LOCK(shareable_section_); ... } | |
92 // void NotShareable() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... } | |
93 // ... | |
94 // private: | |
95 // DFAKE_MUTEX(ctor_dtor_); | |
96 // DFAKE_MUTEX(shareable_section_); | |
97 // }; | |
98 | |
99 | |
100 #if !defined(NDEBUG) | |
101 | |
102 // Defines a class member that acts like a mutex. It is used only as a | |
103 // verification tool. | |
104 #define DFAKE_MUTEX(obj) \ | |
105 mutable base::ThreadCollisionWarner obj | |
106 // Asserts the call is never called simultaneously in two threads. Used at | |
107 // member function scope. | |
108 #define DFAKE_SCOPED_LOCK(obj) \ | |
109 base::ThreadCollisionWarner::ScopedCheck s_check_##obj(&obj) | |
110 // Asserts the call is never called simultaneously in two threads. Used at | |
111 // member function scope. Same as DFAKE_SCOPED_LOCK but allows recursive locks. | |
112 #define DFAKE_SCOPED_RECURSIVE_LOCK(obj) \ | |
113 base::ThreadCollisionWarner::ScopedRecursiveCheck sr_check_##obj(&obj) | |
114 // Asserts the code is always executed in the same thread. | |
115 #define DFAKE_SCOPED_LOCK_THREAD_LOCKED(obj) \ | |
116 base::ThreadCollisionWarner::Check check_##obj(&obj) | |
117 | |
118 #else | |
119 | |
120 #define DFAKE_MUTEX(obj) | |
121 #define DFAKE_SCOPED_LOCK(obj) ((void)0) | |
122 #define DFAKE_SCOPED_RECURSIVE_LOCK(obj) ((void)0) | |
123 #define DFAKE_SCOPED_LOCK_THREAD_LOCKED(obj) ((void)0) | |
124 | |
125 #endif | |
126 | |
127 namespace base { | |
128 | |
129 // The class ThreadCollisionWarner uses an Asserter to notify the collision | |
130 // AsserterBase is the interfaces and DCheckAsserter is the default asserter | |
131 // used. During the unit tests is used another class that doesn't "DCHECK" | |
132 // in case of collision (check thread_collision_warner_unittests.cc) | |
133 struct AsserterBase { | |
134 virtual ~AsserterBase() {} | |
135 virtual void warn() = 0; | |
136 }; | |
137 | |
138 struct DCheckAsserter : public AsserterBase { | |
139 virtual ~DCheckAsserter() {} | |
140 virtual void warn(); | |
141 }; | |
142 | |
143 class ThreadCollisionWarner { | |
144 public: | |
145 // The parameter asserter is there only for test purpose | |
146 ThreadCollisionWarner(AsserterBase* asserter = new DCheckAsserter()) | |
147 : valid_thread_id_(0), | |
148 counter_(0), | |
149 asserter_(asserter) {} | |
150 | |
151 ~ThreadCollisionWarner() { | |
152 delete asserter_; | |
153 } | |
154 | |
155 // This class is meant to be used through the macro | |
156 // DFAKE_SCOPED_LOCK_THREAD_LOCKED | |
157 // 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 // from one thread | |
160 class Check { | |
161 public: | |
162 explicit Check(ThreadCollisionWarner* warner) | |
163 : warner_(warner) { | |
164 warner_->EnterSelf(); | |
165 } | |
166 | |
167 ~Check() {} | |
168 | |
169 private: | |
170 ThreadCollisionWarner* warner_; | |
171 | |
172 DISALLOW_COPY_AND_ASSIGN(Check); | |
173 }; | |
174 | |
175 // This class is meant to be used through the macro | |
176 // DFAKE_SCOPED_LOCK | |
177 class ScopedCheck { | |
178 public: | |
179 explicit ScopedCheck(ThreadCollisionWarner* warner) | |
180 : warner_(warner) { | |
181 warner_->Enter(); | |
182 } | |
183 | |
184 ~ScopedCheck() { | |
185 warner_->Leave(); | |
186 } | |
187 | |
188 private: | |
189 ThreadCollisionWarner* warner_; | |
190 | |
191 DISALLOW_COPY_AND_ASSIGN(ScopedCheck); | |
192 }; | |
193 | |
194 // This class is meant to be used through the macro | |
195 // DFAKE_SCOPED_RECURSIVE_LOCK | |
196 class ScopedRecursiveCheck { | |
197 public: | |
198 explicit ScopedRecursiveCheck(ThreadCollisionWarner* warner) | |
199 : warner_(warner) { | |
200 warner_->EnterSelf(); | |
201 } | |
202 | |
203 ~ScopedRecursiveCheck() { | |
204 warner_->Leave(); | |
205 } | |
206 | |
207 private: | |
208 ThreadCollisionWarner* warner_; | |
209 | |
210 DISALLOW_COPY_AND_ASSIGN(ScopedRecursiveCheck); | |
211 }; | |
212 | |
213 private: | |
214 // This method stores the current thread identifier and does a DCHECK | |
215 // if a another thread has already done it, it is safe if same thread | |
216 // calls this multiple time (recursion allowed). | |
217 void EnterSelf(); | |
218 | |
219 // Same as EnterSelf but recursion is not allowed. | |
220 void Enter(); | |
221 | |
222 // Removes the thread_id stored in order to allow other threads to | |
223 // call EnterSelf or Enter. | |
224 void Leave(); | |
225 | |
226 // This stores the thread id that is inside the critical section, if the | |
227 // value is 0 then no thread is inside. | |
228 volatile subtle::Atomic32 valid_thread_id_; | |
229 | |
230 // Counter to trace how many time a critical section was "pinned" | |
231 // (when allowed) in order to unpin it when counter_ reaches 0. | |
232 volatile subtle::Atomic32 counter_; | |
233 | |
234 // Here only for class unit tests purpose, during the test I need to not | |
235 // DCHECK but notify the collision with something else. | |
236 AsserterBase* asserter_; | |
237 | |
238 DISALLOW_COPY_AND_ASSIGN(ThreadCollisionWarner); | |
239 }; | |
240 | |
241 } // namespace base | |
242 | |
243 #endif // BASE_THREAD_COLLISION_WARNER_H_ | |
OLD | NEW |