Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: base/threading/platform_thread_unittest.cc

Issue 1193303002: base/threading: restrict to set only current thread priority (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: android and mac fix Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
186 }; 180 };
187 181
188 } // namespace 182 } // namespace
189 183
190 // Test changing another thread's priority.
191 // NOTE: This test is partially disabled on POSIX, see note above and
192 // http://crbug.com/468793.
193 TEST(PlatformThreadTest, ThreadPriorityOtherThread) {
194 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle());
195
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
230 // Test changing the current thread's priority (which has different semantics on 184 // Test changing the current thread's priority (which has different semantics on
231 // some platforms). 185 // some platforms).
232 // NOTE: This test is partially disabled on POSIX, see note above and
233 // http://crbug.com/468793.
234 TEST(PlatformThreadTest, ThreadPriorityCurrentThread) { 186 TEST(PlatformThreadTest, ThreadPriorityCurrentThread) {
235 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle());
236
237 // Confirm that the current thread's priority is as expected. 187 // Confirm that the current thread's priority is as expected.
188 #if !defined(OS_MACOSX)
Lei Zhang 2015/06/23 05:20:08 I would just skip the test entirely on Mac.
Takashi Toyoshima 2015/06/23 11:42:22 Done.
189 // PlatformThread::GetCurrentThreadPriority() is not implemented on OS X.
238 EXPECT_EQ(ThreadPriority::NORMAL, 190 EXPECT_EQ(ThreadPriority::NORMAL,
239 PlatformThread::GetThreadPriority(current_handle)); 191 PlatformThread::GetCurrentThreadPriority());
240 192 #endif
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 193
252 // Toggle each supported priority on the current thread and confirm it only 194 // Toggle each supported priority on the current thread and confirm it only
253 // affects it (and not the test thread). 195 // affects it (and not the test thread).
254 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { 196 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) {
255 SCOPED_TRACE(i); 197 SCOPED_TRACE(i);
198 #if defined(OS_POSIX)
199 // Only root or a user with CAP_SYS_NICE permission can raise the thread
200 // priority.
201 if (geteuid() != 0 && kThreadPriorityTestValues[i] > ThreadPriority::NORMAL)
202 continue;
203 #endif
256 204
257 // Alter and verify the current thread's priority. 205 // Alter and verify the current thread's priority.
258 PlatformThread::SetThreadPriority(current_handle, 206 PlatformThread::SetCurrentThreadPriority(kThreadPriorityTestValues[i]);
259 kThreadPriorityTestValues[i]); 207 #if !defined(OS_MACOSX)
208 // PlatformThread::GetCurrentThreadPriority() is not implemented on OS X.
260 EXPECT_EQ(kThreadPriorityTestValues[i], 209 EXPECT_EQ(kThreadPriorityTestValues[i],
261 PlatformThread::GetThreadPriority(current_handle)); 210 PlatformThread::GetCurrentThreadPriority());
262 211 #endif
263 // Make sure the test thread was otherwise unaffected.
264 EXPECT_EQ(ThreadPriority::NORMAL,
265 PlatformThread::GetThreadPriority(handle));
266 } 212 }
267 213
268 // Restore current thread priority for follow-up tests. 214 // Restore current thread priority for follow-up tests.
269 PlatformThread::SetThreadPriority(current_handle, ThreadPriority::NORMAL); 215 PlatformThread::SetCurrentThreadPriority(ThreadPriority::NORMAL);
270
271 thread.MarkForTermination();
272 PlatformThread::Join(handle);
273 } 216 }
274 217
275 } // namespace base 218 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698