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.

253 lines
7.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2014 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. // Borrowed from Chromium's src/base/threading/thread_checker_unittest.cc.
  11. #include "rtc_base/thread_checker.h"
  12. #include <memory>
  13. #include <utility>
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/constructor_magic.h"
  16. #include "rtc_base/null_socket_server.h"
  17. #include "rtc_base/socket_server.h"
  18. #include "rtc_base/task_queue.h"
  19. #include "rtc_base/thread.h"
  20. #include "test/gtest.h"
  21. // Duplicated from base/threading/thread_checker.h so that we can be
  22. // good citizens there and undef the macro.
  23. #define ENABLE_THREAD_CHECKER RTC_DCHECK_IS_ON
  24. namespace rtc {
  25. namespace {
  26. // Simple class to exercise the basics of ThreadChecker.
  27. // Both the destructor and DoStuff should verify that they were
  28. // called on the same thread as the constructor.
  29. class ThreadCheckerClass : public ThreadChecker {
  30. public:
  31. ThreadCheckerClass() {}
  32. // Verifies that it was called on the same thread as the constructor.
  33. void DoStuff() { RTC_DCHECK(IsCurrent()); }
  34. void Detach() { ThreadChecker::Detach(); }
  35. static void MethodOnDifferentThreadImpl();
  36. static void DetachThenCallFromDifferentThreadImpl();
  37. private:
  38. RTC_DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass);
  39. };
  40. // Calls ThreadCheckerClass::DoStuff on another thread.
  41. class CallDoStuffOnThread : public Thread {
  42. public:
  43. explicit CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class)
  44. : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())),
  45. thread_checker_class_(thread_checker_class) {
  46. SetName("call_do_stuff_on_thread", nullptr);
  47. }
  48. void Run() override { thread_checker_class_->DoStuff(); }
  49. // New method. Needed since Thread::Join is protected, and it is called by
  50. // the TEST.
  51. void Join() { Thread::Join(); }
  52. private:
  53. ThreadCheckerClass* thread_checker_class_;
  54. RTC_DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread);
  55. };
  56. // Deletes ThreadCheckerClass on a different thread.
  57. class DeleteThreadCheckerClassOnThread : public Thread {
  58. public:
  59. explicit DeleteThreadCheckerClassOnThread(
  60. std::unique_ptr<ThreadCheckerClass> thread_checker_class)
  61. : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())),
  62. thread_checker_class_(std::move(thread_checker_class)) {
  63. SetName("delete_thread_checker_class_on_thread", nullptr);
  64. }
  65. void Run() override { thread_checker_class_.reset(); }
  66. // New method. Needed since Thread::Join is protected, and it is called by
  67. // the TEST.
  68. void Join() { Thread::Join(); }
  69. bool has_been_deleted() const { return !thread_checker_class_; }
  70. private:
  71. std::unique_ptr<ThreadCheckerClass> thread_checker_class_;
  72. RTC_DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread);
  73. };
  74. } // namespace
  75. TEST(ThreadCheckerTest, CallsAllowedOnSameThread) {
  76. std::unique_ptr<ThreadCheckerClass> thread_checker_class(
  77. new ThreadCheckerClass);
  78. // Verify that DoStuff doesn't assert.
  79. thread_checker_class->DoStuff();
  80. // Verify that the destructor doesn't assert.
  81. thread_checker_class.reset();
  82. }
  83. TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) {
  84. std::unique_ptr<ThreadCheckerClass> thread_checker_class(
  85. new ThreadCheckerClass);
  86. // Verify that the destructor doesn't assert
  87. // when called on a different thread.
  88. DeleteThreadCheckerClassOnThread delete_on_thread(
  89. std::move(thread_checker_class));
  90. EXPECT_FALSE(delete_on_thread.has_been_deleted());
  91. delete_on_thread.Start();
  92. delete_on_thread.Join();
  93. EXPECT_TRUE(delete_on_thread.has_been_deleted());
  94. }
  95. TEST(ThreadCheckerTest, Detach) {
  96. std::unique_ptr<ThreadCheckerClass> thread_checker_class(
  97. new ThreadCheckerClass);
  98. // Verify that DoStuff doesn't assert when called on a different thread after
  99. // a call to Detach.
  100. thread_checker_class->Detach();
  101. CallDoStuffOnThread call_on_thread(thread_checker_class.get());
  102. call_on_thread.Start();
  103. call_on_thread.Join();
  104. }
  105. #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
  106. void ThreadCheckerClass::MethodOnDifferentThreadImpl() {
  107. std::unique_ptr<ThreadCheckerClass> thread_checker_class(
  108. new ThreadCheckerClass);
  109. // DoStuff should assert in debug builds only when called on a
  110. // different thread.
  111. CallDoStuffOnThread call_on_thread(thread_checker_class.get());
  112. call_on_thread.Start();
  113. call_on_thread.Join();
  114. }
  115. #if ENABLE_THREAD_CHECKER
  116. TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
  117. ASSERT_DEATH({ ThreadCheckerClass::MethodOnDifferentThreadImpl(); }, "");
  118. }
  119. #else
  120. TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) {
  121. ThreadCheckerClass::MethodOnDifferentThreadImpl();
  122. }
  123. #endif // ENABLE_THREAD_CHECKER
  124. void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() {
  125. std::unique_ptr<ThreadCheckerClass> thread_checker_class(
  126. new ThreadCheckerClass);
  127. // DoStuff doesn't assert when called on a different thread
  128. // after a call to Detach.
  129. thread_checker_class->Detach();
  130. CallDoStuffOnThread call_on_thread(thread_checker_class.get());
  131. call_on_thread.Start();
  132. call_on_thread.Join();
  133. // DoStuff should assert in debug builds only after moving to
  134. // another thread.
  135. thread_checker_class->DoStuff();
  136. }
  137. #if ENABLE_THREAD_CHECKER
  138. TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) {
  139. ASSERT_DEATH({ ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); },
  140. "");
  141. }
  142. #else
  143. TEST(ThreadCheckerTest, DetachFromThreadInRelease) {
  144. ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
  145. }
  146. #endif // ENABLE_THREAD_CHECKER
  147. #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
  148. class ThreadAnnotateTest {
  149. public:
  150. // Next two function should create warnings when compile (e.g. if used with
  151. // specific T).
  152. // TODO(danilchap): Find a way to test they do not compile when thread
  153. // annotation checks enabled.
  154. template <typename T>
  155. void access_var_no_annotate() {
  156. var_thread_ = 42;
  157. }
  158. template <typename T>
  159. void access_fun_no_annotate() {
  160. function();
  161. }
  162. // Functions below should be able to compile.
  163. void access_var_annotate_thread() {
  164. RTC_DCHECK_RUN_ON(thread_);
  165. var_thread_ = 42;
  166. }
  167. void access_var_annotate_checker() {
  168. RTC_DCHECK_RUN_ON(&checker_);
  169. var_checker_ = 44;
  170. }
  171. void access_var_annotate_queue() {
  172. RTC_DCHECK_RUN_ON(queue_);
  173. var_queue_ = 46;
  174. }
  175. void access_fun_annotate() {
  176. RTC_DCHECK_RUN_ON(thread_);
  177. function();
  178. }
  179. void access_fun_and_var() {
  180. RTC_DCHECK_RUN_ON(thread_);
  181. fun_acccess_var();
  182. }
  183. private:
  184. void function() RTC_RUN_ON(thread_) {}
  185. void fun_acccess_var() RTC_RUN_ON(thread_) { var_thread_ = 13; }
  186. rtc::Thread* thread_;
  187. rtc::ThreadChecker checker_;
  188. rtc::TaskQueue* queue_;
  189. int var_thread_ RTC_GUARDED_BY(thread_);
  190. int var_checker_ RTC_GUARDED_BY(checker_);
  191. int var_queue_ RTC_GUARDED_BY(queue_);
  192. };
  193. // Just in case we ever get lumped together with other compilation units.
  194. #undef ENABLE_THREAD_CHECKER
  195. } // namespace rtc