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/threading/platform_thread.h" | 7 #include "base/threading/platform_thread.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
7 | 9 |
8 #include "testing/gtest/include/gtest/gtest.h" | 10 #if defined(OS_WIN) |
| 11 #include <windows.h> |
| 12 #endif |
9 | 13 |
10 namespace base { | 14 namespace base { |
11 | 15 |
12 // Trivial tests that thread runs and doesn't crash on create and join --------- | 16 // Trivial tests that thread runs and doesn't crash on create and join --------- |
13 | 17 |
14 class TrivialThread : public PlatformThread::Delegate { | 18 class TrivialThread : public PlatformThread::Delegate { |
15 public: | 19 public: |
16 TrivialThread() : did_run_(false) {} | 20 TrivialThread() : did_run_(false) {} |
17 | 21 |
18 void ThreadMain() override { did_run_ = true; } | 22 void ThreadMain() override { did_run_ = true; } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // Make sure no two threads get the same ID. | 113 // Make sure no two threads get the same ID. |
110 for (size_t i = 0; i < n; ++i) { | 114 for (size_t i = 0; i < n; ++i) { |
111 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id()); | 115 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id()); |
112 } | 116 } |
113 } | 117 } |
114 | 118 |
115 // Make sure that the thread ID is the same across calls. | 119 // Make sure that the thread ID is the same across calls. |
116 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); | 120 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); |
117 } | 121 } |
118 | 122 |
| 123 namespace { |
| 124 |
| 125 const ThreadPriority kThreadPriorityTestValues[] = { |
| 126 // Disable non-normal priority toggling on POSIX as it appears to be broken |
| 127 // (http://crbug.com/468793). This is prefered to disabling the tests altogether |
| 128 // on POSIX as it at least provides coverage for running this code under |
| 129 // "normal" priority. |
| 130 #if !defined(OS_POSIX) |
| 131 kThreadPriority_RealtimeAudio, |
| 132 kThreadPriority_Display, |
| 133 kThreadPriority_Background, |
| 134 #endif // !defined(OS_POSIX) |
| 135 // Keep normal last to test unbackgrounding. |
| 136 kThreadPriority_Normal |
| 137 }; |
| 138 |
| 139 } // namespace |
| 140 |
| 141 // Test changing another thread's priority. |
| 142 // NOTE: This test is partially disabled on POSIX, see note above and |
| 143 // http://crbug.com/468793. |
| 144 TEST(PlatformThreadTest, ThreadPriorityOtherThread) { |
| 145 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle()); |
| 146 |
| 147 // Confirm that the current thread's priority is as expected. |
| 148 EXPECT_EQ(kThreadPriority_Normal, |
| 149 PlatformThread::GetThreadPriority(current_handle)); |
| 150 |
| 151 // Create a test thread. |
| 152 FunctionTestThread thread; |
| 153 PlatformThreadHandle handle; |
| 154 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
| 155 EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId()); |
| 156 |
| 157 // New threads should get normal priority by default. |
| 158 EXPECT_EQ(kThreadPriority_Normal, PlatformThread::GetThreadPriority(handle)); |
| 159 |
| 160 // Toggle each supported priority on the test thread and confirm it only |
| 161 // affects it (and not the current thread). |
| 162 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { |
| 163 SCOPED_TRACE(i); |
| 164 |
| 165 // Alter and verify the test thread's priority. |
| 166 PlatformThread::SetThreadPriority(handle, kThreadPriorityTestValues[i]); |
| 167 EXPECT_EQ(kThreadPriorityTestValues[i], |
| 168 PlatformThread::GetThreadPriority(handle)); |
| 169 |
| 170 // Make sure the current thread was otherwise unaffected. |
| 171 EXPECT_EQ(kThreadPriority_Normal, |
| 172 PlatformThread::GetThreadPriority(current_handle)); |
| 173 } |
| 174 } |
| 175 |
| 176 // Test changing the current thread's priority (which has different semantics on |
| 177 // some platforms). |
| 178 // NOTE: This test is partially disabled on POSIX, see note above and |
| 179 // http://crbug.com/468793. |
| 180 TEST(PlatformThreadTest, ThreadPriorityCurrentThread) { |
| 181 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle()); |
| 182 |
| 183 // Confirm that the current thread's priority is as expected. |
| 184 EXPECT_EQ(kThreadPriority_Normal, |
| 185 PlatformThread::GetThreadPriority(current_handle)); |
| 186 |
| 187 // Create a test thread for verification purposes only. |
| 188 FunctionTestThread thread; |
| 189 PlatformThreadHandle handle; |
| 190 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
| 191 EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId()); |
| 192 |
| 193 // Confirm that the new thread's priority is as expected. |
| 194 EXPECT_EQ(kThreadPriority_Normal, PlatformThread::GetThreadPriority(handle)); |
| 195 |
| 196 // Toggle each supported priority on the test thread and confirm it only |
| 197 // affects it (and not the current thread). |
| 198 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { |
| 199 SCOPED_TRACE(i); |
| 200 |
| 201 // Alter and verify the current thread's priority. |
| 202 PlatformThread::SetThreadPriority(current_handle, |
| 203 kThreadPriorityTestValues[i]); |
| 204 EXPECT_EQ(kThreadPriorityTestValues[i], |
| 205 PlatformThread::GetThreadPriority(current_handle)); |
| 206 |
| 207 // Make sure the test thread was otherwise unaffected. |
| 208 EXPECT_EQ(kThreadPriority_Normal, |
| 209 PlatformThread::GetThreadPriority(handle)); |
| 210 } |
| 211 |
| 212 // Restore current thread priority for follow-up tests. |
| 213 PlatformThread::SetThreadPriority(current_handle, kThreadPriority_Normal); |
| 214 } |
| 215 |
119 } // namespace base | 216 } // namespace base |
OLD | NEW |