| 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 | #ifndef API_SEQUENCE_CHECKER_H_ |
| 11 | #define API_SEQUENCE_CHECKER_H_ |
| 12 |
| 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/synchronization/sequence_checker_internal.h" |
| 15 | #include "rtc_base/thread_annotations.h" |
| 16 |
| 17 | namespace webrtc { |
| 18 |
| 19 | // SequenceChecker is a helper class used to help verify that some methods |
| 20 | // of a class are called on the same task queue or thread. A |
| 21 | // SequenceChecker is bound to a a task queue if the object is |
| 22 | // created on a task queue, or a thread otherwise. |
| 23 | // |
| 24 | // |
| 25 | // Example: |
| 26 | // class MyClass { |
| 27 | // public: |
| 28 | // void Foo() { |
| 29 | // RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 30 | // ... (do stuff) ... |
| 31 | // } |
| 32 | // |
| 33 | // private: |
| 34 | // SequenceChecker sequence_checker_; |
| 35 | // } |
| 36 | // |
| 37 | // In Release mode, IsCurrent will always return true. |
| 38 | class RTC_LOCKABLE SequenceChecker |
| 39 | #if RTC_DCHECK_IS_ON |
| 40 | : public webrtc_sequence_checker_internal::SequenceCheckerImpl { |
| 41 | using Impl = webrtc_sequence_checker_internal::SequenceCheckerImpl; |
| 42 | #else |
| 43 | : public webrtc_sequence_checker_internal::SequenceCheckerDoNothing { |
| 44 | using Impl = webrtc_sequence_checker_internal::SequenceCheckerDoNothing; |
| 45 | #endif |
| 46 | public: |
| 47 | enum InitialState : bool { kDetached = false, kAttached = true }; |
| 48 |
| 49 | // TODO(tommi): We could maybe join these two ctors and have fewer factory |
| 50 | // functions. At the moment they're separate to minimize code changes when |
| 51 | // we added the second ctor as well as avoiding to have unnecessary code at |
| 52 | // the SequenceChecker which much only run for the SequenceCheckerImpl |
| 53 | // implementation. |
| 54 | // In theory we could have something like: |
| 55 | // |
| 56 | // SequenceChecker(InitialState initial_state = kAttached, |
| 57 | // TaskQueueBase* attached_queue = TaskQueueBase::Current()); |
| 58 | // |
| 59 | // But the problem with that is having the call to `Current()` exist for |
| 60 | // `SequenceCheckerDoNothing`. |
| 61 | explicit SequenceChecker(InitialState initial_state = kAttached) |
| 62 | : Impl(initial_state) {} |
| 63 | explicit SequenceChecker(TaskQueueBase* attached_queue) |
| 64 | : Impl(attached_queue) {} |
| 65 |
| 66 | // Returns true if sequence checker is attached to the current sequence. |
| 67 | bool IsCurrent() const { return Impl::IsCurrent(); } |
| 68 | // Detaches checker from sequence to which it is attached. Next attempt |
| 69 | // to do a check with this checker will result in attaching this checker |
| 70 | // to the sequence on which check was performed. |
| 71 | void Detach() { Impl::Detach(); } |
| 72 | }; |
| 73 |
| 74 | } // namespace webrtc |
| 75 |
| 76 | // RTC_RUN_ON/RTC_GUARDED_BY/RTC_DCHECK_RUN_ON macros allows to annotate |
| 77 | // variables are accessed from same thread/task queue. |
| 78 | // Using tools designed to check mutexes, it checks at compile time everywhere |
| 79 | // variable is access, there is a run-time dcheck thread/task queue is correct. |
| 80 | // |
| 81 | // class SequenceCheckerExample { |
| 82 | // public: |
| 83 | // int CalledFromPacer() RTC_RUN_ON(pacer_sequence_checker_) { |
| 84 | // return var2_; |
| 85 | // } |
| 86 | // |
| 87 | // void CallMeFromPacer() { |
| 88 | // RTC_DCHECK_RUN_ON(&pacer_sequence_checker_) |
| 89 | // << "Should be called from pacer"; |
| 90 | // CalledFromPacer(); |
| 91 | // } |
| 92 | // |
| 93 | // private: |
| 94 | // int pacer_var_ RTC_GUARDED_BY(pacer_sequence_checker_); |
| 95 | // SequenceChecker pacer_sequence_checker_; |
| 96 | // }; |
| 97 | // |
| 98 | // class TaskQueueExample { |
| 99 | // public: |
| 100 | // class Encoder { |
| 101 | // public: |
| 102 | // rtc::TaskQueueBase& Queue() { return encoder_queue_; } |
| 103 | // void Encode() { |
| 104 | // RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 105 | // DoSomething(var_); |
| 106 | // } |
| 107 | // |
| 108 | // private: |
| 109 | // rtc::TaskQueueBase& encoder_queue_; |
| 110 | // Frame var_ RTC_GUARDED_BY(encoder_queue_); |
| 111 | // }; |
| 112 | // |
| 113 | // void Encode() { |
| 114 | // // Will fail at runtime when DCHECK is enabled: |
| 115 | // // encoder_->Encode(); |
| 116 | // // Will work: |
| 117 | // rtc::scoped_refptr<Encoder> encoder = encoder_; |
| 118 | // encoder_->Queue().PostTask([encoder] { encoder->Encode(); }); |
| 119 | // } |
| 120 | // |
| 121 | // private: |
| 122 | // rtc::scoped_refptr<Encoder> encoder_; |
| 123 | // } |
| 124 |
| 125 | // Document if a function expected to be called from same thread/task queue. |
| 126 | #define RTC_RUN_ON(x) \ |
| 127 | RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(x)) |
| 128 |
| 129 | // Checks current code is running on the desired sequence. |
| 130 | // |
| 131 | // First statement validates it is running on the sequence `x`. |
| 132 | // Second statement annotates for the thread safety analyzer the check was done. |
| 133 | // Such annotation has to be attached to a function, and that function has to be |
| 134 | // called. Thus current implementation creates a noop lambda and calls it. |
| 135 | #define RTC_DCHECK_RUN_ON(x) \ |
| 136 | RTC_DCHECK((x)->IsCurrent()) \ |
| 137 | << webrtc::webrtc_sequence_checker_internal::ExpectationToString(x); \ |
| 138 | []() RTC_ASSERT_EXCLUSIVE_LOCK(x) {}() |
| 139 |
| 140 | #endif // API_SEQUENCE_CHECKER_H_ |
| 141 |