OLD | NEW |
---|---|
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 |
19 class TrivialThread : public PlatformThread::Delegate { | 22 class TrivialThread : public PlatformThread::Delegate { |
20 public: | 23 public: |
21 TrivialThread() : did_run_(false) {} | 24 TrivialThread() : did_run_(false) {} |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 for (size_t n = 0; n < arraysize(thread); n++) | 166 for (size_t n = 0; n < arraysize(thread); n++) |
164 ASSERT_FALSE(thread[n].IsRunning()); | 167 ASSERT_FALSE(thread[n].IsRunning()); |
165 | 168 |
166 // Make sure that the thread ID is the same across calls. | 169 // Make sure that the thread ID is the same across calls. |
167 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); | 170 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); |
168 } | 171 } |
169 | 172 |
170 namespace { | 173 namespace { |
171 | 174 |
172 const ThreadPriority kThreadPriorityTestValues[] = { | 175 const ThreadPriority kThreadPriorityTestValues[] = { |
173 // Disable non-normal priority toggling on POSIX as it appears to be broken | 176 // Basically, the order should be higher to lower to cover as much cases |
gab
2015/06/26 15:57:13
Remove "Basically" I'd say, i.e.:
// The order sh
Takashi Toyoshima
2015/06/29 05:48:51
Done.
| |
174 // (http://crbug.com/468793). This is prefered to disabling the tests altogether | 177 // as possible on Linux trybots running without CAP_SYS_NICE permission. |
175 // on POSIX as it at least provides coverage for running this code under | 178 ThreadPriority::REALTIME_AUDIO, |
176 // "normal" priority. | |
177 #if !defined(OS_POSIX) | |
178 ThreadPriority::DISPLAY, | 179 ThreadPriority::DISPLAY, |
179 ThreadPriority::REALTIME_AUDIO, | 180 // This redundant BACKGROUND priority is to test backgrounding from other |
180 // Keep BACKGROUND second to last to test backgrounding from other | 181 // priorities, and unbackgrounding. |
181 // priorities. | |
182 ThreadPriority::BACKGROUND, | 182 ThreadPriority::BACKGROUND, |
183 #endif // !defined(OS_POSIX) | 183 ThreadPriority::NORMAL, |
184 // Keep NORMAL last to test unbackgrounding. | 184 ThreadPriority::BACKGROUND |
185 ThreadPriority::NORMAL | |
186 }; | 185 }; |
187 | 186 |
188 } // namespace | 187 } // namespace |
189 | 188 |
190 // Test changing another thread's priority. | 189 #if defined(OS_MACOSX) || defined(OS_ANDROID) |
191 // NOTE: This test is partially disabled on POSIX, see note above and | 190 // PlatformThread::GetCurrentThreadPriority() is not implemented on OS X. |
192 // http://crbug.com/468793. | 191 #define MAYBE_ThreadPriorityCurrentThread DISABLED_ThreadPriorityCurrentThread |
193 TEST(PlatformThreadTest, ThreadPriorityOtherThread) { | 192 #else |
194 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle()); | 193 #define MAYBE_ThreadPriorityCurrentThread ThreadPriorityCurrentThread |
195 | 194 #endif |
196 // Confirm that the current thread's priority is as expected. | |
197 EXPECT_EQ(ThreadPriority::NORMAL, | |
198 PlatformThread::GetThreadPriority(current_handle)); | |
199 | |
200 // Create a test thread. | |
201 FunctionTestThread thread; | |
202 PlatformThreadHandle handle; | |
203 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | |
204 thread.WaitForThreadStart(); | |
205 EXPECT_NE(thread.thread_id(), kInvalidThreadId); | |
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 | |
226 thread.MarkForTermination(); | |
227 PlatformThread::Join(handle); | |
228 } | |
229 | 195 |
230 // Test changing the current thread's priority (which has different semantics on | 196 // Test changing the current thread's priority (which has different semantics on |
231 // some platforms). | 197 // some platforms). |
232 // NOTE: This test is partially disabled on POSIX, see note above and | 198 TEST(PlatformThreadTest, MAYBE_ThreadPriorityCurrentThread) { |
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. | 199 // Confirm that the current thread's priority is as expected. |
238 EXPECT_EQ(ThreadPriority::NORMAL, | 200 EXPECT_EQ(ThreadPriority::NORMAL, |
239 PlatformThread::GetThreadPriority(current_handle)); | 201 PlatformThread::GetCurrentThreadPriority()); |
240 | 202 |
241 // Create a test thread for verification purposes only. | 203 // Toggle each supported priority on the current thread and confirm it |
242 FunctionTestThread thread; | 204 // affects it. |
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) { | 205 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { |
255 SCOPED_TRACE(i); | 206 SCOPED_TRACE(i); |
207 #if defined(OS_POSIX) | |
208 // Only root or a user with CAP_SYS_NICE permission can raise the thread | |
209 // priority. | |
210 ThreadPriority current_priority = | |
211 PlatformThread::GetCurrentThreadPriority(); | |
gab
2015/06/26 15:57:13
inline this in the conditional below maybe? (other
Takashi Toyoshima
2015/06/29 05:48:51
Done.
| |
212 if (geteuid() != 0 && kThreadPriorityTestValues[i] > current_priority) | |
gab
2015/06/26 15:57:13
According to http://stackoverflow.com/questions/41
Takashi Toyoshima
2015/06/29 05:48:51
To check the capability, we need libcap.so and the
| |
213 continue; | |
214 #endif | |
256 | 215 |
257 // Alter and verify the current thread's priority. | 216 // Alter and verify the current thread's priority. |
258 PlatformThread::SetThreadPriority(current_handle, | 217 PlatformThread::SetCurrentThreadPriority(kThreadPriorityTestValues[i]); |
259 kThreadPriorityTestValues[i]); | |
260 EXPECT_EQ(kThreadPriorityTestValues[i], | 218 EXPECT_EQ(kThreadPriorityTestValues[i], |
261 PlatformThread::GetThreadPriority(current_handle)); | 219 PlatformThread::GetCurrentThreadPriority()); |
262 | |
263 // Make sure the test thread was otherwise unaffected. | |
264 EXPECT_EQ(ThreadPriority::NORMAL, | |
265 PlatformThread::GetThreadPriority(handle)); | |
266 } | 220 } |
267 | 221 |
268 // Restore current thread priority for follow-up tests. | 222 // Restore current thread priority for follow-up tests. |
269 PlatformThread::SetThreadPriority(current_handle, ThreadPriority::NORMAL); | 223 PlatformThread::SetCurrentThreadPriority(ThreadPriority::NORMAL); |
270 | |
271 thread.MarkForTermination(); | |
272 PlatformThread::Join(handle); | |
273 } | 224 } |
274 | 225 |
275 } // namespace base | 226 } // namespace base |
OLD | NEW |