Chromium Code Reviews| 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 class SCOPED_LOCKABLE AnnounceOnThread { | |
|
tommi
2016/05/19 08:26:14
Since this class is not meant to be used directly,
danilchap
2016/05/19 11:45:29
Done.
| |
| 93 public: | |
| 94 template<typename ThreadLikeObject> | |
| 95 explicit AnnounceOnThread(const ThreadLikeObject* thread_like_object) | |
| 96 EXCLUSIVE_LOCK_FUNCTION(thread_like_object) {} | |
| 97 | |
| 98 template<typename ThreadLikeObject> | |
| 99 static bool IsCurrent(const ThreadLikeObject* thread_like_object) { | |
| 100 return thread_like_object->IsCurrent(); | |
| 101 } | |
| 102 static bool IsCurrent(const rtc::ThreadChecker* checker) { | |
| 103 return checker->CalledOnValidThread(); | |
| 104 } | |
| 105 | |
| 106 ~AnnounceOnThread() UNLOCK_FUNCTION() {} | |
| 107 private: | |
| 108 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AnnounceOnThread); | |
| 109 }; | |
| 110 | |
| 89 } // namespace rtc | 111 } // namespace rtc |
| 90 | 112 |
| 113 // RUN_ON/ACCESS_ON/RTC_DCHECK_RUN_ON macros allows to annotate variables are | |
| 114 // access from same thread/queue. | |
|
tommi
2016/05/19 08:26:14
s/access from same/accessed on the same
danilchap
2016/05/19 11:45:29
Done.
| |
| 115 // Using tools designed to check mutexes, it checks at compile time everywhere | |
| 116 // variable is access, there is a run-time dcheck thread/queue is correct. | |
| 117 // | |
| 118 // class ExampleThread { | |
| 119 // rtc::Thread* network_thread_ | |
| 120 // int transport_ ACCESS_ON(network_thread_); | |
| 121 // void NeedVar1() { | |
| 122 // RTC_DCHECK_RUN_ON(network_thread_); | |
| 123 // transport_->Send(); | |
| 124 // } | |
| 125 // }; | |
| 126 // | |
| 127 // class ExampleThreadChecker { | |
| 128 // int pacer_var_ ACCESS_ON(pacer_thread_checker_); | |
| 129 // rtc::ThreadChecker pacer_thread_checker_; | |
| 130 // | |
| 131 // int CalledFromPacer() RUN_ON(pacer_thread_checker_) { | |
| 132 // return var2_; | |
| 133 // } | |
| 134 // | |
| 135 // void CallMeFromPacer() { | |
| 136 // RTC_DCHECK_RUN_ON(&pacer_thread_checker_) | |
| 137 // << "Should be called from pacer"; | |
| 138 // CalledFromPacer(); | |
| 139 // } | |
| 140 // }; | |
| 141 // | |
| 142 // class TaskQueueExample { | |
| 143 // class Encoder { | |
| 144 // public: | |
| 145 // rtc::TaskQueue* Queue() { return encoder_queue_; } | |
| 146 // void Encode() { | |
| 147 // RTC_DCHECK_RUN_ON(encoder_queue_); | |
| 148 // DoSomething(var_); | |
| 149 // } | |
| 150 // | |
| 151 // private: | |
| 152 // rtc::TaskQueue* const encoder_queue_; | |
| 153 // Frame var_ ACCESS_ON(encoder_queue_); | |
| 154 // }; | |
| 155 // rtc::scoped_ref_ptr<Encoder> encoder_; | |
| 156 // | |
| 157 // void Encode() { | |
| 158 // // Will fail at runtime: | |
| 159 // // encoder_->Encode(); | |
| 160 // // Wile work: | |
| 161 // rtc::scoped_ref_ptr<Encoder> encoder = encoder_; | |
| 162 // encoder_->Queue()->PostTask([encoder] { encoder->Encode(); }); | |
| 163 // } | |
| 164 // } | |
| 165 | |
| 166 #define RTC_DCHECK_RUN_ON(thread_like_object) \ | |
|
tommi
2016/05/19 08:26:14
have you checked if this produces zero code in non
danilchap
2016/05/19 11:45:29
I wrote it in a way it is easy for compiler to opt
| |
| 167 rtc::AnnounceOnThread thread_announcer(thread_like_object); \ | |
| 168 RTC_DCHECK(rtc::AnnounceOnThread::IsCurrent(thread_like_object)) | |
| 169 | |
| 91 #endif // WEBRTC_BASE_THREAD_CHECKER_H_ | 170 #endif // WEBRTC_BASE_THREAD_CHECKER_H_ |
| OLD | NEW |