Chromium Code Reviews| 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 ThreadPriority::REALTIME_AUDIO, |
| 174 // (http://crbug.com/468793). This is prefered to disabling the tests altogether | |
| 175 // on POSIX as it at least provides coverage for running this code under | |
| 176 // "normal" priority. | |
| 177 #if !defined(OS_POSIX) | |
| 178 ThreadPriority::DISPLAY, | 177 ThreadPriority::DISPLAY, |
| 179 ThreadPriority::REALTIME_AUDIO, | 178 ThreadPriority::NORMAL, |
| 180 // Keep BACKGROUND second to last to test backgrounding from other | 179 ThreadPriority::BACKGROUND |
| 181 // priorities. | |
| 182 ThreadPriority::BACKGROUND, | |
| 183 #endif // !defined(OS_POSIX) | |
| 184 // Keep NORMAL last to test unbackgrounding. | |
| 185 ThreadPriority::NORMAL | |
|
gab
2015/06/23 17:01:32
The order here was intentional, please keep it thi
Takashi Toyoshima
2015/06/24 04:15:39
Yep, I changed this order because of the reason ga
gab
2015/06/26 15:57:13
But the code at the end of the test doesn't test t
Takashi Toyoshima
2015/06/29 05:48:51
Oh, that's right. I missed the point that the last
| |
| 186 }; | 180 }; |
| 187 | 181 |
| 188 } // namespace | 182 } // namespace |
| 189 | 183 |
| 190 // Test changing another thread's priority. | 184 #if defined(OS_MACOSX) |
| 191 // NOTE: This test is partially disabled on POSIX, see note above and | 185 // PlatformThread::GetCurrentThreadPriority() is not implemented on OS X. |
|
gab
2015/06/23 17:01:32
Nor on OS_ANDROID
Takashi Toyoshima
2015/06/24 04:15:39
Ah, you are right. But it's curious that Android b
gab
2015/06/25 14:49:00
Well actually Android should work for all but REAL
Takashi Toyoshima
2015/06/26 12:28:53
No, GetCurrentThreadPriorityForPlatform() has NOTR
gab
2015/06/26 15:57:13
No, the Android code has NOTIMPLEMENTED() which on
Takashi Toyoshima
2015/06/29 05:48:51
Oh, I confused them. Right, I'll enable the test f
| |
| 192 // http://crbug.com/468793. | 186 #define MAYBE_ThreadPriorityCurrentThread DISABLED_ThreadPriorityCurrentThread |
| 193 TEST(PlatformThreadTest, ThreadPriorityOtherThread) { | 187 #else |
| 194 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle()); | 188 #define MAYBE_ThreadPriorityCurrentThread ThreadPriorityCurrentThread |
| 195 | 189 #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 | 190 |
| 230 // Test changing the current thread's priority (which has different semantics on | 191 // Test changing the current thread's priority (which has different semantics on |
| 231 // some platforms). | 192 // some platforms). |
| 232 // NOTE: This test is partially disabled on POSIX, see note above and | 193 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. | 194 // Confirm that the current thread's priority is as expected. |
| 238 EXPECT_EQ(ThreadPriority::NORMAL, | 195 EXPECT_EQ(ThreadPriority::NORMAL, |
| 239 PlatformThread::GetThreadPriority(current_handle)); | 196 PlatformThread::GetCurrentThreadPriority()); |
| 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 | 197 |
| 252 // Toggle each supported priority on the current thread and confirm it only | 198 // Toggle each supported priority on the current thread and confirm it only |
| 253 // affects it (and not the test thread). | 199 // affects it (and not the test thread). |
|
gab
2015/06/23 17:01:32
Fix this comment (there no longer is a test thread
Takashi Toyoshima
2015/06/24 04:15:39
Done.
| |
| 254 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { | 200 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { |
| 255 SCOPED_TRACE(i); | 201 SCOPED_TRACE(i); |
| 202 #if defined(OS_POSIX) | |
| 203 // Only root or a user with CAP_SYS_NICE permission can raise the thread | |
| 204 // priority. | |
|
gab
2015/06/23 17:01:32
Please document this in the SetCurrentThreadPriori
Takashi Toyoshima
2015/06/24 04:15:39
Done.
| |
| 205 if (geteuid() != 0 && kThreadPriorityTestValues[i] > ThreadPriority::NORMAL) | |
|
gab
2015/06/23 17:01:32
Ah ok I now understand why you want to test backgr
Takashi Toyoshima
2015/06/24 04:15:39
Thanks you, that idea sounds nice.
| |
| 206 continue; | |
| 207 #endif | |
| 256 | 208 |
| 257 // Alter and verify the current thread's priority. | 209 // Alter and verify the current thread's priority. |
| 258 PlatformThread::SetThreadPriority(current_handle, | 210 PlatformThread::SetCurrentThreadPriority(kThreadPriorityTestValues[i]); |
| 259 kThreadPriorityTestValues[i]); | |
| 260 EXPECT_EQ(kThreadPriorityTestValues[i], | 211 EXPECT_EQ(kThreadPriorityTestValues[i], |
| 261 PlatformThread::GetThreadPriority(current_handle)); | 212 PlatformThread::GetCurrentThreadPriority()); |
| 262 | |
| 263 // Make sure the test thread was otherwise unaffected. | |
| 264 EXPECT_EQ(ThreadPriority::NORMAL, | |
| 265 PlatformThread::GetThreadPriority(handle)); | |
| 266 } | 213 } |
| 267 | 214 |
| 268 // Restore current thread priority for follow-up tests. | 215 // Restore current thread priority for follow-up tests. |
| 269 PlatformThread::SetThreadPriority(current_handle, ThreadPriority::NORMAL); | 216 PlatformThread::SetCurrentThreadPriority(ThreadPriority::NORMAL); |
| 270 | |
| 271 thread.MarkForTermination(); | |
| 272 PlatformThread::Join(handle); | |
| 273 } | 217 } |
| 274 | 218 |
| 275 } // namespace base | 219 } // namespace base |
| OLD | NEW |