| 6 | #define BASE_THREADING_THREAD_CHECKER_H_ |
| 7 |
| 8 | #include "base/compiler_specific.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/strings/string_piece.h" |
| 11 | #include "base/thread_annotations.h" |
| 12 | #include "base/threading/thread_checker_impl.h" |
| 13 |
| 14 | // ThreadChecker is a helper class used to help verify that some methods of a |
| 15 | // class are called from the same thread (for thread-affinity). It supports |
| 16 | // thread safety annotations (see base/thread_annotations.h). |
| 17 | // |
| 18 | // Use the macros below instead of the ThreadChecker directly so that the unused |
| 19 | // member doesn't result in an extra byte (four when padded) per instance in |
| 20 | // production. |
| 21 | // |
| 22 | // Usage of this class should be *rare* as most classes require thread-safety |
| 23 | // but not thread-affinity. Prefer base::SequenceChecker to verify thread-safe |
| 24 | // access. |
| 25 | // |
| 26 | // Thread-affinity checks should only be required in classes that use thread- |
| 27 | // local-storage or a third-party API that does. |
| 28 | // |
| 29 | // Prefer to encode the minimum requirements of each class instead of the |
| 30 | // environment it happens to run in today. e.g. if a class requires thread- |
| 31 | // safety but not thread-affinity, use a SequenceChecker even if it happens to |
| 32 | // run on a SingleThreadTaskRunner today. That makes it easier to understand |
| 33 | // what would need to change to turn that SingleThreadTaskRunner into a |
| 34 | // SequencedTaskRunner for ease of scheduling as well as minimizes side-effects |
| 35 | // if that change is made. |
| 36 | // |
| 37 | // Usage: |
| 38 | // class MyClass { |
| 39 | // public: |
| 40 | // MyClass() { |
| 41 | // // It's sometimes useful to detach on construction for objects that are |
| 42 | // // constructed in one place and forever after used from another |
| 43 | // // thread. |
| 44 | // DETACH_FROM_THREAD(thread_checker_); |
| 45 | // } |
| 46 | // |
| 47 | // ~MyClass() { |
| 48 | // // ThreadChecker doesn't automatically check it's destroyed on origin |
| 49 | // // thread for the same reason it's sometimes detached in the |
| 50 | // // constructor. It's okay to destroy off thread if the owner otherwise |
| 51 | // // knows usage on the associated thread is done. If you're not |
| 52 | // // detaching in the constructor, you probably want to explicitly check |
| 53 | // // in the destructor. |
| 54 | // DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 55 | // } |
| 56 | // |
| 57 | // void MyMethod() { |
| 58 | // DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 59 | // ... (do stuff) ... |
| 60 | // } |
| 61 | // |
| 62 | // void MyOtherMethod() |
| 63 | // VALID_CONTEXT_REQUIRED(thread_checker_) { |
| 64 | // foo_ = 42; |
| 65 | // } |
| 66 | // |
| 67 | // private: |
| 68 | // int foo_ GUARDED_BY_CONTEXT(thread_checker_); |
| 69 | // |
| 70 | // THREAD_CHECKER(thread_checker_); |
| 71 | // } |
| 72 |
| 73 | #define THREAD_CHECKER_INTERNAL_CONCAT2(a, b) a##b |
| 74 | #define THREAD_CHECKER_INTERNAL_CONCAT(a, b) \ |
| 75 | THREAD_CHECKER_INTERNAL_CONCAT2(a, b) |
| 76 | #define THREAD_CHECKER_INTERNAL_UID(prefix) \ |
| 77 | THREAD_CHECKER_INTERNAL_CONCAT(prefix, __LINE__) |
| 78 |
| 79 | #if DCHECK_IS_ON() |
| 80 | #define THREAD_CHECKER(name) base::ThreadChecker name |
| 81 | #define DCHECK_CALLED_ON_VALID_THREAD(name, ...) \ |
| 82 | base::ScopedValidateThreadChecker THREAD_CHECKER_INTERNAL_UID( \ |
| 83 | scoped_validate_thread_checker_)(name, ##__VA_ARGS__); |
| 84 | #define DETACH_FROM_THREAD(name) (name).DetachFromThread() |
| 85 | #else // DCHECK_IS_ON() |
| 86 | #define THREAD_CHECKER(name) static_assert(true, "") |
| 87 | #define DCHECK_CALLED_ON_VALID_THREAD(name, ...) EAT_STREAM_PARAMETERS |
| 88 | #define DETACH_FROM_THREAD(name) |
| 89 | #endif // DCHECK_IS_ON() |
| 90 |
| 91 | namespace base { |
| 92 |
| 93 | // Do nothing implementation, for use in release mode. |
| 94 | // |
| 95 | // Note: You should almost always use the ThreadChecker class (through the above |
| 96 | // macros) to get the right version for your build configuration. |
| 97 | // Note: This is only a check, not a "lock". It is marked "LOCKABLE" only in |
| 98 | // order to support thread_annotations.h. |
| 99 | class LOCKABLE ThreadCheckerDoNothing { |
| 100 | public: |
| 101 | ThreadCheckerDoNothing() = default; |
| 102 |
| 103 | // Moving between matching threads is allowed to help classes with |
| 104 | // ThreadCheckers that want a default move-construct/assign. |
| 105 | ThreadCheckerDoNothing(ThreadCheckerDoNothing&& other) = default; |
| 106 | ThreadCheckerDoNothing& operator=(ThreadCheckerDoNothing&& other) = default; |
| 107 |
| 108 | bool CalledOnValidThread() const WARN_UNUSED_RESULT { return true; } |
| 109 | void DetachFromThread() {} |
| 110 |
| 111 | private: |
| 112 | DISALLOW_COPY_AND_ASSIGN(ThreadCheckerDoNothing); |
| 113 | }; |
| 114 |
| 115 | // Note that ThreadCheckerImpl::CalledOnValidThread() returns false when called |
| 116 | // from tasks posted to SingleThreadTaskRunners bound to different sequences, |
| 117 | // even if the tasks happen to run on the same thread (e.g. two independent |
| 118 | // SingleThreadTaskRunners on the ThreadPool that happen to share a thread). |
| 119 | #if DCHECK_IS_ON() |
| 120 | class ThreadChecker : public ThreadCheckerImpl { |
| 121 | }; |
| 122 | #else |
| 123 | class ThreadChecker : public ThreadCheckerDoNothing { |
| 124 | }; |
| 125 | #endif // DCHECK_IS_ON() |
| 126 |
| 127 | class SCOPED_LOCKABLE ScopedValidateThreadChecker { |
| 128 | public: |
| 129 | explicit ScopedValidateThreadChecker(const ThreadChecker& checker) |
| 130 | EXCLUSIVE_LOCK_FUNCTION(checker) { |
| 131 | DCHECK(checker.CalledOnValidThread()); |
| 132 | } |
| 133 |
| 134 | explicit ScopedValidateThreadChecker(const ThreadChecker& checker, |
| 135 | const StringPiece& msg) |
| 136 | EXCLUSIVE_LOCK_FUNCTION(checker) { |
| 137 | DCHECK(checker.CalledOnValidThread()) << msg; |
| 138 | } |
| 139 |
| 140 | ~ScopedValidateThreadChecker() UNLOCK_FUNCTION() {} |
| 141 |
| 142 | private: |
| 143 | DISALLOW_COPY_AND_ASSIGN(ScopedValidateThreadChecker); |
| 144 | }; |
| 145 |
| 146 | } // namespace base |
| 147 |
| 148 | #endif // BASE_THREADING_THREAD_CHECKER_H_ |
| 149 |