blob: ba5c695801ffd28b73f18f9e1227a2d0e9062b60 [file] [log] [blame]
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:441/*
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
jbauch1286d0e2016-04-26 10:13:2213#include <memory>
14
kjellander19796962017-06-30 17:45:2115#include "webrtc/rtc_base/checks.h"
16#include "webrtc/rtc_base/constructormagic.h"
tommia0d6f392017-07-14 21:44:4617#include "webrtc/rtc_base/nullsocketserver.h"
kjellander19796962017-06-30 17:45:2118#include "webrtc/rtc_base/task_queue.h"
19#include "webrtc/rtc_base/thread.h"
20#include "webrtc/rtc_base/thread_checker.h"
kwiberg36a24792016-10-01 05:29:4321#include "webrtc/test/gtest.h"
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4422
23// Duplicated from base/threading/thread_checker.h so that we can be
24// good citizens there and undef the macro.
kwibergd9e48cf2016-10-04 20:46:5625#define ENABLE_THREAD_CHECKER RTC_DCHECK_IS_ON
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4426
27namespace rtc {
28
29namespace {
30
31// Simple class to exercise the basics of ThreadChecker.
32// Both the destructor and DoStuff should verify that they were
33// called on the same thread as the constructor.
34class ThreadCheckerClass : public ThreadChecker {
35 public:
36 ThreadCheckerClass() {}
37
38 // Verifies that it was called on the same thread as the constructor.
henrikg5c075c82015-09-17 07:24:3439 void DoStuff() { RTC_DCHECK(CalledOnValidThread()); }
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4440
41 void DetachFromThread() {
42 ThreadChecker::DetachFromThread();
43 }
44
45 static void MethodOnDifferentThreadImpl();
46 static void DetachThenCallFromDifferentThreadImpl();
47
48 private:
henrikg9199c0e2015-09-16 12:37:4449 RTC_DISALLOW_COPY_AND_ASSIGN(ThreadCheckerClass);
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4450};
51
52// Calls ThreadCheckerClass::DoStuff on another thread.
53class CallDoStuffOnThread : public Thread {
54 public:
55 explicit CallDoStuffOnThread(ThreadCheckerClass* thread_checker_class)
tommia0d6f392017-07-14 21:44:4656 : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())),
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4457 thread_checker_class_(thread_checker_class) {
deadbeef16378412017-02-27 22:06:4158 SetName("call_do_stuff_on_thread", nullptr);
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4459 }
60
kjellander@webrtc.org860ac532015-03-04 12:58:3561 void Run() override { thread_checker_class_->DoStuff(); }
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4462
63 // New method. Needed since Thread::Join is protected, and it is called by
64 // the TEST.
65 void Join() {
66 Thread::Join();
67 }
68
69 private:
70 ThreadCheckerClass* thread_checker_class_;
71
henrikg9199c0e2015-09-16 12:37:4472 RTC_DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread);
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4473};
74
75// Deletes ThreadCheckerClass on a different thread.
76class DeleteThreadCheckerClassOnThread : public Thread {
77 public:
78 explicit DeleteThreadCheckerClassOnThread(
tommia0d6f392017-07-14 21:44:4679 std::unique_ptr<ThreadCheckerClass> thread_checker_class)
80 : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())),
81 thread_checker_class_(std::move(thread_checker_class)) {
deadbeef16378412017-02-27 22:06:4182 SetName("delete_thread_checker_class_on_thread", nullptr);
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4483 }
84
kjellander@webrtc.org860ac532015-03-04 12:58:3585 void Run() override { thread_checker_class_.reset(); }
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4486
87 // New method. Needed since Thread::Join is protected, and it is called by
88 // the TEST.
89 void Join() {
90 Thread::Join();
91 }
92
tommia0d6f392017-07-14 21:44:4693 bool has_been_deleted() const { return !thread_checker_class_; }
94
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4495 private:
jbauch1286d0e2016-04-26 10:13:2296 std::unique_ptr<ThreadCheckerClass> thread_checker_class_;
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4497
henrikg9199c0e2015-09-16 12:37:4498 RTC_DISALLOW_COPY_AND_ASSIGN(DeleteThreadCheckerClassOnThread);
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:4499};
100
101} // namespace
102
henrike@webrtc.orgd0892c42014-10-09 22:08:15103TEST(ThreadCheckerTest, CallsAllowedOnSameThread) {
jbauch1286d0e2016-04-26 10:13:22104 std::unique_ptr<ThreadCheckerClass> thread_checker_class(
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:44105 new ThreadCheckerClass);
106
107 // Verify that DoStuff doesn't assert.
108 thread_checker_class->DoStuff();
109
110 // Verify that the destructor doesn't assert.
111 thread_checker_class.reset();
112}
113
henrike@webrtc.orgd0892c42014-10-09 22:08:15114TEST(ThreadCheckerTest, DestructorAllowedOnDifferentThread) {
jbauch1286d0e2016-04-26 10:13:22115 std::unique_ptr<ThreadCheckerClass> thread_checker_class(
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:44116 new ThreadCheckerClass);
117
118 // Verify that the destructor doesn't assert
119 // when called on a different thread.
120 DeleteThreadCheckerClassOnThread delete_on_thread(
tommia0d6f392017-07-14 21:44:46121 std::move(thread_checker_class));
122
123 EXPECT_FALSE(delete_on_thread.has_been_deleted());
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:44124
125 delete_on_thread.Start();
126 delete_on_thread.Join();
tommia0d6f392017-07-14 21:44:46127
128 EXPECT_TRUE(delete_on_thread.has_been_deleted());
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:44129}
130
henrike@webrtc.orgd0892c42014-10-09 22:08:15131TEST(ThreadCheckerTest, DetachFromThread) {
jbauch1286d0e2016-04-26 10:13:22132 std::unique_ptr<ThreadCheckerClass> thread_checker_class(
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:44133 new ThreadCheckerClass);
134
135 // Verify that DoStuff doesn't assert when called on a different thread after
136 // a call to DetachFromThread.
137 thread_checker_class->DetachFromThread();
138 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
139
140 call_on_thread.Start();
141 call_on_thread.Join();
142}
143
144#if GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
145
146void ThreadCheckerClass::MethodOnDifferentThreadImpl() {
jbauch1286d0e2016-04-26 10:13:22147 std::unique_ptr<ThreadCheckerClass> thread_checker_class(
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:44148 new ThreadCheckerClass);
149
150 // DoStuff should assert in debug builds only when called on a
151 // different thread.
152 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
153
154 call_on_thread.Start();
155 call_on_thread.Join();
156}
157
158#if ENABLE_THREAD_CHECKER
tommi@webrtc.org8454c032015-02-07 19:17:47159TEST(ThreadCheckerDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:44160 ASSERT_DEATH({
161 ThreadCheckerClass::MethodOnDifferentThreadImpl();
162 }, "");
163}
164#else
165TEST(ThreadCheckerTest, MethodAllowedOnDifferentThreadInRelease) {
166 ThreadCheckerClass::MethodOnDifferentThreadImpl();
167}
168#endif // ENABLE_THREAD_CHECKER
169
170void ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl() {
jbauch1286d0e2016-04-26 10:13:22171 std::unique_ptr<ThreadCheckerClass> thread_checker_class(
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:44172 new ThreadCheckerClass);
173
174 // DoStuff doesn't assert when called on a different thread
175 // after a call to DetachFromThread.
176 thread_checker_class->DetachFromThread();
177 CallDoStuffOnThread call_on_thread(thread_checker_class.get());
178
179 call_on_thread.Start();
180 call_on_thread.Join();
181
182 // DoStuff should assert in debug builds only after moving to
183 // another thread.
184 thread_checker_class->DoStuff();
185}
186
187#if ENABLE_THREAD_CHECKER
tommi@webrtc.org8454c032015-02-07 19:17:47188TEST(ThreadCheckerDeathTest, DetachFromThreadInDebug) {
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:44189 ASSERT_DEATH({
190 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
191 }, "");
192}
193#else
194TEST(ThreadCheckerTest, DetachFromThreadInRelease) {
195 ThreadCheckerClass::DetachThenCallFromDifferentThreadImpl();
196}
197#endif // ENABLE_THREAD_CHECKER
198
199#endif // GTEST_HAS_DEATH_TEST || !ENABLE_THREAD_CHECKER
200
danilchap528ae062016-05-19 13:49:03201class ThreadAnnotateTest {
202 public:
203 // Next two function should create warnings when compile (e.g. if used with
204 // specific T).
205 // TODO(danilchap): Find a way to test they do not compile when thread
206 // annotation checks enabled.
207 template<typename T>
208 void access_var_no_annotate() {
209 var_thread_ = 42;
210 }
211
212 template<typename T>
213 void access_fun_no_annotate() {
214 function();
215 }
216
217 // Functions below should be able to compile.
218 void access_var_annotate_thread() {
219 RTC_DCHECK_RUN_ON(thread_);
220 var_thread_ = 42;
221 }
222
223 void access_var_annotate_checker() {
224 RTC_DCHECK_RUN_ON(&checker_);
225 var_checker_ = 44;
226 }
227
228 void access_var_annotate_queue() {
229 RTC_DCHECK_RUN_ON(queue_);
230 var_queue_ = 46;
231 }
232
233 void access_fun_annotate() {
234 RTC_DCHECK_RUN_ON(thread_);
235 function();
236 }
237
238 void access_fun_and_var() {
239 RTC_DCHECK_RUN_ON(thread_);
240 fun_acccess_var();
241 }
242
243 private:
danilchap84ec0f62017-09-06 11:10:21244 void function() RTC_RUN_ON(thread_) {}
245 void fun_acccess_var() RTC_RUN_ON(thread_) { var_thread_ = 13; }
danilchap528ae062016-05-19 13:49:03246
247 rtc::Thread* thread_;
248 rtc::ThreadChecker checker_;
249 rtc::TaskQueue* queue_;
250
danilchap84ec0f62017-09-06 11:10:21251 int var_thread_ RTC_ACCESS_ON(thread_);
252 int var_checker_ RTC_GUARDED_BY(checker_);
253 int var_queue_ RTC_ACCESS_ON(queue_);
danilchap528ae062016-05-19 13:49:03254};
255
henrik.lundin@webrtc.orgd3a28862014-06-16 11:34:44256// Just in case we ever get lumped together with other compilation units.
257#undef ENABLE_THREAD_CHECKER
258
259} // namespace rtc