Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(396)

Side by Side Diff: base/threading/platform_thread_unittest.cc

Issue 1641513004: Update //base to chromium 9659b08ea5a34f889dc4166217f438095ddc10d2 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/compiler_specific.h" 5 #include "base/compiler_specific.h"
6 #include "base/macros.h" 6 #include "base/macros.h"
7 #include "base/synchronization/waitable_event.h" 7 #include "base/synchronization/waitable_event.h"
8 #include "base/threading/platform_thread.h" 8 #include "base/threading/platform_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 #if defined(OS_WIN) 11 #if defined(OS_POSIX)
12 #include <sys/types.h>
13 #include <unistd.h>
14 #elif defined(OS_WIN)
12 #include <windows.h> 15 #include <windows.h>
13 #endif 16 #endif
14 17
15 namespace base { 18 namespace base {
16 19
17 // Trivial tests that thread runs and doesn't crash on create and join --------- 20 // Trivial tests that thread runs and doesn't crash on create and join ---------
18 21
22 namespace {
23
19 class TrivialThread : public PlatformThread::Delegate { 24 class TrivialThread : public PlatformThread::Delegate {
20 public: 25 public:
21 TrivialThread() : did_run_(false) {} 26 TrivialThread() : did_run_(false) {}
22 27
23 void ThreadMain() override { did_run_ = true; } 28 void ThreadMain() override { did_run_ = true; }
24 29
25 bool did_run() const { return did_run_; } 30 bool did_run() const { return did_run_; }
26 31
27 private: 32 private:
28 bool did_run_; 33 bool did_run_;
29 34
30 DISALLOW_COPY_AND_ASSIGN(TrivialThread); 35 DISALLOW_COPY_AND_ASSIGN(TrivialThread);
31 }; 36 };
32 37
38 } // namespace
39
33 TEST(PlatformThreadTest, Trivial) { 40 TEST(PlatformThreadTest, Trivial) {
34 TrivialThread thread; 41 TrivialThread thread;
35 PlatformThreadHandle handle; 42 PlatformThreadHandle handle;
36 43
37 ASSERT_FALSE(thread.did_run()); 44 ASSERT_FALSE(thread.did_run());
38 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); 45 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
39 PlatformThread::Join(handle); 46 PlatformThread::Join(handle);
40 ASSERT_TRUE(thread.did_run()); 47 ASSERT_TRUE(thread.did_run());
41 } 48 }
42 49
43 TEST(PlatformThreadTest, TrivialTimesTen) { 50 TEST(PlatformThreadTest, TrivialTimesTen) {
44 TrivialThread thread[10]; 51 TrivialThread thread[10];
45 PlatformThreadHandle handle[arraysize(thread)]; 52 PlatformThreadHandle handle[arraysize(thread)];
46 53
47 for (size_t n = 0; n < arraysize(thread); n++) 54 for (size_t n = 0; n < arraysize(thread); n++)
48 ASSERT_FALSE(thread[n].did_run()); 55 ASSERT_FALSE(thread[n].did_run());
49 for (size_t n = 0; n < arraysize(thread); n++) 56 for (size_t n = 0; n < arraysize(thread); n++)
50 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); 57 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n]));
51 for (size_t n = 0; n < arraysize(thread); n++) 58 for (size_t n = 0; n < arraysize(thread); n++)
52 PlatformThread::Join(handle[n]); 59 PlatformThread::Join(handle[n]);
53 for (size_t n = 0; n < arraysize(thread); n++) 60 for (size_t n = 0; n < arraysize(thread); n++)
54 ASSERT_TRUE(thread[n].did_run()); 61 ASSERT_TRUE(thread[n].did_run());
55 } 62 }
56 63
57 // Tests of basic thread functions --------------------------------------------- 64 // Tests of basic thread functions ---------------------------------------------
58 65
66 namespace {
67
59 class FunctionTestThread : public PlatformThread::Delegate { 68 class FunctionTestThread : public PlatformThread::Delegate {
60 public: 69 public:
61 FunctionTestThread() 70 FunctionTestThread()
62 : thread_id_(kInvalidThreadId), 71 : thread_id_(kInvalidThreadId),
63 thread_started_(true, false), 72 termination_ready_(true, false),
64 terminate_thread_(true, false), 73 terminate_thread_(true, false),
65 done_(false) {} 74 done_(false) {}
66 ~FunctionTestThread() override { 75 ~FunctionTestThread() override {
67 EXPECT_TRUE(terminate_thread_.IsSignaled()) 76 EXPECT_TRUE(terminate_thread_.IsSignaled())
68 << "Need to mark thread for termination and join the underlying thread " 77 << "Need to mark thread for termination and join the underlying thread "
69 << "before destroying a FunctionTestThread as it owns the " 78 << "before destroying a FunctionTestThread as it owns the "
70 << "WaitableEvent blocking the underlying thread's main."; 79 << "WaitableEvent blocking the underlying thread's main.";
71 } 80 }
72 81
73 // Grabs |thread_id_|, signals |thread_started_|, and then waits for 82 // Grabs |thread_id_|, runs an optional test on that thread, signals
74 // |terminate_thread_| to be signaled before exiting. 83 // |termination_ready_|, and then waits for |terminate_thread_| to be
84 // signaled before exiting.
75 void ThreadMain() override { 85 void ThreadMain() override {
76 thread_id_ = PlatformThread::CurrentId(); 86 thread_id_ = PlatformThread::CurrentId();
77 EXPECT_NE(thread_id_, kInvalidThreadId); 87 EXPECT_NE(thread_id_, kInvalidThreadId);
78 88
79 // Make sure that the thread ID is the same across calls. 89 // Make sure that the thread ID is the same across calls.
80 EXPECT_EQ(thread_id_, PlatformThread::CurrentId()); 90 EXPECT_EQ(thread_id_, PlatformThread::CurrentId());
81 91
82 thread_started_.Signal(); 92 // Run extra tests.
93 RunTest();
83 94
95 termination_ready_.Signal();
84 terminate_thread_.Wait(); 96 terminate_thread_.Wait();
85 97
86 done_ = true; 98 done_ = true;
87 } 99 }
88 100
89 PlatformThreadId thread_id() const { 101 PlatformThreadId thread_id() const {
90 EXPECT_TRUE(thread_started_.IsSignaled()) << "Thread ID still unknown"; 102 EXPECT_TRUE(termination_ready_.IsSignaled()) << "Thread ID still unknown";
91 return thread_id_; 103 return thread_id_;
92 } 104 }
93 105
94 bool IsRunning() const { 106 bool IsRunning() const { return termination_ready_.IsSignaled() && !done_; }
95 return thread_started_.IsSignaled() && !done_;
96 }
97 107
98 // Blocks until this thread is started. 108 // Blocks until this thread is started and ready to be terminated.
99 void WaitForThreadStart() { thread_started_.Wait(); } 109 void WaitForTerminationReady() { termination_ready_.Wait(); }
100 110
101 // Mark this thread for termination (callers must then join this thread to be 111 // Marks this thread for termination (callers must then join this thread to be
102 // guaranteed of termination). 112 // guaranteed of termination).
103 void MarkForTermination() { terminate_thread_.Signal(); } 113 void MarkForTermination() { terminate_thread_.Signal(); }
104 114
105 private: 115 private:
116 // Runs an optional test on the newly created thread.
117 virtual void RunTest() {}
118
106 PlatformThreadId thread_id_; 119 PlatformThreadId thread_id_;
107 120
108 mutable WaitableEvent thread_started_; 121 mutable WaitableEvent termination_ready_;
109 WaitableEvent terminate_thread_; 122 WaitableEvent terminate_thread_;
110 bool done_; 123 bool done_;
111 124
112 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread); 125 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread);
113 }; 126 };
114 127
128 } // namespace
129
115 TEST(PlatformThreadTest, Function) { 130 TEST(PlatformThreadTest, Function) {
116 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); 131 PlatformThreadId main_thread_id = PlatformThread::CurrentId();
117 132
118 FunctionTestThread thread; 133 FunctionTestThread thread;
119 PlatformThreadHandle handle; 134 PlatformThreadHandle handle;
120 135
121 ASSERT_FALSE(thread.IsRunning()); 136 ASSERT_FALSE(thread.IsRunning());
122 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); 137 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
123 thread.WaitForThreadStart(); 138 thread.WaitForTerminationReady();
124 ASSERT_TRUE(thread.IsRunning()); 139 ASSERT_TRUE(thread.IsRunning());
125 EXPECT_NE(thread.thread_id(), main_thread_id); 140 EXPECT_NE(thread.thread_id(), main_thread_id);
126 141
127 thread.MarkForTermination(); 142 thread.MarkForTermination();
128 PlatformThread::Join(handle); 143 PlatformThread::Join(handle);
129 ASSERT_FALSE(thread.IsRunning()); 144 ASSERT_FALSE(thread.IsRunning());
130 145
131 // Make sure that the thread ID is the same across calls. 146 // Make sure that the thread ID is the same across calls.
132 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); 147 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId());
133 } 148 }
134 149
135 TEST(PlatformThreadTest, FunctionTimesTen) { 150 TEST(PlatformThreadTest, FunctionTimesTen) {
136 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); 151 PlatformThreadId main_thread_id = PlatformThread::CurrentId();
137 152
138 FunctionTestThread thread[10]; 153 FunctionTestThread thread[10];
139 PlatformThreadHandle handle[arraysize(thread)]; 154 PlatformThreadHandle handle[arraysize(thread)];
140 155
141 for (size_t n = 0; n < arraysize(thread); n++) 156 for (size_t n = 0; n < arraysize(thread); n++)
142 ASSERT_FALSE(thread[n].IsRunning()); 157 ASSERT_FALSE(thread[n].IsRunning());
143 158
144 for (size_t n = 0; n < arraysize(thread); n++) 159 for (size_t n = 0; n < arraysize(thread); n++)
145 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); 160 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n]));
146 for (size_t n = 0; n < arraysize(thread); n++) 161 for (size_t n = 0; n < arraysize(thread); n++)
147 thread[n].WaitForThreadStart(); 162 thread[n].WaitForTerminationReady();
148 163
149 for (size_t n = 0; n < arraysize(thread); n++) { 164 for (size_t n = 0; n < arraysize(thread); n++) {
150 ASSERT_TRUE(thread[n].IsRunning()); 165 ASSERT_TRUE(thread[n].IsRunning());
151 EXPECT_NE(thread[n].thread_id(), main_thread_id); 166 EXPECT_NE(thread[n].thread_id(), main_thread_id);
152 167
153 // Make sure no two threads get the same ID. 168 // Make sure no two threads get the same ID.
154 for (size_t i = 0; i < n; ++i) { 169 for (size_t i = 0; i < n; ++i) {
155 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id()); 170 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id());
156 } 171 }
157 } 172 }
158 173
159 for (size_t n = 0; n < arraysize(thread); n++) 174 for (size_t n = 0; n < arraysize(thread); n++)
160 thread[n].MarkForTermination(); 175 thread[n].MarkForTermination();
161 for (size_t n = 0; n < arraysize(thread); n++) 176 for (size_t n = 0; n < arraysize(thread); n++)
162 PlatformThread::Join(handle[n]); 177 PlatformThread::Join(handle[n]);
163 for (size_t n = 0; n < arraysize(thread); n++) 178 for (size_t n = 0; n < arraysize(thread); n++)
164 ASSERT_FALSE(thread[n].IsRunning()); 179 ASSERT_FALSE(thread[n].IsRunning());
165 180
166 // Make sure that the thread ID is the same across calls. 181 // Make sure that the thread ID is the same across calls.
167 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); 182 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId());
168 } 183 }
169 184
170 namespace { 185 namespace {
171 186
172 const ThreadPriority kThreadPriorityTestValues[] = { 187 const ThreadPriority kThreadPriorityTestValues[] = {
173 // Disable non-normal priority toggling on POSIX as it appears to be broken 188 // The order should be higher to lower to cover as much cases as possible on
174 // (http://crbug.com/468793). This is prefered to disabling the tests altogether 189 // Linux trybots running without CAP_SYS_NICE permission.
175 // on POSIX as it at least provides coverage for running this code under 190 #if !defined(OS_ANDROID)
176 // "normal" priority. 191 // PlatformThread::GetCurrentThreadPriority() on Android does not support
177 #if !defined(OS_POSIX) 192 // REALTIME_AUDIO case. See http://crbug.com/505474.
193 ThreadPriority::REALTIME_AUDIO,
194 #endif
178 ThreadPriority::DISPLAY, 195 ThreadPriority::DISPLAY,
179 ThreadPriority::REALTIME_AUDIO, 196 // This redundant BACKGROUND priority is to test backgrounding from other
180 // Keep BACKGROUND second to last to test backgrounding from other 197 // priorities, and unbackgrounding.
181 // priorities. 198 ThreadPriority::BACKGROUND, ThreadPriority::NORMAL,
182 ThreadPriority::BACKGROUND, 199 ThreadPriority::BACKGROUND};
183 #endif // !defined(OS_POSIX) 200
184 // Keep NORMAL last to test unbackgrounding. 201 bool IsBumpingPriorityAllowed() {
185 ThreadPriority::NORMAL 202 #if defined(OS_POSIX)
203 // Only root can raise thread priority on POSIX environment. On Linux, users
204 // who have CAP_SYS_NICE permission also can raise the thread priority, but
205 // libcap.so would be needed to check the capability.
206 return geteuid() == 0;
207 #else
208 return true;
209 #endif
210 }
211
212 class ThreadPriorityTestThread : public FunctionTestThread {
213 public:
214 ThreadPriorityTestThread() = default;
215 ~ThreadPriorityTestThread() override = default;
216
217 private:
218 void RunTest() override {
219 // Confirm that the current thread's priority is as expected.
220 EXPECT_EQ(ThreadPriority::NORMAL,
221 PlatformThread::GetCurrentThreadPriority());
222
223 // Toggle each supported priority on the current thread and confirm it
224 // affects it.
225 const bool bumping_priority_allowed = IsBumpingPriorityAllowed();
226 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) {
227 SCOPED_TRACE(i);
228 if (!bumping_priority_allowed &&
229 kThreadPriorityTestValues[i] >
230 PlatformThread::GetCurrentThreadPriority()) {
231 continue;
232 }
233
234 // Alter and verify the current thread's priority.
235 PlatformThread::SetCurrentThreadPriority(kThreadPriorityTestValues[i]);
236 EXPECT_EQ(kThreadPriorityTestValues[i],
237 PlatformThread::GetCurrentThreadPriority());
238 }
239 }
240
241 DISALLOW_COPY_AND_ASSIGN(ThreadPriorityTestThread);
186 }; 242 };
187 243
188 } // namespace 244 } // namespace
189 245
190 // Test changing another thread's priority. 246 #if defined(OS_MACOSX)
191 // NOTE: This test is partially disabled on POSIX, see note above and 247 // PlatformThread::GetCurrentThreadPriority() is not implemented on OS X.
192 // http://crbug.com/468793. 248 #define MAYBE_ThreadPriorityCurrentThread DISABLED_ThreadPriorityCurrentThread
193 TEST(PlatformThreadTest, ThreadPriorityOtherThread) { 249 #else
194 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle()); 250 #define MAYBE_ThreadPriorityCurrentThread ThreadPriorityCurrentThread
251 #endif
195 252
196 // Confirm that the current thread's priority is as expected. 253 // Test changing a created thread's priority (which has different semantics on
197 EXPECT_EQ(ThreadPriority::NORMAL, 254 // some platforms).
198 PlatformThread::GetThreadPriority(current_handle)); 255 TEST(PlatformThreadTest, MAYBE_ThreadPriorityCurrentThread) {
256 ThreadPriorityTestThread thread;
257 PlatformThreadHandle handle;
199 258
200 // Create a test thread. 259 ASSERT_FALSE(thread.IsRunning());
201 FunctionTestThread thread;
202 PlatformThreadHandle handle;
203 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); 260 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
204 thread.WaitForThreadStart(); 261 thread.WaitForTerminationReady();
205 EXPECT_NE(thread.thread_id(), kInvalidThreadId); 262 ASSERT_TRUE(thread.IsRunning());
206 EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId());
207
208 // New threads should get normal priority by default.
209 EXPECT_EQ(ThreadPriority::NORMAL, PlatformThread::GetThreadPriority(handle));
210
211 // Toggle each supported priority on the test thread and confirm it only
212 // affects it (and not the current thread).
213 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) {
214 SCOPED_TRACE(i);
215
216 // Alter and verify the test thread's priority.
217 PlatformThread::SetThreadPriority(handle, kThreadPriorityTestValues[i]);
218 EXPECT_EQ(kThreadPriorityTestValues[i],
219 PlatformThread::GetThreadPriority(handle));
220
221 // Make sure the current thread was otherwise unaffected.
222 EXPECT_EQ(ThreadPriority::NORMAL,
223 PlatformThread::GetThreadPriority(current_handle));
224 }
225 263
226 thread.MarkForTermination(); 264 thread.MarkForTermination();
227 PlatformThread::Join(handle); 265 PlatformThread::Join(handle);
228 } 266 ASSERT_FALSE(thread.IsRunning());
229
230 // Test changing the current thread's priority (which has different semantics on
231 // some platforms).
232 // NOTE: This test is partially disabled on POSIX, see note above and
233 // http://crbug.com/468793.
234 TEST(PlatformThreadTest, ThreadPriorityCurrentThread) {
235 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle());
236
237 // Confirm that the current thread's priority is as expected.
238 EXPECT_EQ(ThreadPriority::NORMAL,
239 PlatformThread::GetThreadPriority(current_handle));
240
241 // Create a test thread for verification purposes only.
242 FunctionTestThread thread;
243 PlatformThreadHandle handle;
244 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
245 thread.WaitForThreadStart();
246 EXPECT_NE(thread.thread_id(), kInvalidThreadId);
247 EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId());
248
249 // Confirm that the new thread's priority is as expected.
250 EXPECT_EQ(ThreadPriority::NORMAL, PlatformThread::GetThreadPriority(handle));
251
252 // Toggle each supported priority on the current thread and confirm it only
253 // affects it (and not the test thread).
254 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) {
255 SCOPED_TRACE(i);
256
257 // Alter and verify the current thread's priority.
258 PlatformThread::SetThreadPriority(current_handle,
259 kThreadPriorityTestValues[i]);
260 EXPECT_EQ(kThreadPriorityTestValues[i],
261 PlatformThread::GetThreadPriority(current_handle));
262
263 // Make sure the test thread was otherwise unaffected.
264 EXPECT_EQ(ThreadPriority::NORMAL,
265 PlatformThread::GetThreadPriority(handle));
266 }
267
268 // Restore current thread priority for follow-up tests.
269 PlatformThread::SetThreadPriority(current_handle, ThreadPriority::NORMAL);
270
271 thread.MarkForTermination();
272 PlatformThread::Join(handle);
273 } 267 }
274 268
275 } // namespace base 269 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698