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.
5 #ifndef BASE_THREAD_COLLISION_WARNER_H_
6 #define BASE_THREAD_COLLISION_WARNER_H_
11 #include "base/atomicops.h"
13 // A helper class alongside macros to be used to verify assumptions about thread
16 // Example: Queue implementation non thread-safe but still usable if clients
17 // are synchronized somehow.
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
23 // class NonThreadSafeQueue {
26 // void push(int) { DFAKE_SCOPED_LOCK(push_pop_); ... }
27 // int pop() { DFAKE_SCOPED_LOCK(push_pop_); ... }
30 // DFAKE_MUTEX(push_pop_);
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
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
42 // class NonThreadSafeQueue {
45 // DFAKE_SCOPED_LOCK(push_pop_);
49 // DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_);
53 // void bar() { DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_); ... }
56 // DFAKE_MUTEX(push_pop_);
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
64 // In this case the macro DFAKE_SCOPED_LOCK_THREAD_LOCKED pins the
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.
69 // class NonThreadSafeQueue {
72 // void push(int) { DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_); ... }
73 // int pop() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_); ... }
76 // DFAKE_MUTEX(push_pop_);
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).
84 // In this case 3 Critical sections have to be defined
86 // class ExoticClass {
88 // ExoticClass() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... }
89 // ~ExoticClass() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... }
91 // void Shareable() { DFAKE_SCOPED_LOCK(shareable_section_); ... }
92 // void NotShareable() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... }
95 // DFAKE_MUTEX(ctor_dtor_);
96 // DFAKE_MUTEX(shareable_section_);
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)
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)
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;
138 struct DCheckAsserter
: public AsserterBase
{
139 virtual ~DCheckAsserter() {}
143 class ThreadCollisionWarner
{
145 // The parameter asserter is there only for test purpose
146 ThreadCollisionWarner(AsserterBase
* asserter
= new DCheckAsserter())
147 : valid_thread_id_(0),
149 asserter_(asserter
) {}
151 ~ThreadCollisionWarner() {
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
162 explicit Check(ThreadCollisionWarner
* warner
)
164 warner_
->EnterSelf();
170 ThreadCollisionWarner
* warner_
;
172 DISALLOW_COPY_AND_ASSIGN(Check
);
175 // This class is meant to be used through the macro
179 explicit ScopedCheck(ThreadCollisionWarner
* warner
)
189 ThreadCollisionWarner
* warner_
;
191 DISALLOW_COPY_AND_ASSIGN(ScopedCheck
);
194 // This class is meant to be used through the macro
195 // DFAKE_SCOPED_RECURSIVE_LOCK
196 class ScopedRecursiveCheck
{
198 explicit ScopedRecursiveCheck(ThreadCollisionWarner
* warner
)
200 warner_
->EnterSelf();
203 ~ScopedRecursiveCheck() {
208 ThreadCollisionWarner
* warner_
;
210 DISALLOW_COPY_AND_ASSIGN(ScopedRecursiveCheck
);
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).
219 // Same as EnterSelf but recursion is not allowed.
222 // Removes the thread_id stored in order to allow other threads to
223 // call EnterSelf or Enter.
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_
;
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_
;
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_
;
238 DISALLOW_COPY_AND_ASSIGN(ThreadCollisionWarner
);
243 #endif // BASE_THREAD_COLLISION_WARNER_H_