1 // Copyright (c) 2012 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_THREADING_THREAD_COLLISION_WARNER_H_
6 #define BASE_THREADING_THREAD_COLLISION_WARNER_H_
10 #include "base/atomicops.h"
11 #include "base/base_export.h"
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
15 // A helper class alongside macros to be used to verify assumptions about thread
18 // Example: Queue implementation non thread-safe but still usable if clients
19 // are synchronized somehow.
21 // In this case the macro DFAKE_SCOPED_LOCK has to be
22 // used, it checks that if a thread is inside the push/pop then
23 // noone else is still inside the pop/push
25 // class NonThreadSafeQueue {
28 // void push(int) { DFAKE_SCOPED_LOCK(push_pop_); ... }
29 // int pop() { DFAKE_SCOPED_LOCK(push_pop_); ... }
32 // DFAKE_MUTEX(push_pop_);
36 // Example: Queue implementation non thread-safe but still usable if clients
37 // are synchronized somehow, it calls a method to "protect" from
38 // a "protected" method
40 // In this case the macro DFAKE_SCOPED_RECURSIVE_LOCK
41 // has to be used, it checks that if a thread is inside the push/pop
42 // then noone else is still inside the pop/push
44 // class NonThreadSafeQueue {
47 // DFAKE_SCOPED_LOCK(push_pop_);
51 // DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_);
55 // void bar() { DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_); ... }
58 // DFAKE_MUTEX(push_pop_);
62 // Example: Queue implementation not usable even if clients are synchronized,
63 // so only one thread in the class life cycle can use the two members
66 // In this case the macro DFAKE_SCOPED_LOCK_THREAD_LOCKED pins the
68 // critical section the first time a thread enters push or pop, from
69 // that time on only that thread is allowed to execute push or pop.
71 // class NonThreadSafeQueue {
74 // void push(int) { DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_); ... }
75 // int pop() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_); ... }
78 // DFAKE_MUTEX(push_pop_);
82 // Example: Class that has to be contructed/destroyed on same thread, it has
83 // a "shareable" method (with external synchronization) and a not
84 // shareable method (even with external synchronization).
86 // In this case 3 Critical sections have to be defined
88 // class ExoticClass {
90 // ExoticClass() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... }
91 // ~ExoticClass() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... }
93 // void Shareable() { DFAKE_SCOPED_LOCK(shareable_section_); ... }
94 // void NotShareable() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... }
97 // DFAKE_MUTEX(ctor_dtor_);
98 // DFAKE_MUTEX(shareable_section_);
104 // Defines a class member that acts like a mutex. It is used only as a
105 // verification tool.
106 #define DFAKE_MUTEX(obj) \
107 mutable base::ThreadCollisionWarner obj
108 // Asserts the call is never called simultaneously in two threads. Used at
109 // member function scope.
110 #define DFAKE_SCOPED_LOCK(obj) \
111 base::ThreadCollisionWarner::ScopedCheck s_check_##obj(&obj)
112 // Asserts the call is never called simultaneously in two threads. Used at
113 // member function scope. Same as DFAKE_SCOPED_LOCK but allows recursive locks.
114 #define DFAKE_SCOPED_RECURSIVE_LOCK(obj) \
115 base::ThreadCollisionWarner::ScopedRecursiveCheck sr_check_##obj(&obj)
116 // Asserts the code is always executed in the same thread.
117 #define DFAKE_SCOPED_LOCK_THREAD_LOCKED(obj) \
118 base::ThreadCollisionWarner::Check check_##obj(&obj)
122 #define DFAKE_MUTEX(obj) typedef void InternalFakeMutexType##obj
123 #define DFAKE_SCOPED_LOCK(obj) ((void)0)
124 #define DFAKE_SCOPED_RECURSIVE_LOCK(obj) ((void)0)
125 #define DFAKE_SCOPED_LOCK_THREAD_LOCKED(obj) ((void)0)
131 // The class ThreadCollisionWarner uses an Asserter to notify the collision
132 // AsserterBase is the interfaces and DCheckAsserter is the default asserter
133 // used. During the unit tests is used another class that doesn't "DCHECK"
134 // in case of collision (check thread_collision_warner_unittests.cc)
135 struct BASE_EXPORT AsserterBase
{
136 virtual ~AsserterBase() {}
137 virtual void warn() = 0;
140 struct BASE_EXPORT DCheckAsserter
: public AsserterBase
{
141 virtual ~DCheckAsserter() {}
142 virtual void warn() OVERRIDE
;
145 class BASE_EXPORT ThreadCollisionWarner
{
147 // The parameter asserter is there only for test purpose
148 explicit ThreadCollisionWarner(AsserterBase
* asserter
= new DCheckAsserter())
149 : valid_thread_id_(0),
151 asserter_(asserter
) {}
153 ~ThreadCollisionWarner() {
157 // This class is meant to be used through the macro
158 // DFAKE_SCOPED_LOCK_THREAD_LOCKED
159 // it doesn't leave the critical section, as opposed to ScopedCheck,
160 // because the critical section being pinned is allowed to be used only
162 class BASE_EXPORT Check
{
164 explicit Check(ThreadCollisionWarner
* warner
)
166 warner_
->EnterSelf();
172 ThreadCollisionWarner
* warner_
;
174 DISALLOW_COPY_AND_ASSIGN(Check
);
177 // This class is meant to be used through the macro
179 class BASE_EXPORT ScopedCheck
{
181 explicit ScopedCheck(ThreadCollisionWarner
* warner
)
191 ThreadCollisionWarner
* warner_
;
193 DISALLOW_COPY_AND_ASSIGN(ScopedCheck
);
196 // This class is meant to be used through the macro
197 // DFAKE_SCOPED_RECURSIVE_LOCK
198 class BASE_EXPORT ScopedRecursiveCheck
{
200 explicit ScopedRecursiveCheck(ThreadCollisionWarner
* warner
)
202 warner_
->EnterSelf();
205 ~ScopedRecursiveCheck() {
210 ThreadCollisionWarner
* warner_
;
212 DISALLOW_COPY_AND_ASSIGN(ScopedRecursiveCheck
);
216 // This method stores the current thread identifier and does a DCHECK
217 // if a another thread has already done it, it is safe if same thread
218 // calls this multiple time (recursion allowed).
221 // Same as EnterSelf but recursion is not allowed.
224 // Removes the thread_id stored in order to allow other threads to
225 // call EnterSelf or Enter.
228 // This stores the thread id that is inside the critical section, if the
229 // value is 0 then no thread is inside.
230 volatile subtle::Atomic32 valid_thread_id_
;
232 // Counter to trace how many time a critical section was "pinned"
233 // (when allowed) in order to unpin it when counter_ reaches 0.
234 volatile subtle::Atomic32 counter_
;
236 // Here only for class unit tests purpose, during the test I need to not
237 // DCHECK but notify the collision with something else.
238 AsserterBase
* asserter_
;
240 DISALLOW_COPY_AND_ASSIGN(ThreadCollisionWarner
);
245 #endif // BASE_THREADING_THREAD_COLLISION_WARNER_H_