OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 // Note that this does not perfectly match situations where RTC_DCHECK is | 21 // Note that this does not perfectly match situations where RTC_DCHECK is |
22 // enabled. For example a non-official release build may have | 22 // enabled. For example a non-official release build may have |
23 // DCHECK_ALWAYS_ON undefined (and therefore ThreadChecker would be | 23 // DCHECK_ALWAYS_ON undefined (and therefore ThreadChecker would be |
24 // disabled) but have RTC_DCHECKs enabled at runtime. | 24 // disabled) but have RTC_DCHECKs enabled at runtime. |
25 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) | 25 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
26 #define ENABLE_THREAD_CHECKER 1 | 26 #define ENABLE_THREAD_CHECKER 1 |
27 #else | 27 #else |
28 #define ENABLE_THREAD_CHECKER 0 | 28 #define ENABLE_THREAD_CHECKER 0 |
29 #endif | 29 #endif |
30 | 30 |
| 31 #include "webrtc/base/checks.h" |
| 32 #include "webrtc/base/constructormagic.h" |
| 33 #include "webrtc/base/thread_annotations.h" |
31 #include "webrtc/base/thread_checker_impl.h" | 34 #include "webrtc/base/thread_checker_impl.h" |
32 | 35 |
33 namespace rtc { | 36 namespace rtc { |
34 | 37 |
35 // Do nothing implementation, for use in release mode. | 38 // Do nothing implementation, for use in release mode. |
36 // | 39 // |
37 // Note: You should almost always use the ThreadChecker class to get the | 40 // Note: You should almost always use the ThreadChecker class to get the |
38 // right version for your build configuration. | 41 // right version for your build configuration. |
39 class ThreadCheckerDoNothing { | 42 class ThreadCheckerDoNothing { |
40 public: | 43 public: |
(...skipping 29 matching lines...) Expand all Loading... |
70 // RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 73 // RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
71 // ... (do stuff) ... | 74 // ... (do stuff) ... |
72 // } | 75 // } |
73 // | 76 // |
74 // private: | 77 // private: |
75 // ThreadChecker thread_checker_; | 78 // ThreadChecker thread_checker_; |
76 // } | 79 // } |
77 // | 80 // |
78 // In Release mode, CalledOnValidThread will always return true. | 81 // In Release mode, CalledOnValidThread will always return true. |
79 #if ENABLE_THREAD_CHECKER | 82 #if ENABLE_THREAD_CHECKER |
80 class ThreadChecker : public ThreadCheckerImpl { | 83 class LOCKABLE ThreadChecker : public ThreadCheckerImpl { |
81 }; | 84 }; |
82 #else | 85 #else |
83 class ThreadChecker : public ThreadCheckerDoNothing { | 86 class LOCKABLE ThreadChecker : public ThreadCheckerDoNothing { |
84 }; | 87 }; |
85 #endif // ENABLE_THREAD_CHECKER | 88 #endif // ENABLE_THREAD_CHECKER |
86 | 89 |
87 #undef ENABLE_THREAD_CHECKER | 90 #undef ENABLE_THREAD_CHECKER |
88 | 91 |
| 92 namespace internal { |
| 93 class SCOPED_LOCKABLE AnnounceOnThread { |
| 94 public: |
| 95 template<typename ThreadLikeObject> |
| 96 explicit AnnounceOnThread(const ThreadLikeObject* thread_like_object) |
| 97 EXCLUSIVE_LOCK_FUNCTION(thread_like_object) {} |
| 98 ~AnnounceOnThread() UNLOCK_FUNCTION() {} |
| 99 |
| 100 template<typename ThreadLikeObject> |
| 101 static bool IsCurrent(const ThreadLikeObject* thread_like_object) { |
| 102 return thread_like_object->IsCurrent(); |
| 103 } |
| 104 static bool IsCurrent(const rtc::ThreadChecker* checker) { |
| 105 return checker->CalledOnValidThread(); |
| 106 } |
| 107 |
| 108 private: |
| 109 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AnnounceOnThread); |
| 110 }; |
| 111 |
| 112 } // namespace internal |
89 } // namespace rtc | 113 } // namespace rtc |
90 | 114 |
| 115 // RUN_ON/ACCESS_ON/RTC_DCHECK_RUN_ON macros allows to annotate variables are |
| 116 // accessed from same thread/task queue. |
| 117 // Using tools designed to check mutexes, it checks at compile time everywhere |
| 118 // variable is access, there is a run-time dcheck thread/task queue is correct. |
| 119 // |
| 120 // class ExampleThread { |
| 121 // public: |
| 122 // void NeedVar1() { |
| 123 // RTC_DCHECK_RUN_ON(network_thread_); |
| 124 // transport_->Send(); |
| 125 // } |
| 126 // |
| 127 // private: |
| 128 // rtc::Thread* network_thread_; |
| 129 // int transport_ ACCESS_ON(network_thread_); |
| 130 // }; |
| 131 // |
| 132 // class ExampleThreadChecker { |
| 133 // public: |
| 134 // int CalledFromPacer() RUN_ON(pacer_thread_checker_) { |
| 135 // return var2_; |
| 136 // } |
| 137 // |
| 138 // void CallMeFromPacer() { |
| 139 // RTC_DCHECK_RUN_ON(&pacer_thread_checker_) |
| 140 // << "Should be called from pacer"; |
| 141 // CalledFromPacer(); |
| 142 // } |
| 143 // |
| 144 // private: |
| 145 // int pacer_var_ ACCESS_ON(pacer_thread_checker_); |
| 146 // rtc::ThreadChecker pacer_thread_checker_; |
| 147 // }; |
| 148 // |
| 149 // class TaskQueueExample { |
| 150 // public: |
| 151 // class Encoder { |
| 152 // public: |
| 153 // rtc::TaskQueue* Queue() { return encoder_queue_; } |
| 154 // void Encode() { |
| 155 // RTC_DCHECK_RUN_ON(encoder_queue_); |
| 156 // DoSomething(var_); |
| 157 // } |
| 158 // |
| 159 // private: |
| 160 // rtc::TaskQueue* const encoder_queue_; |
| 161 // Frame var_ ACCESS_ON(encoder_queue_); |
| 162 // }; |
| 163 // |
| 164 // void Encode() { |
| 165 // // Will fail at runtime when DCHECK is enabled: |
| 166 // // encoder_->Encode(); |
| 167 // // Will work: |
| 168 // rtc::scoped_ref_ptr<Encoder> encoder = encoder_; |
| 169 // encoder_->Queue()->PostTask([encoder] { encoder->Encode(); }); |
| 170 // } |
| 171 // |
| 172 // private: |
| 173 // rtc::scoped_ref_ptr<Encoder> encoder_; |
| 174 // } |
| 175 |
| 176 #define RTC_DCHECK_RUN_ON(thread_like_object) \ |
| 177 rtc::internal::AnnounceOnThread thread_announcer(thread_like_object); \ |
| 178 RTC_DCHECK(rtc::internal::AnnounceOnThread::IsCurrent(thread_like_object)) |
| 179 |
91 #endif // WEBRTC_BASE_THREAD_CHECKER_H_ | 180 #endif // WEBRTC_BASE_THREAD_CHECKER_H_ |
OLD | NEW |