You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2019 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. #ifndef RTC_BASE_SYNCHRONIZATION_SEQUENCE_CHECKER_H_
  11. #define RTC_BASE_SYNCHRONIZATION_SEQUENCE_CHECKER_H_
  12. #include "api/task_queue/task_queue_base.h"
  13. #include "rtc_base/critical_section.h"
  14. #include "rtc_base/platform_thread_types.h"
  15. #include "rtc_base/system/rtc_export.h"
  16. #include "rtc_base/thread_annotations.h"
  17. namespace webrtc {
  18. // Real implementation of SequenceChecker, for use in debug mode, or
  19. // for temporary use in release mode (e.g. to RTC_CHECK on a threading issue
  20. // seen only in the wild).
  21. //
  22. // Note: You should almost always use the SequenceChecker class to get the
  23. // right version for your build configuration.
  24. class RTC_EXPORT SequenceCheckerImpl {
  25. public:
  26. SequenceCheckerImpl();
  27. ~SequenceCheckerImpl();
  28. bool IsCurrent() const;
  29. // Changes the task queue or thread that is checked for in IsCurrent. This can
  30. // be useful when an object may be created on one task queue / thread and then
  31. // used exclusively on another thread.
  32. void Detach();
  33. private:
  34. rtc::CriticalSection lock_;
  35. // These are mutable so that IsCurrent can set them.
  36. mutable bool attached_ RTC_GUARDED_BY(lock_);
  37. mutable rtc::PlatformThreadRef valid_thread_ RTC_GUARDED_BY(lock_);
  38. mutable const TaskQueueBase* valid_queue_ RTC_GUARDED_BY(lock_);
  39. mutable const void* valid_system_queue_ RTC_GUARDED_BY(lock_);
  40. };
  41. // Do nothing implementation, for use in release mode.
  42. //
  43. // Note: You should almost always use the SequenceChecker class to get the
  44. // right version for your build configuration.
  45. class SequenceCheckerDoNothing {
  46. public:
  47. bool IsCurrent() const { return true; }
  48. void Detach() {}
  49. };
  50. // SequenceChecker is a helper class used to help verify that some methods
  51. // of a class are called on the same task queue or thread. A
  52. // SequenceChecker is bound to a a task queue if the object is
  53. // created on a task queue, or a thread otherwise.
  54. //
  55. //
  56. // Example:
  57. // class MyClass {
  58. // public:
  59. // void Foo() {
  60. // RTC_DCHECK_RUN_ON(sequence_checker_);
  61. // ... (do stuff) ...
  62. // }
  63. //
  64. // private:
  65. // SequenceChecker sequence_checker_;
  66. // }
  67. //
  68. // In Release mode, IsCurrent will always return true.
  69. #if RTC_DCHECK_IS_ON
  70. class RTC_LOCKABLE SequenceChecker : public SequenceCheckerImpl {};
  71. #else
  72. class RTC_LOCKABLE SequenceChecker : public SequenceCheckerDoNothing {};
  73. #endif // RTC_ENABLE_THREAD_CHECKER
  74. namespace webrtc_seq_check_impl {
  75. // Helper class used by RTC_DCHECK_RUN_ON (see example usage below).
  76. class RTC_SCOPED_LOCKABLE SequenceCheckerScope {
  77. public:
  78. template <typename ThreadLikeObject>
  79. explicit SequenceCheckerScope(const ThreadLikeObject* thread_like_object)
  80. RTC_EXCLUSIVE_LOCK_FUNCTION(thread_like_object) {}
  81. SequenceCheckerScope(const SequenceCheckerScope&) = delete;
  82. SequenceCheckerScope& operator=(const SequenceCheckerScope&) = delete;
  83. ~SequenceCheckerScope() RTC_UNLOCK_FUNCTION() {}
  84. template <typename ThreadLikeObject>
  85. static bool IsCurrent(const ThreadLikeObject* thread_like_object) {
  86. return thread_like_object->IsCurrent();
  87. }
  88. };
  89. } // namespace webrtc_seq_check_impl
  90. } // namespace webrtc
  91. // RTC_RUN_ON/RTC_GUARDED_BY/RTC_DCHECK_RUN_ON macros allows to annotate
  92. // variables are accessed from same thread/task queue.
  93. // Using tools designed to check mutexes, it checks at compile time everywhere
  94. // variable is access, there is a run-time dcheck thread/task queue is correct.
  95. //
  96. // class ThreadExample {
  97. // public:
  98. // void NeedVar1() {
  99. // RTC_DCHECK_RUN_ON(network_thread_);
  100. // transport_->Send();
  101. // }
  102. //
  103. // private:
  104. // rtc::Thread* network_thread_;
  105. // int transport_ RTC_GUARDED_BY(network_thread_);
  106. // };
  107. //
  108. // class SequenceCheckerExample {
  109. // public:
  110. // int CalledFromPacer() RTC_RUN_ON(pacer_sequence_checker_) {
  111. // return var2_;
  112. // }
  113. //
  114. // void CallMeFromPacer() {
  115. // RTC_DCHECK_RUN_ON(&pacer_sequence_checker_)
  116. // << "Should be called from pacer";
  117. // CalledFromPacer();
  118. // }
  119. //
  120. // private:
  121. // int pacer_var_ RTC_GUARDED_BY(pacer_sequence_checker_);
  122. // SequenceChecker pacer_sequence_checker_;
  123. // };
  124. //
  125. // class TaskQueueExample {
  126. // public:
  127. // class Encoder {
  128. // public:
  129. // rtc::TaskQueue* Queue() { return encoder_queue_; }
  130. // void Encode() {
  131. // RTC_DCHECK_RUN_ON(encoder_queue_);
  132. // DoSomething(var_);
  133. // }
  134. //
  135. // private:
  136. // rtc::TaskQueue* const encoder_queue_;
  137. // Frame var_ RTC_GUARDED_BY(encoder_queue_);
  138. // };
  139. //
  140. // void Encode() {
  141. // // Will fail at runtime when DCHECK is enabled:
  142. // // encoder_->Encode();
  143. // // Will work:
  144. // rtc::scoped_refptr<Encoder> encoder = encoder_;
  145. // encoder_->Queue()->PostTask([encoder] { encoder->Encode(); });
  146. // }
  147. //
  148. // private:
  149. // rtc::scoped_refptr<Encoder> encoder_;
  150. // }
  151. // Document if a function expected to be called from same thread/task queue.
  152. #define RTC_RUN_ON(x) \
  153. RTC_THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(x))
  154. #define RTC_DCHECK_RUN_ON(x) \
  155. webrtc::webrtc_seq_check_impl::SequenceCheckerScope seq_check_scope(x); \
  156. RTC_DCHECK((x)->IsCurrent())
  157. #endif // RTC_BASE_SYNCHRONIZATION_SEQUENCE_CHECKER_H_