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

Side by Side Diff: base/threading/platform_thread_linux.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: two more android fix and review #2 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/threading/platform_thread.h" 5 #include "base/threading/platform_thread.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <sched.h> 8 #include <sched.h>
9 9
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/threading/platform_thread_internal_posix.h" 12 #include "base/threading/platform_thread_internal_posix.h"
13 #include "base/threading/thread_id_name_manager.h" 13 #include "base/threading/thread_id_name_manager.h"
14 #include "base/tracked_objects.h" 14 #include "base/tracked_objects.h"
15 15
16 #if !defined(OS_NACL) 16 #if !defined(OS_NACL)
17 #include <pthread.h> 17 #include <pthread.h>
18 #include <sys/prctl.h> 18 #include <sys/prctl.h>
19 #include <sys/types.h> 19 #include <sys/types.h>
20 #include <unistd.h> 20 #include <unistd.h>
21 #endif 21 #endif
22 22
23 namespace base { 23 namespace base {
24 24
25 namespace internal { 25 namespace internal {
26 26
27 namespace { 27 namespace {
28 #if !defined(OS_NACL) 28 #if !defined(OS_NACL)
29 const struct sched_param kRealTimePrio = {8}; 29 const struct sched_param kRealTimePrio = {8};
30 const struct sched_param kResetPrio = {0};
30 #endif 31 #endif
31 } // namespace 32 } // namespace
32 33
33 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = { 34 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = {
34 {ThreadPriority::BACKGROUND, 10}, 35 {ThreadPriority::BACKGROUND, 10},
35 {ThreadPriority::NORMAL, 0}, 36 {ThreadPriority::NORMAL, 0},
36 {ThreadPriority::DISPLAY, -6}, 37 {ThreadPriority::DISPLAY, -6},
37 {ThreadPriority::REALTIME_AUDIO, -10}, 38 {ThreadPriority::REALTIME_AUDIO, -10},
38 }; 39 };
39 40
40 bool SetThreadPriorityForPlatform(PlatformThreadHandle handle, 41 bool SetCurrentThreadPriorityForPlatform(ThreadPriority priority) {
41 ThreadPriority priority) {
42 #if !defined(OS_NACL) 42 #if !defined(OS_NACL)
43 // TODO(gab): Assess the correctness of using |pthread_self()| below instead 43 ThreadPriority current_priority;
44 // of |handle|. http://crbug.com/468793. 44 if (priority != ThreadPriority::REALTIME_AUDIO &&
45 GetCurrentThreadPriorityForPlatform(&current_priority) &&
46 current_priority == ThreadPriority::REALTIME_AUDIO) {
47 // If the pthread's round-robin scheduler is already enabled, and the new
48 // priority will use setpriority() instead, the pthread scheduler should be
49 // reset to use SCHED_OTHER so that setpriority() just works.
50 pthread_setschedparam(pthread_self(), SCHED_OTHER, &kResetPrio);
gab 2015/06/23 17:01:32 I think we should do a separate CL for actual logi
Takashi Toyoshima 2015/06/24 04:15:39 Done.
51 return false;
52 }
45 return priority == ThreadPriority::REALTIME_AUDIO && 53 return priority == ThreadPriority::REALTIME_AUDIO &&
46 pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0; 54 pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0;
47 #else 55 #else
48 return false; 56 return false;
49 #endif 57 #endif
50 } 58 }
51 59
52 bool GetThreadPriorityForPlatform(PlatformThreadHandle handle, 60 bool GetCurrentThreadPriorityForPlatform(ThreadPriority* priority) {
53 ThreadPriority* priority) {
54 #if !defined(OS_NACL) 61 #if !defined(OS_NACL)
55 int maybe_sched_rr = 0; 62 int maybe_sched_rr = 0;
56 struct sched_param maybe_realtime_prio = {0}; 63 struct sched_param maybe_realtime_prio = {0};
57 // TODO(gab): Assess the correctness of using |pthread_self()| below instead
58 // of |handle|. http://crbug.com/468793.
59 if (pthread_getschedparam(pthread_self(), &maybe_sched_rr, 64 if (pthread_getschedparam(pthread_self(), &maybe_sched_rr,
60 &maybe_realtime_prio) == 0 && 65 &maybe_realtime_prio) == 0 &&
61 maybe_sched_rr == SCHED_RR && 66 maybe_sched_rr == SCHED_RR &&
62 maybe_realtime_prio.sched_priority == kRealTimePrio.sched_priority) { 67 maybe_realtime_prio.sched_priority == kRealTimePrio.sched_priority) {
63 *priority = ThreadPriority::REALTIME_AUDIO; 68 *priority = ThreadPriority::REALTIME_AUDIO;
64 return true; 69 return true;
65 } 70 }
66 #endif 71 #endif
67 return false; 72 return false;
68 } 73 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 #if !defined(THREAD_SANITIZER) 109 #if !defined(THREAD_SANITIZER)
105 return 0; 110 return 0;
106 #else 111 #else
107 // ThreadSanitizer bloats the stack heavily. Evidence has been that the 112 // ThreadSanitizer bloats the stack heavily. Evidence has been that the
108 // default stack size isn't enough for some browser tests. 113 // default stack size isn't enough for some browser tests.
109 return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux). 114 return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux).
110 #endif 115 #endif
111 } 116 }
112 117
113 } // namespace base 118 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698