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. 4 5 #ifndef BASE_THREADING_THREAD_CHECKER_H_ 6 #define BASE_THREADING_THREAD_CHECKER_H_ 7 8 // Apart from debug builds, we also enable the thread checker in 9 // builds with DCHECK_ALWAYS_ON so that trybots and waterfall bots 10 // with this define will get the same level of thread checking as 11 // debug bots. 12 // 13 // Note that this does not perfectly match situations where DCHECK is 14 // enabled. For example a non-official release build may have 15 // DCHECK_ALWAYS_ON undefined (and therefore ThreadChecker would be 16 // disabled) but have DCHECKs enabled at runtime. 17 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) 18 #define ENABLE_THREAD_CHECKER 1 19 #else 20 #define ENABLE_THREAD_CHECKER 0 21 #endif 22 23 #if ENABLE_THREAD_CHECKER 24 #include "base/threading/thread_checker_impl.h" 25 #endif 26 27 namespace base { 28 29 // Do nothing implementation, for use in release mode. 30 // 31 // Note: You should almost always use the ThreadChecker class to get the 32 // right version for your build configuration. 33 class ThreadCheckerDoNothing { 34 public: 35 bool CalledOnValidThread() const { 36 return true; 37 } 38 39 void DetachFromThread() {} 40 }; 41 42 // ThreadChecker is a helper class used to help verify that some methods of a 43 // class are called from the same thread. It provides identical functionality to 44 // base::NonThreadSafe, but it is meant to be held as a member variable, rather 45 // than inherited from base::NonThreadSafe. 46 // 47 // While inheriting from base::NonThreadSafe may give a clear indication about 48 // the thread-safety of a class, it may also lead to violations of the style 49 // guide with regard to multiple inheritance. The choice between having a 50 // ThreadChecker member and inheriting from base::NonThreadSafe should be based 51 // on whether: 52 // - Derived classes need to know the thread they belong to, as opposed to 53 // having that functionality fully encapsulated in the base class. 54 // - Derived classes should be able to reassign the base class to another 55 // thread, via DetachFromThread. 56 // 57 // If neither of these are true, then having a ThreadChecker member and calling 58 // CalledOnValidThread is the preferable solution. 59 // 60 // Example: 61 // class MyClass { 62 // public: 63 // void Foo() { 64 // DCHECK(thread_checker_.CalledOnValidThread()); 65 // ... (do stuff) ... 66 // } 67 // 68 // private: 69 // ThreadChecker thread_checker_; 70 // } 71 // 72 // In Release mode, CalledOnValidThread will always return true. 73 #if ENABLE_THREAD_CHECKER 74 class ThreadChecker : public ThreadCheckerImpl { 75 }; 76 #else 77 class ThreadChecker : public ThreadCheckerDoNothing { 78 }; 79 #endif // ENABLE_THREAD_CHECKER 80 81 #undef ENABLE_THREAD_CHECKER 82 83 } // namespace base 84 85 #endif // BASE_THREADING_THREAD_CHECKER_H_ 86