1 // Copyright 2012 The Chromium Authors 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_THREADING_THREAD_COLLISION_WARNER_H_ 6 #define BASE_THREADING_THREAD_COLLISION_WARNER_H_ 7 8 #include "base/atomicops.h" 9 #include "base/base_export.h" 10 #include "base/compiler_specific.h" 11 #include "base/dcheck_is_on.h" 12 #include "base/macros/uniquify.h" 13 #include "base/memory/raw_ptr.h" 14 15 // A helper class alongside macros to be used to verify assumptions about thread 16 // safety of a class. 17 // 18 // Example: Queue implementation non thread-safe but still usable if clients 19 // are synchronized somehow. 20 // 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 24 // 25 // class NonThreadSafeQueue { 26 // public: 27 // ... 28 // void push(int) { DFAKE_SCOPED_LOCK(push_pop_); ... } 29 // int pop() { DFAKE_SCOPED_LOCK(push_pop_); ... } 30 // ... 31 // private: 32 // DFAKE_MUTEX(push_pop_); 33 // }; 34 // 35 // 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 39 // 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 43 // 44 // class NonThreadSafeQueue { 45 // public: 46 // void push(int) { 47 // DFAKE_SCOPED_LOCK(push_pop_); 48 // ... 49 // } 50 // int pop() { 51 // DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_); 52 // bar(); 53 // ... 54 // } 55 // void bar() { DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_); ... } 56 // ... 57 // private: 58 // DFAKE_MUTEX(push_pop_); 59 // }; 60 // 61 // 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 64 // push/pop. 65 // 66 // In this case the macro DFAKE_SCOPED_LOCK_THREAD_LOCKED pins the 67 // specified 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. 70 // 71 // class NonThreadSafeQueue { 72 // public: 73 // ... 74 // void push(int) { DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_); ... } 75 // int pop() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_); ... } 76 // ... 77 // private: 78 // DFAKE_MUTEX(push_pop_); 79 // }; 80 // 81 // 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). 85 // 86 // In this case 3 Critical sections have to be defined 87 // 88 // class ExoticClass { 89 // public: 90 // ExoticClass() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... } 91 // ~ExoticClass() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... } 92 // 93 // void Shareable() { DFAKE_SCOPED_LOCK(shareable_section_); ... } 94 // void NotShareable() { DFAKE_SCOPED_LOCK_THREAD_LOCKED(ctor_dtor_); ... } 95 // ... 96 // private: 97 // DFAKE_MUTEX(ctor_dtor_); 98 // DFAKE_MUTEX(shareable_section_); 99 // }; 100 101 #if DCHECK_IS_ON() 102 103 // Defines a class member that acts like a mutex. It is used only as a 104 // verification tool. 105 #define DFAKE_MUTEX(obj) \ 106 mutable base::ThreadCollisionWarner obj 107 // Asserts the call is never called simultaneously in two threads. Used at 108 // member function scope. 109 #define DFAKE_SCOPED_LOCK(obj) \ 110 base::ThreadCollisionWarner::ScopedCheck BASE_UNIQUIFY(s_check_)(&obj) 111 // Asserts the call is never called simultaneously in two threads. Used at 112 // member function scope. Same as DFAKE_SCOPED_LOCK but allows recursive locks. 113 #define DFAKE_SCOPED_RECURSIVE_LOCK(obj) \ 114 base::ThreadCollisionWarner::ScopedRecursiveCheck BASE_UNIQUIFY(sr_check)( \ 115 &obj) 116 // Asserts the code is always executed in the same thread. 117 #define DFAKE_SCOPED_LOCK_THREAD_LOCKED(obj) \ 118 base::ThreadCollisionWarner::Check BASE_UNIQUIFY(check_)(&obj) 119 120 #else 121 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) 126 127 #endif 128 129 namespace base { 130 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() = default; 137 virtual void warn() = 0; 138 }; 139 140 struct BASE_EXPORT DCheckAsserter : public AsserterBase { 141 ~DCheckAsserter() override = default; 142 void warn() override; 143 }; 144 145 class BASE_EXPORT ThreadCollisionWarner { 146 public: 147 // The parameter asserter is there only for test purpose 148 explicit ThreadCollisionWarner(AsserterBase* asserter = new DCheckAsserter()) 149 : valid_thread_id_(0), 150 counter_(0), 151 asserter_(asserter) {} 152 153 ThreadCollisionWarner(const ThreadCollisionWarner&) = delete; 154 ThreadCollisionWarner& operator=(const ThreadCollisionWarner&) = delete; 155 ~ThreadCollisionWarner()156 ~ThreadCollisionWarner() { asserter_.ClearAndDelete(); } 157 158 // This class is meant to be used through the macro 159 // DFAKE_SCOPED_LOCK_THREAD_LOCKED 160 // it doesn't leave the critical section, as opposed to ScopedCheck, 161 // because the critical section being pinned is allowed to be used only 162 // from one thread 163 class BASE_EXPORT Check { 164 public: Check(ThreadCollisionWarner * warner)165 explicit Check(ThreadCollisionWarner* warner) 166 : warner_(warner) { 167 warner_->EnterSelf(); 168 } 169 170 Check(const Check&) = delete; 171 Check& operator=(const Check&) = delete; 172 173 ~Check() = default; 174 175 private: 176 raw_ptr<ThreadCollisionWarner> warner_; 177 }; 178 179 // This class is meant to be used through the macro 180 // DFAKE_SCOPED_LOCK 181 class BASE_EXPORT ScopedCheck { 182 public: ScopedCheck(ThreadCollisionWarner * warner)183 explicit ScopedCheck(ThreadCollisionWarner* warner) 184 : warner_(warner) { 185 warner_->Enter(); 186 } 187 188 ScopedCheck(const ScopedCheck&) = delete; 189 ScopedCheck& operator=(const ScopedCheck&) = delete; 190 ~ScopedCheck()191 ~ScopedCheck() { 192 warner_->Leave(); 193 } 194 195 private: 196 raw_ptr<ThreadCollisionWarner> warner_; 197 }; 198 199 // This class is meant to be used through the macro 200 // DFAKE_SCOPED_RECURSIVE_LOCK 201 class BASE_EXPORT ScopedRecursiveCheck { 202 public: ScopedRecursiveCheck(ThreadCollisionWarner * warner)203 explicit ScopedRecursiveCheck(ThreadCollisionWarner* warner) 204 : warner_(warner) { 205 warner_->EnterSelf(); 206 } 207 208 ScopedRecursiveCheck(const ScopedRecursiveCheck&) = delete; 209 ScopedRecursiveCheck& operator=(const ScopedRecursiveCheck&) = delete; 210 ~ScopedRecursiveCheck()211 ~ScopedRecursiveCheck() { 212 warner_->Leave(); 213 } 214 215 private: 216 raw_ptr<ThreadCollisionWarner> warner_; 217 }; 218 219 private: 220 // This method stores the current thread identifier and does a DCHECK 221 // if a another thread has already done it, it is safe if same thread 222 // calls this multiple time (recursion allowed). 223 void EnterSelf(); 224 225 // Same as EnterSelf but recursion is not allowed. 226 void Enter(); 227 228 // Removes the thread_id stored in order to allow other threads to 229 // call EnterSelf or Enter. 230 void Leave(); 231 232 // This stores the thread id that is inside the critical section, if the 233 // value is 0 then no thread is inside. 234 volatile subtle::Atomic32 valid_thread_id_; 235 236 // Counter to trace how many time a critical section was "pinned" 237 // (when allowed) in order to unpin it when counter_ reaches 0. 238 volatile subtle::Atomic32 counter_; 239 240 // Here only for class unit tests purpose, during the test I need to not 241 // DCHECK but notify the collision with something else. 242 raw_ptr<AsserterBase> asserter_; 243 }; 244 245 } // namespace base 246 247 #endif // BASE_THREADING_THREAD_COLLISION_WARNER_H_ 248