| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/compiler_specific.h" | |
| 6 #include "base/macros.h" | |
| 7 #include "base/synchronization/waitable_event.h" | |
| 8 #include "base/threading/platform_thread.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 #if defined(OS_POSIX) | |
| 12 #include <sys/types.h> | |
| 13 #include <unistd.h> | |
| 14 #elif defined(OS_WIN) | |
| 15 #include <windows.h> | |
| 16 #endif | |
| 17 | |
| 18 namespace base { | |
| 19 | |
| 20 // Trivial tests that thread runs and doesn't crash on create and join --------- | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 class TrivialThread : public PlatformThread::Delegate { | |
| 25 public: | |
| 26 TrivialThread() : did_run_(false) {} | |
| 27 | |
| 28 void ThreadMain() override { did_run_ = true; } | |
| 29 | |
| 30 bool did_run() const { return did_run_; } | |
| 31 | |
| 32 private: | |
| 33 bool did_run_; | |
| 34 | |
| 35 DISALLOW_COPY_AND_ASSIGN(TrivialThread); | |
| 36 }; | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 TEST(PlatformThreadTest, Trivial) { | |
| 41 TrivialThread thread; | |
| 42 PlatformThreadHandle handle; | |
| 43 | |
| 44 ASSERT_FALSE(thread.did_run()); | |
| 45 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | |
| 46 PlatformThread::Join(handle); | |
| 47 ASSERT_TRUE(thread.did_run()); | |
| 48 } | |
| 49 | |
| 50 TEST(PlatformThreadTest, TrivialTimesTen) { | |
| 51 TrivialThread thread[10]; | |
| 52 PlatformThreadHandle handle[arraysize(thread)]; | |
| 53 | |
| 54 for (size_t n = 0; n < arraysize(thread); n++) | |
| 55 ASSERT_FALSE(thread[n].did_run()); | |
| 56 for (size_t n = 0; n < arraysize(thread); n++) | |
| 57 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); | |
| 58 for (size_t n = 0; n < arraysize(thread); n++) | |
| 59 PlatformThread::Join(handle[n]); | |
| 60 for (size_t n = 0; n < arraysize(thread); n++) | |
| 61 ASSERT_TRUE(thread[n].did_run()); | |
| 62 } | |
| 63 | |
| 64 // Tests of basic thread functions --------------------------------------------- | |
| 65 | |
| 66 namespace { | |
| 67 | |
| 68 class FunctionTestThread : public PlatformThread::Delegate { | |
| 69 public: | |
| 70 FunctionTestThread() | |
| 71 : thread_id_(kInvalidThreadId), | |
| 72 termination_ready_(true, false), | |
| 73 terminate_thread_(true, false), | |
| 74 done_(false) {} | |
| 75 ~FunctionTestThread() override { | |
| 76 EXPECT_TRUE(terminate_thread_.IsSignaled()) | |
| 77 << "Need to mark thread for termination and join the underlying thread " | |
| 78 << "before destroying a FunctionTestThread as it owns the " | |
| 79 << "WaitableEvent blocking the underlying thread's main."; | |
| 80 } | |
| 81 | |
| 82 // Grabs |thread_id_|, runs an optional test on that thread, signals | |
| 83 // |termination_ready_|, and then waits for |terminate_thread_| to be | |
| 84 // signaled before exiting. | |
| 85 void ThreadMain() override { | |
| 86 thread_id_ = PlatformThread::CurrentId(); | |
| 87 EXPECT_NE(thread_id_, kInvalidThreadId); | |
| 88 | |
| 89 // Make sure that the thread ID is the same across calls. | |
| 90 EXPECT_EQ(thread_id_, PlatformThread::CurrentId()); | |
| 91 | |
| 92 // Run extra tests. | |
| 93 RunTest(); | |
| 94 | |
| 95 termination_ready_.Signal(); | |
| 96 terminate_thread_.Wait(); | |
| 97 | |
| 98 done_ = true; | |
| 99 } | |
| 100 | |
| 101 PlatformThreadId thread_id() const { | |
| 102 EXPECT_TRUE(termination_ready_.IsSignaled()) << "Thread ID still unknown"; | |
| 103 return thread_id_; | |
| 104 } | |
| 105 | |
| 106 bool IsRunning() const { return termination_ready_.IsSignaled() && !done_; } | |
| 107 | |
| 108 // Blocks until this thread is started and ready to be terminated. | |
| 109 void WaitForTerminationReady() { termination_ready_.Wait(); } | |
| 110 | |
| 111 // Marks this thread for termination (callers must then join this thread to be | |
| 112 // guaranteed of termination). | |
| 113 void MarkForTermination() { terminate_thread_.Signal(); } | |
| 114 | |
| 115 private: | |
| 116 // Runs an optional test on the newly created thread. | |
| 117 virtual void RunTest() {} | |
| 118 | |
| 119 PlatformThreadId thread_id_; | |
| 120 | |
| 121 mutable WaitableEvent termination_ready_; | |
| 122 WaitableEvent terminate_thread_; | |
| 123 bool done_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread); | |
| 126 }; | |
| 127 | |
| 128 } // namespace | |
| 129 | |
| 130 TEST(PlatformThreadTest, Function) { | |
| 131 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); | |
| 132 | |
| 133 FunctionTestThread thread; | |
| 134 PlatformThreadHandle handle; | |
| 135 | |
| 136 ASSERT_FALSE(thread.IsRunning()); | |
| 137 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | |
| 138 thread.WaitForTerminationReady(); | |
| 139 ASSERT_TRUE(thread.IsRunning()); | |
| 140 EXPECT_NE(thread.thread_id(), main_thread_id); | |
| 141 | |
| 142 thread.MarkForTermination(); | |
| 143 PlatformThread::Join(handle); | |
| 144 ASSERT_FALSE(thread.IsRunning()); | |
| 145 | |
| 146 // Make sure that the thread ID is the same across calls. | |
| 147 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); | |
| 148 } | |
| 149 | |
| 150 TEST(PlatformThreadTest, FunctionTimesTen) { | |
| 151 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); | |
| 152 | |
| 153 FunctionTestThread thread[10]; | |
| 154 PlatformThreadHandle handle[arraysize(thread)]; | |
| 155 | |
| 156 for (size_t n = 0; n < arraysize(thread); n++) | |
| 157 ASSERT_FALSE(thread[n].IsRunning()); | |
| 158 | |
| 159 for (size_t n = 0; n < arraysize(thread); n++) | |
| 160 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); | |
| 161 for (size_t n = 0; n < arraysize(thread); n++) | |
| 162 thread[n].WaitForTerminationReady(); | |
| 163 | |
| 164 for (size_t n = 0; n < arraysize(thread); n++) { | |
| 165 ASSERT_TRUE(thread[n].IsRunning()); | |
| 166 EXPECT_NE(thread[n].thread_id(), main_thread_id); | |
| 167 | |
| 168 // Make sure no two threads get the same ID. | |
| 169 for (size_t i = 0; i < n; ++i) { | |
| 170 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id()); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 for (size_t n = 0; n < arraysize(thread); n++) | |
| 175 thread[n].MarkForTermination(); | |
| 176 for (size_t n = 0; n < arraysize(thread); n++) | |
| 177 PlatformThread::Join(handle[n]); | |
| 178 for (size_t n = 0; n < arraysize(thread); n++) | |
| 179 ASSERT_FALSE(thread[n].IsRunning()); | |
| 180 | |
| 181 // Make sure that the thread ID is the same across calls. | |
| 182 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); | |
| 183 } | |
| 184 | |
| 185 namespace { | |
| 186 | |
| 187 const ThreadPriority kThreadPriorityTestValues[] = { | |
| 188 // The order should be higher to lower to cover as much cases as possible on | |
| 189 // Linux trybots running without CAP_SYS_NICE permission. | |
| 190 #if !defined(OS_ANDROID) | |
| 191 // PlatformThread::GetCurrentThreadPriority() on Android does not support | |
| 192 // REALTIME_AUDIO case. See http://crbug.com/505474. | |
| 193 ThreadPriority::REALTIME_AUDIO, | |
| 194 #endif | |
| 195 ThreadPriority::DISPLAY, | |
| 196 // This redundant BACKGROUND priority is to test backgrounding from other | |
| 197 // priorities, and unbackgrounding. | |
| 198 ThreadPriority::BACKGROUND, ThreadPriority::NORMAL, | |
| 199 ThreadPriority::BACKGROUND}; | |
| 200 | |
| 201 bool IsBumpingPriorityAllowed() { | |
| 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); | |
| 242 }; | |
| 243 | |
| 244 } // namespace | |
| 245 | |
| 246 #if defined(OS_MACOSX) | |
| 247 // PlatformThread::GetCurrentThreadPriority() is not implemented on OS X. | |
| 248 #define MAYBE_ThreadPriorityCurrentThread DISABLED_ThreadPriorityCurrentThread | |
| 249 #else | |
| 250 #define MAYBE_ThreadPriorityCurrentThread ThreadPriorityCurrentThread | |
| 251 #endif | |
| 252 | |
| 253 // Test changing a created thread's priority (which has different semantics on | |
| 254 // some platforms). | |
| 255 TEST(PlatformThreadTest, MAYBE_ThreadPriorityCurrentThread) { | |
| 256 ThreadPriorityTestThread thread; | |
| 257 PlatformThreadHandle handle; | |
| 258 | |
| 259 ASSERT_FALSE(thread.IsRunning()); | |
| 260 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | |
| 261 thread.WaitForTerminationReady(); | |
| 262 ASSERT_TRUE(thread.IsRunning()); | |
| 263 | |
| 264 thread.MarkForTermination(); | |
| 265 PlatformThread::Join(handle); | |
| 266 ASSERT_FALSE(thread.IsRunning()); | |
| 267 } | |
| 268 | |
| 269 } // namespace base | |
| OLD | NEW |