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

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

Issue 1660273004: base: Set initial thread priority in ThreadFunc. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cl format and add comment Created 4 years, 10 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
« no previous file with comments | « base/threading/platform_thread_posix.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "base/threading/platform_thread.h" 10 #include "base/threading/platform_thread.h"
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 // who have CAP_SYS_NICE permission also can raise the thread priority, but 210 // who have CAP_SYS_NICE permission also can raise the thread priority, but
211 // libcap.so would be needed to check the capability. 211 // libcap.so would be needed to check the capability.
212 return geteuid() == 0; 212 return geteuid() == 0;
213 #else 213 #else
214 return true; 214 return true;
215 #endif 215 #endif
216 } 216 }
217 217
218 class ThreadPriorityTestThread : public FunctionTestThread { 218 class ThreadPriorityTestThread : public FunctionTestThread {
219 public: 219 public:
220 ThreadPriorityTestThread() = default; 220 explicit ThreadPriorityTestThread(ThreadPriority priority)
221 : priority_(priority) {}
221 ~ThreadPriorityTestThread() override = default; 222 ~ThreadPriorityTestThread() override = default;
222 223
223 private: 224 private:
224 void RunTest() override { 225 void RunTest() override {
225 // Confirm that the current thread's priority is as expected. 226 // Confirm that the current thread's priority is as expected.
226 EXPECT_EQ(ThreadPriority::NORMAL, 227 EXPECT_EQ(ThreadPriority::NORMAL,
227 PlatformThread::GetCurrentThreadPriority()); 228 PlatformThread::GetCurrentThreadPriority());
228 229
229 // Toggle each supported priority on the current thread and confirm it 230 // Alter and verify the current thread's priority.
230 // affects it. 231 PlatformThread::SetCurrentThreadPriority(priority_);
231 const bool bumping_priority_allowed = IsBumpingPriorityAllowed(); 232 EXPECT_EQ(priority_, PlatformThread::GetCurrentThreadPriority());
232 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { 233 }
233 SCOPED_TRACE(i);
234 if (!bumping_priority_allowed &&
235 kThreadPriorityTestValues[i] >
236 PlatformThread::GetCurrentThreadPriority()) {
237 continue;
238 }
239 234
240 // Alter and verify the current thread's priority. 235 const ThreadPriority priority_;
241 PlatformThread::SetCurrentThreadPriority(kThreadPriorityTestValues[i]);
242 EXPECT_EQ(kThreadPriorityTestValues[i],
243 PlatformThread::GetCurrentThreadPriority());
244 }
245 }
246 236
247 DISALLOW_COPY_AND_ASSIGN(ThreadPriorityTestThread); 237 DISALLOW_COPY_AND_ASSIGN(ThreadPriorityTestThread);
248 }; 238 };
249 239
250 } // namespace 240 } // namespace
251 241
252 #if defined(OS_MACOSX) 242 #if defined(OS_MACOSX)
253 // PlatformThread::GetCurrentThreadPriority() is not implemented on OS X. 243 // PlatformThread::GetCurrentThreadPriority() is not implemented on OS X.
254 #define MAYBE_ThreadPriorityCurrentThread DISABLED_ThreadPriorityCurrentThread 244 #define MAYBE_ThreadPriorityCurrentThread DISABLED_ThreadPriorityCurrentThread
255 #else 245 #else
256 #define MAYBE_ThreadPriorityCurrentThread ThreadPriorityCurrentThread 246 #define MAYBE_ThreadPriorityCurrentThread ThreadPriorityCurrentThread
257 #endif 247 #endif
258 248
259 // Test changing a created thread's priority (which has different semantics on 249 // Test changing a created thread's priority (which has different semantics on
260 // some platforms). 250 // some platforms).
261 TEST(PlatformThreadTest, MAYBE_ThreadPriorityCurrentThread) { 251 TEST(PlatformThreadTest, MAYBE_ThreadPriorityCurrentThread) {
262 ThreadPriorityTestThread thread; 252 const bool bumping_priority_allowed = IsBumpingPriorityAllowed();
263 PlatformThreadHandle handle; 253 if (bumping_priority_allowed) {
254 // Bump the priority in order to verify that new threads are started with
255 // normal priority.
256 PlatformThread::SetCurrentThreadPriority(ThreadPriority::DISPLAY);
257 }
264 258
265 ASSERT_FALSE(thread.IsRunning()); 259 // Toggle each supported priority on the thread and confirm it affects it.
266 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); 260 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) {
267 thread.WaitForTerminationReady(); 261 if (!bumping_priority_allowed &&
268 ASSERT_TRUE(thread.IsRunning()); 262 kThreadPriorityTestValues[i] >
263 PlatformThread::GetCurrentThreadPriority()) {
264 continue;
265 }
269 266
270 thread.MarkForTermination(); 267 ThreadPriorityTestThread thread(kThreadPriorityTestValues[i]);
271 PlatformThread::Join(handle); 268 PlatformThreadHandle handle;
272 ASSERT_FALSE(thread.IsRunning()); 269
270 ASSERT_FALSE(thread.IsRunning());
271 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
272 thread.WaitForTerminationReady();
273 ASSERT_TRUE(thread.IsRunning());
274
275 thread.MarkForTermination();
276 PlatformThread::Join(handle);
277 ASSERT_FALSE(thread.IsRunning());
278 }
273 } 279 }
274 280
275 } // namespace base 281 } // namespace base
OLDNEW
« no previous file with comments | « base/threading/platform_thread_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698