| 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 |
| 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 { |
| 95 return thread_started_.IsSignaled() && !done_; | 107 return termination_ready_.IsSignaled() && !done_; |
| 96 } | 108 } |
| 97 | 109 |
| 98 // Blocks until this thread is started. | 110 // Blocks until this thread is started and ready to be terminated. |
| 99 void WaitForThreadStart() { thread_started_.Wait(); } | 111 void WaitForTerminationReady() { termination_ready_.Wait(); } |
| 100 | 112 |
| 101 // Mark this thread for termination (callers must then join this thread to be | 113 // Marks this thread for termination (callers must then join this thread to be |
| 102 // guaranteed of termination). | 114 // guaranteed of termination). |
| 103 void MarkForTermination() { terminate_thread_.Signal(); } | 115 void MarkForTermination() { terminate_thread_.Signal(); } |
| 104 | 116 |
| 105 private: | 117 private: |
| 118 // Runs an optional test on the newly created thread. |
| 119 virtual void RunTest() {} |
| 120 |
| 106 PlatformThreadId thread_id_; | 121 PlatformThreadId thread_id_; |
| 107 | 122 |
| 108 mutable WaitableEvent thread_started_; | 123 mutable WaitableEvent termination_ready_; |
| 109 WaitableEvent terminate_thread_; | 124 WaitableEvent terminate_thread_; |
| 110 bool done_; | 125 bool done_; |
| 111 | 126 |
| 112 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread); | 127 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread); |
| 113 }; | 128 }; |
| 114 | 129 |
| 130 } // namespace |
| 131 |
| 115 TEST(PlatformThreadTest, Function) { | 132 TEST(PlatformThreadTest, Function) { |
| 116 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); | 133 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); |
| 117 | 134 |
| 118 FunctionTestThread thread; | 135 FunctionTestThread thread; |
| 119 PlatformThreadHandle handle; | 136 PlatformThreadHandle handle; |
| 120 | 137 |
| 121 ASSERT_FALSE(thread.IsRunning()); | 138 ASSERT_FALSE(thread.IsRunning()); |
| 122 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | 139 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
| 123 thread.WaitForThreadStart(); | 140 thread.WaitForTerminationReady(); |
| 124 ASSERT_TRUE(thread.IsRunning()); | 141 ASSERT_TRUE(thread.IsRunning()); |
| 125 EXPECT_NE(thread.thread_id(), main_thread_id); | 142 EXPECT_NE(thread.thread_id(), main_thread_id); |
| 126 | 143 |
| 127 thread.MarkForTermination(); | 144 thread.MarkForTermination(); |
| 128 PlatformThread::Join(handle); | 145 PlatformThread::Join(handle); |
| 129 ASSERT_FALSE(thread.IsRunning()); | 146 ASSERT_FALSE(thread.IsRunning()); |
| 130 | 147 |
| 131 // Make sure that the thread ID is the same across calls. | 148 // Make sure that the thread ID is the same across calls. |
| 132 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); | 149 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); |
| 133 } | 150 } |
| 134 | 151 |
| 135 TEST(PlatformThreadTest, FunctionTimesTen) { | 152 TEST(PlatformThreadTest, FunctionTimesTen) { |
| 136 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); | 153 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); |
| 137 | 154 |
| 138 FunctionTestThread thread[10]; | 155 FunctionTestThread thread[10]; |
| 139 PlatformThreadHandle handle[arraysize(thread)]; | 156 PlatformThreadHandle handle[arraysize(thread)]; |
| 140 | 157 |
| 141 for (size_t n = 0; n < arraysize(thread); n++) | 158 for (size_t n = 0; n < arraysize(thread); n++) |
| 142 ASSERT_FALSE(thread[n].IsRunning()); | 159 ASSERT_FALSE(thread[n].IsRunning()); |
| 143 | 160 |
| 144 for (size_t n = 0; n < arraysize(thread); n++) | 161 for (size_t n = 0; n < arraysize(thread); n++) |
| 145 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); | 162 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); |
| 146 for (size_t n = 0; n < arraysize(thread); n++) | 163 for (size_t n = 0; n < arraysize(thread); n++) |
| 147 thread[n].WaitForThreadStart(); | 164 thread[n].WaitForTerminationReady(); |
| 148 | 165 |
| 149 for (size_t n = 0; n < arraysize(thread); n++) { | 166 for (size_t n = 0; n < arraysize(thread); n++) { |
| 150 ASSERT_TRUE(thread[n].IsRunning()); | 167 ASSERT_TRUE(thread[n].IsRunning()); |
| 151 EXPECT_NE(thread[n].thread_id(), main_thread_id); | 168 EXPECT_NE(thread[n].thread_id(), main_thread_id); |
| 152 | 169 |
| 153 // Make sure no two threads get the same ID. | 170 // Make sure no two threads get the same ID. |
| 154 for (size_t i = 0; i < n; ++i) { | 171 for (size_t i = 0; i < n; ++i) { |
| 155 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id()); | 172 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id()); |
| 156 } | 173 } |
| 157 } | 174 } |
| 158 | 175 |
| 159 for (size_t n = 0; n < arraysize(thread); n++) | 176 for (size_t n = 0; n < arraysize(thread); n++) |
| 160 thread[n].MarkForTermination(); | 177 thread[n].MarkForTermination(); |
| 161 for (size_t n = 0; n < arraysize(thread); n++) | 178 for (size_t n = 0; n < arraysize(thread); n++) |
| 162 PlatformThread::Join(handle[n]); | 179 PlatformThread::Join(handle[n]); |
| 163 for (size_t n = 0; n < arraysize(thread); n++) | 180 for (size_t n = 0; n < arraysize(thread); n++) |
| 164 ASSERT_FALSE(thread[n].IsRunning()); | 181 ASSERT_FALSE(thread[n].IsRunning()); |
| 165 | 182 |
| 166 // Make sure that the thread ID is the same across calls. | 183 // Make sure that the thread ID is the same across calls. |
| 167 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); | 184 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); |
| 168 } | 185 } |
| 169 | 186 |
| 170 namespace { | 187 namespace { |
| 171 | 188 |
| 172 const ThreadPriority kThreadPriorityTestValues[] = { | 189 const ThreadPriority kThreadPriorityTestValues[] = { |
| 173 // Disable non-normal priority toggling on POSIX as it appears to be broken | 190 // 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 | 191 // Linux trybots running without CAP_SYS_NICE permission. |
| 175 // on POSIX as it at least provides coverage for running this code under | 192 #if !defined(OS_ANDROID) |
| 176 // "normal" priority. | 193 // PlatformThread::GetCurrentThreadPriority() on Android does not support |
| 177 #if !defined(OS_POSIX) | 194 // REALTIME_AUDIO case. See http://crbug.com/505474. |
| 195 ThreadPriority::REALTIME_AUDIO, |
| 196 #endif |
| 178 ThreadPriority::DISPLAY, | 197 ThreadPriority::DISPLAY, |
| 179 ThreadPriority::REALTIME_AUDIO, | 198 // This redundant BACKGROUND priority is to test backgrounding from other |
| 180 // Keep BACKGROUND second to last to test backgrounding from other | 199 // priorities, and unbackgrounding. |
| 181 // priorities. | |
| 182 ThreadPriority::BACKGROUND, | 200 ThreadPriority::BACKGROUND, |
| 183 #endif // !defined(OS_POSIX) | 201 ThreadPriority::NORMAL, |
| 184 // Keep NORMAL last to test unbackgrounding. | 202 ThreadPriority::BACKGROUND}; |
| 185 ThreadPriority::NORMAL | 203 |
| 204 bool IsBumpingPriorityAllowed() { |
| 205 #if defined(OS_POSIX) |
| 206 // Only root can raise thread priority on POSIX environment. On Linux, users |
| 207 // who have CAP_SYS_NICE permission also can raise the thread priority, but |
| 208 // libcap.so would be needed to check the capability. |
| 209 return geteuid() == 0; |
| 210 #else |
| 211 return true; |
| 212 #endif |
| 213 } |
| 214 |
| 215 class ThreadPriorityTestThread : public FunctionTestThread { |
| 216 public: |
| 217 ThreadPriorityTestThread() = default; |
| 218 ~ThreadPriorityTestThread() override = default; |
| 219 |
| 220 private: |
| 221 void RunTest() override { |
| 222 // Confirm that the current thread's priority is as expected. |
| 223 EXPECT_EQ(ThreadPriority::NORMAL, |
| 224 PlatformThread::GetCurrentThreadPriority()); |
| 225 |
| 226 // Toggle each supported priority on the current thread and confirm it |
| 227 // affects it. |
| 228 const bool bumping_priority_allowed = IsBumpingPriorityAllowed(); |
| 229 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { |
| 230 SCOPED_TRACE(i); |
| 231 if (!bumping_priority_allowed && |
| 232 kThreadPriorityTestValues[i] > |
| 233 PlatformThread::GetCurrentThreadPriority()) { |
| 234 continue; |
| 235 } |
| 236 |
| 237 // Alter and verify the current thread's priority. |
| 238 PlatformThread::SetCurrentThreadPriority(kThreadPriorityTestValues[i]); |
| 239 EXPECT_EQ(kThreadPriorityTestValues[i], |
| 240 PlatformThread::GetCurrentThreadPriority()); |
| 241 } |
| 242 } |
| 243 |
| 244 DISALLOW_COPY_AND_ASSIGN(ThreadPriorityTestThread); |
| 186 }; | 245 }; |
| 187 | 246 |
| 188 } // namespace | 247 } // namespace |
| 189 | 248 |
| 190 // Test changing another thread's priority. | 249 #if defined(OS_MACOSX) |
| 191 // NOTE: This test is partially disabled on POSIX, see note above and | 250 // PlatformThread::GetCurrentThreadPriority() is not implemented on OS X. |
| 192 // http://crbug.com/468793. | 251 #define MAYBE_ThreadPriorityCurrentThread DISABLED_ThreadPriorityCurrentThread |
| 193 TEST(PlatformThreadTest, ThreadPriorityOtherThread) { | 252 #else |
| 194 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle()); | 253 #define MAYBE_ThreadPriorityCurrentThread ThreadPriorityCurrentThread |
| 254 #endif |
| 195 | 255 |
| 196 // Confirm that the current thread's priority is as expected. | 256 // Test changing a created thread's priority (which has different semantics on |
| 197 EXPECT_EQ(ThreadPriority::NORMAL, | 257 // some platforms). |
| 198 PlatformThread::GetThreadPriority(current_handle)); | 258 TEST(PlatformThreadTest, MAYBE_ThreadPriorityCurrentThread) { |
| 259 ThreadPriorityTestThread thread; |
| 260 PlatformThreadHandle handle; |
| 199 | 261 |
| 200 // Create a test thread. | 262 ASSERT_FALSE(thread.IsRunning()); |
| 201 FunctionTestThread thread; | |
| 202 PlatformThreadHandle handle; | |
| 203 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | 263 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
| 204 thread.WaitForThreadStart(); | 264 thread.WaitForTerminationReady(); |
| 205 EXPECT_NE(thread.thread_id(), kInvalidThreadId); | 265 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 | 266 |
| 226 thread.MarkForTermination(); | 267 thread.MarkForTermination(); |
| 227 PlatformThread::Join(handle); | 268 PlatformThread::Join(handle); |
| 228 } | 269 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 } | 270 } |
| 274 | 271 |
| 275 } // namespace base | 272 } // namespace base |
| OLD | NEW |