Changed mft_h264_decoder's API to match with video_decode_engine.h. Also changed...
[chromium-blink-merge.git] / base / thread_collision_warner.h
blob1e14d1a6306aff50aa98cb453987bb06e5d6351a
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_
7 #pragma once
9 #include <memory>
11 #include "base/atomicops.h"
13 // A helper class alongside macros to be used to verify assumptions about thread
14 // safety of a class.
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 {
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 // };
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 {
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 // };
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.
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.
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 // };
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 {
87 // public:
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_); ... }
93 // ...
94 // private:
95 // DFAKE_MUTEX(ctor_dtor_);
96 // DFAKE_MUTEX(shareable_section_);
97 // };
100 #if !defined(NDEBUG)
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)
118 #else
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)
125 #endif
127 namespace base {
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() {}
140 virtual void warn();
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) {}
151 ~ThreadCollisionWarner() {
152 delete asserter_;
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();
167 ~Check() {}
169 private:
170 ThreadCollisionWarner* warner_;
172 DISALLOW_COPY_AND_ASSIGN(Check);
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();
184 ~ScopedCheck() {
185 warner_->Leave();
188 private:
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 {
197 public:
198 explicit ScopedRecursiveCheck(ThreadCollisionWarner* warner)
199 : warner_(warner) {
200 warner_->EnterSelf();
203 ~ScopedRecursiveCheck() {
204 warner_->Leave();
207 private:
208 ThreadCollisionWarner* warner_;
210 DISALLOW_COPY_AND_ASSIGN(ScopedRecursiveCheck);
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();
219 // Same as EnterSelf but recursion is not allowed.
220 void Enter();
222 // Removes the thread_id stored in order to allow other threads to
223 // call EnterSelf or Enter.
224 void Leave();
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);
241 } // namespace base
243 #endif // BASE_THREAD_COLLISION_WARNER_H_