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

Side by Side Diff: base/threading/platform_thread_posix.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: review #48 Created 5 years, 5 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_mac.mm ('k') | base/threading/platform_thread_unittest.cc » ('j') | 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 "base/threading/platform_thread.h" 5 #include "base/threading/platform_thread.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <pthread.h> 8 #include <pthread.h>
9 #include <sched.h> 9 #include <sched.h>
10 #include <sys/resource.h> 10 #include <sys/resource.h>
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 }; 51 };
52 52
53 void* ThreadFunc(void* params) { 53 void* ThreadFunc(void* params) {
54 base::InitOnThread(); 54 base::InitOnThread();
55 ThreadParams* thread_params = static_cast<ThreadParams*>(params); 55 ThreadParams* thread_params = static_cast<ThreadParams*>(params);
56 56
57 PlatformThread::Delegate* delegate = thread_params->delegate; 57 PlatformThread::Delegate* delegate = thread_params->delegate;
58 if (!thread_params->joinable) 58 if (!thread_params->joinable)
59 base::ThreadRestrictions::SetSingletonAllowed(false); 59 base::ThreadRestrictions::SetSingletonAllowed(false);
60 60
61 if (thread_params->priority != ThreadPriority::NORMAL) { 61 if (thread_params->priority != ThreadPriority::NORMAL)
62 PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(), 62 PlatformThread::SetCurrentThreadPriority(thread_params->priority);
63 thread_params->priority);
64 }
65 63
66 // Stash the id in the handle so the calling thread has a complete 64 // Stash the id in the handle so the calling thread has a complete
67 // handle, and unblock the parent thread. 65 // handle, and unblock the parent thread.
68 *(thread_params->handle) = PlatformThreadHandle(pthread_self(), 66 *(thread_params->handle) = PlatformThreadHandle(pthread_self(),
69 PlatformThread::CurrentId()); 67 PlatformThread::CurrentId());
70 thread_params->handle_set.Signal(); 68 thread_params->handle_set.Signal();
71 69
72 ThreadIdNameManager::GetInstance()->RegisterThread( 70 ThreadIdNameManager::GetInstance()->RegisterThread(
73 PlatformThread::CurrentHandle().platform_handle(), 71 PlatformThread::CurrentHandle().platform_handle(),
74 PlatformThread::CurrentId()); 72 PlatformThread::CurrentId());
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 222
225 // static 223 // static
226 void PlatformThread::Join(PlatformThreadHandle thread_handle) { 224 void PlatformThread::Join(PlatformThreadHandle thread_handle) {
227 // Joining another thread may block the current thread for a long time, since 225 // Joining another thread may block the current thread for a long time, since
228 // the thread referred to by |thread_handle| may still be running long-lived / 226 // the thread referred to by |thread_handle| may still be running long-lived /
229 // blocking tasks. 227 // blocking tasks.
230 base::ThreadRestrictions::AssertIOAllowed(); 228 base::ThreadRestrictions::AssertIOAllowed();
231 CHECK_EQ(0, pthread_join(thread_handle.platform_handle(), NULL)); 229 CHECK_EQ(0, pthread_join(thread_handle.platform_handle(), NULL));
232 } 230 }
233 231
234 // Mac has its own Set/GetThreadPriority() implementations. 232 // Mac has its own Set/GetCurrentThreadPriority() implementations.
235 #if !defined(OS_MACOSX) 233 #if !defined(OS_MACOSX)
236 234
237 // static 235 // static
238 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, 236 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) {
239 ThreadPriority priority) {
240 #if defined(OS_NACL) 237 #if defined(OS_NACL)
241 NOTIMPLEMENTED(); 238 NOTIMPLEMENTED();
242 #else 239 #else
243 if (internal::SetThreadPriorityForPlatform(handle, priority)) 240 if (internal::SetCurrentThreadPriorityForPlatform(priority))
244 return; 241 return;
245 242
246 // setpriority(2) should change the whole thread group's (i.e. process) 243 // setpriority(2) should change the whole thread group's (i.e. process)
247 // priority. However, as stated in the bugs section of 244 // priority. However, as stated in the bugs section of
248 // http://man7.org/linux/man-pages/man2/getpriority.2.html: "under the current 245 // http://man7.org/linux/man-pages/man2/getpriority.2.html: "under the current
249 // Linux/NPTL implementation of POSIX threads, the nice value is a per-thread 246 // Linux/NPTL implementation of POSIX threads, the nice value is a per-thread
250 // attribute". Also, 0 is prefered to the current thread id since it is 247 // attribute". Also, 0 is prefered to the current thread id since it is
251 // equivalent but makes sandboxing easier (https://crbug.com/399473). 248 // equivalent but makes sandboxing easier (https://crbug.com/399473).
252 DCHECK_NE(handle.id(), kInvalidThreadId);
253 const int nice_setting = internal::ThreadPriorityToNiceValue(priority); 249 const int nice_setting = internal::ThreadPriorityToNiceValue(priority);
254 const PlatformThreadId current_id = PlatformThread::CurrentId(); 250 if (setpriority(PRIO_PROCESS, 0, nice_setting)) {
255 if (setpriority(PRIO_PROCESS, handle.id() == current_id ? 0 : handle.id(), 251 DVPLOG(1) << "Failed to set nice value of thread ("
256 nice_setting)) { 252 << PlatformThread::CurrentId() << ") to " << nice_setting;
257 DVPLOG(1) << "Failed to set nice value of thread (" << handle.id()
258 << ") to " << nice_setting;
259 } 253 }
260 #endif // defined(OS_NACL) 254 #endif // defined(OS_NACL)
261 } 255 }
262 256
263 // static 257 // static
264 ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { 258 ThreadPriority PlatformThread::GetCurrentThreadPriority() {
265 #if defined(OS_NACL) 259 #if defined(OS_NACL)
266 NOTIMPLEMENTED(); 260 NOTIMPLEMENTED();
267 return ThreadPriority::NORMAL; 261 return ThreadPriority::NORMAL;
268 #else 262 #else
269 // Mirrors SetThreadPriority()'s implementation. 263 // Mirrors SetCurrentThreadPriority()'s implementation.
270 ThreadPriority platform_specific_priority; 264 ThreadPriority platform_specific_priority;
271 if (internal::GetThreadPriorityForPlatform(handle, 265 if (internal::GetCurrentThreadPriorityForPlatform(
272 &platform_specific_priority)) { 266 &platform_specific_priority)) {
273 return platform_specific_priority; 267 return platform_specific_priority;
274 } 268 }
275 269
276 DCHECK_NE(handle.id(), kInvalidThreadId);
277 const PlatformThreadId current_id = PlatformThread::CurrentId();
278 // Need to clear errno before calling getpriority(): 270 // Need to clear errno before calling getpriority():
279 // http://man7.org/linux/man-pages/man2/getpriority.2.html 271 // http://man7.org/linux/man-pages/man2/getpriority.2.html
280 errno = 0; 272 errno = 0;
281 int nice_value = 273 int nice_value = getpriority(PRIO_PROCESS, 0);
282 getpriority(PRIO_PROCESS, handle.id() == current_id ? 0 : handle.id());
283 if (errno != 0) { 274 if (errno != 0) {
284 DVPLOG(1) << "Failed to get nice value of thread (" << handle.id() << ")"; 275 DVPLOG(1) << "Failed to get nice value of thread ("
276 << PlatformThread::CurrentId() << ")";
285 return ThreadPriority::NORMAL; 277 return ThreadPriority::NORMAL;
286 } 278 }
287 279
288 return internal::NiceValueToThreadPriority(nice_value); 280 return internal::NiceValueToThreadPriority(nice_value);
289 #endif // !defined(OS_NACL) 281 #endif // !defined(OS_NACL)
290 } 282 }
291 283
292 #endif // !defined(OS_MACOSX) 284 #endif // !defined(OS_MACOSX)
293 285
294 } // namespace base 286 } // namespace base
OLDNEW
« no previous file with comments | « base/threading/platform_thread_mac.mm ('k') | base/threading/platform_thread_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698