henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 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 | |
| 11 | // Borrowed from Chromium's src/base/threading/thread_checker_unittest.cc. |
| 12 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 13 | #include "rtc_base/thread_checker.h" |
| 14 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 15 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include <utility> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/constructor_magic.h" |
| 20 | #include "rtc_base/null_socket_server.h" |
| 21 | #include "rtc_base/socket_server.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/task_queue.h" |
| 23 | #include "rtc_base/thread.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "test/gtest.h" |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 25 | |
| 26 | // Duplicated from base/threading/thread_checker.h so that we can be |
| 27 | // good citizens there and undef the macro. |
kwiberg | 5377bc7 | 2016-10-04 13:46:56 -0700 | [diff] [blame] | 28 | #define ENABLE_THREAD_CHECKER RTC_DCHECK_IS_ON |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 29 | |
| 30 | namespace rtc { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | // Simple class to exercise the basics of ThreadChecker. |
| 35 | // Both the destructor and DoStuff should verify that they were |
| 36 | // called on the same thread as the constructor. |
| 37 | class ThreadCheckerClass : public ThreadChecker { |
| 38 | public: |
| 39 | ThreadCheckerClass() {} |
| 40 | |
| 41 | // Verifies that it was called on the same thread as the constructor. |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 42 | void DoStuff() { RTC_DCHECK(IsCurrent()); } |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 43 | |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 44 | void Detach() { ThreadChecker::Detach(); } |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 45 | |
| 46 | static void MethodOnDifferentThreadImpl(); |
| 47 | static void DetachThenCallFromDifferentThreadImpl(); |
| 48 | |
| 49 | private: |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 50 | RTC_DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | // Calls ThreadCheckerClass::DoStuff on another thread. |
| 54 | class CallDoStuffOnThread : public Thread { |
| 55 | public: |
| 56 | explicit CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class) |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 57 | : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())), |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 58 | thread_checker_class_(thread_checker_class) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 59 | SetName("call_do_stuff_on_thread", nullptr); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 60 | } |
| 61 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 62 | void Run() override { thread_checker_class_->DoStuff(); } |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 63 | |
| 64 | // New method. Needed since Thread::Join is protected, and it is called by |
| 65 | // the TEST. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 66 | void Join() { Thread::Join(); } |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | ThreadCheckerClass* thread_checker_class_; |
| 70 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 71 | RTC_DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // Deletes ThreadCheckerClass on a different thread. |
| 75 | class DeleteThreadCheckerClassOnThread : public Thread { |
| 76 | public: |
| 77 | explicit DeleteThreadCheckerClassOnThread( |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 78 | std::unique_ptr<ThreadCheckerClass> thread_checker_class) |
| 79 | : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())), |
| 80 | thread_checker_class_(std::move(thread_checker_class)) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 81 | SetName("delete_thread_checker_class_on_thread", nullptr); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 82 | } |
| 83 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 84 | void Run() override { thread_checker_class_.reset(); } |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 85 | |
| 86 | // New method. Needed since Thread::Join is protected, and it is called by |
| 87 | // the TEST. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 88 | void Join() { Thread::Join(); } |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 89 | |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 90 | bool has_been_deleted() const { return !thread_checker_class_; } |
| 91 | |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 92 | private: |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 93 | std::unique_ptr<ThreadCheckerClass> thread_checker_class_; |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 94 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 95 | RTC_DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // namespace |
| 99 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 100 | TEST(ThreadCheckerTest, CallsAllowedOnSameThread) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 101 | std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 102 | new ThreadCheckerClass); |
| 103 | |
| 104 | // Verify that DoStuff doesn't assert. |
| 105 | thread_checker_class->DoStuff(); |
| 106 | |
| 107 | // Verify that the destructor doesn't assert. |
| 108 | thread_checker_class.reset(); |
| 109 | } |
| 110 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 111 | TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 112 | std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 113 | new ThreadCheckerClass); |
| 114 | |
| 115 | // Verify that the destructor doesn't assert |
| 116 | // when called on a different thread. |
| 117 | DeleteThreadCheckerClassOnThread delete_on_thread( |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 118 | std::move(thread_checker_class)); |
| 119 | |
| 120 | EXPECT_FALSE(delete_on_thread.has_been_deleted()); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 121 | |
| 122 | delete_on_thread.Start(); |
| 123 | delete_on_thread.Join(); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 124 | |
| 125 | EXPECT_TRUE(delete_on_thread.has_been_deleted()); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 128 | TEST(ThreadCheckerTest, Detach) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 129 | std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 130 | new ThreadCheckerClass); |
| 131 | |
| 132 | // Verify that DoStuff doesn't assert when called on a different thread after |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 133 | // a call to Detach. |
| 134 | thread_checker_class->Detach(); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 135 | CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 136 | |
| 137 | call_on_thread.Start(); |
| 138 | call_on_thread.Join(); |
| 139 | } |
| 140 | |
| 141 | #if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER |
| 142 | |
| 143 | void ThreadCheckerClass::MethodOnDifferentThreadImpl() { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 144 | std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 145 | new ThreadCheckerClass); |
| 146 | |
| 147 | // DoStuff should assert in debug builds only when called on a |
| 148 | // different thread. |
| 149 | CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 150 | |
| 151 | call_on_thread.Start(); |
| 152 | call_on_thread.Join(); |
| 153 | } |
| 154 | |
| 155 | #if ENABLE_THREAD_CHECKER |
tommi@webrtc.org | 2eb1660 | 2015-02-07 19:17:47 +0000 | [diff] [blame] | 156 | TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 157 | ASSERT_DEATH({ ThreadCheckerClass::MethodOnDifferentThreadImpl(); }, ""); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 158 | } |
| 159 | #else |
| 160 | TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) { |
| 161 | ThreadCheckerClass::MethodOnDifferentThreadImpl(); |
| 162 | } |
| 163 | #endif // ENABLE_THREAD_CHECKER |
| 164 | |
| 165 | void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 166 | std::unique_ptr<ThreadCheckerClass> thread_checker_class( |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 167 | new ThreadCheckerClass); |
| 168 | |
| 169 | // DoStuff doesn't assert when called on a different thread |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 170 | // after a call to Detach. |
| 171 | thread_checker_class->Detach(); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 172 | CallDoStuffOnThread call_on_thread(thread_checker_class.get()); |
| 173 | |
| 174 | call_on_thread.Start(); |
| 175 | call_on_thread.Join(); |
| 176 | |
| 177 | // DoStuff should assert in debug builds only after moving to |
| 178 | // another thread. |
| 179 | thread_checker_class->DoStuff(); |
| 180 | } |
| 181 | |
| 182 | #if ENABLE_THREAD_CHECKER |
tommi@webrtc.org | 2eb1660 | 2015-02-07 19:17:47 +0000 | [diff] [blame] | 183 | TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 184 | ASSERT_DEATH({ ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); }, |
| 185 | ""); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 186 | } |
| 187 | #else |
| 188 | TEST(ThreadCheckerTest, DetachFromThreadInRelease) { |
| 189 | ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl(); |
| 190 | } |
| 191 | #endif // ENABLE_THREAD_CHECKER |
| 192 | |
| 193 | #endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER |
| 194 | |
danilchap | 8e572f0 | 2016-05-19 06:49:03 -0700 | [diff] [blame] | 195 | class ThreadAnnotateTest { |
| 196 | public: |
| 197 | // Next two function should create warnings when compile (e.g. if used with |
| 198 | // specific T). |
| 199 | // TODO(danilchap): Find a way to test they do not compile when thread |
| 200 | // annotation checks enabled. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 201 | template <typename T> |
danilchap | 8e572f0 | 2016-05-19 06:49:03 -0700 | [diff] [blame] | 202 | void access_var_no_annotate() { |
| 203 | var_thread_ = 42; |
| 204 | } |
| 205 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 206 | template <typename T> |
danilchap | 8e572f0 | 2016-05-19 06:49:03 -0700 | [diff] [blame] | 207 | void access_fun_no_annotate() { |
| 208 | function(); |
| 209 | } |
| 210 | |
| 211 | // Functions below should be able to compile. |
| 212 | void access_var_annotate_thread() { |
| 213 | RTC_DCHECK_RUN_ON(thread_); |
| 214 | var_thread_ = 42; |
| 215 | } |
| 216 | |
| 217 | void access_var_annotate_checker() { |
| 218 | RTC_DCHECK_RUN_ON(&checker_); |
| 219 | var_checker_ = 44; |
| 220 | } |
| 221 | |
| 222 | void access_var_annotate_queue() { |
| 223 | RTC_DCHECK_RUN_ON(queue_); |
| 224 | var_queue_ = 46; |
| 225 | } |
| 226 | |
| 227 | void access_fun_annotate() { |
| 228 | RTC_DCHECK_RUN_ON(thread_); |
| 229 | function(); |
| 230 | } |
| 231 | |
| 232 | void access_fun_and_var() { |
| 233 | RTC_DCHECK_RUN_ON(thread_); |
| 234 | fun_acccess_var(); |
| 235 | } |
| 236 | |
| 237 | private: |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 238 | void function() RTC_RUN_ON(thread_) {} |
| 239 | void fun_acccess_var() RTC_RUN_ON(thread_) { var_thread_ = 13; } |
danilchap | 8e572f0 | 2016-05-19 06:49:03 -0700 | [diff] [blame] | 240 | |
| 241 | rtc::Thread* thread_; |
| 242 | rtc::ThreadChecker checker_; |
| 243 | rtc::TaskQueue* queue_; |
| 244 | |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 245 | int var_thread_ RTC_GUARDED_BY(thread_); |
danilchap | 5301e3c | 2017-09-06 04:10:21 -0700 | [diff] [blame] | 246 | int var_checker_ RTC_GUARDED_BY(checker_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 247 | int var_queue_ RTC_GUARDED_BY(queue_); |
danilchap | 8e572f0 | 2016-05-19 06:49:03 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 250 | // Just in case we ever get lumped together with other compilation units. |
| 251 | #undef ENABLE_THREAD_CHECKER |
| 252 | |
| 253 | } // namespace rtc |