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