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

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

Issue 1015503002: Deduplicate PlatformThread's POSIX implementations of SetThreadPriority. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@background_threads
Patch Set: more fixes Created 5 years, 9 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 <sys/prctl.h> 8 #include <sys/prctl.h>
9 #include <sys/resource.h> 9 #include <sys/resource.h>
10 #include <sys/types.h>
11 #include <unistd.h>
10 12
11 #include "base/android/jni_android.h" 13 #include "base/android/jni_android.h"
12 #include "base/android/thread_utils.h" 14 #include "base/android/thread_utils.h"
13 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
14 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/threading/platform_thread_internal_posix.h"
15 #include "base/threading/thread_id_name_manager.h" 18 #include "base/threading/thread_id_name_manager.h"
16 #include "base/tracked_objects.h" 19 #include "base/tracked_objects.h"
17 #include "jni/ThreadUtils_jni.h" 20 #include "jni/ThreadUtils_jni.h"
18 21
19 namespace base { 22 namespace base {
20 23
21 namespace { 24 namespace internal {
22 int ThreadNiceValue(ThreadPriority priority) {
23 // These nice values are taken from Android, which uses nice
24 // values like linux, but defines some preset nice values.
25 // Process.THREAD_PRIORITY_AUDIO = -16
26 // Process.THREAD_PRIORITY_BACKGROUND = 10
27 // Process.THREAD_PRIORITY_DEFAULT = 0;
28 // Process.THREAD_PRIORITY_DISPLAY = -4;
29 // Process.THREAD_PRIORITY_FOREGROUND = -2;
30 // Process.THREAD_PRIORITY_LESS_FAVORABLE = 1;
31 // Process.THREAD_PRIORITY_LOWEST = 19;
32 // Process.THREAD_PRIORITY_MORE_FAVORABLE = -1;
33 // Process.THREAD_PRIORITY_URGENT_AUDIO = -19;
34 // Process.THREAD_PRIORITY_URGENT_DISPLAY = -8;
35 // We use -6 for display, but we may want to split this
36 // into urgent (-8) and non-urgent (-4).
37 static const int threadPriorityAudio = -16;
38 static const int threadPriorityBackground = 10;
39 static const int threadPriorityDefault = 0;
40 static const int threadPriorityDisplay = -6;
41 switch (priority) {
42 case kThreadPriority_RealtimeAudio:
43 return threadPriorityAudio;
44 case kThreadPriority_Background:
45 return threadPriorityBackground;
46 case kThreadPriority_Normal:
47 return threadPriorityDefault;
48 case kThreadPriority_Display:
49 return threadPriorityDisplay;
50 default:
51 NOTREACHED() << "Unknown priority.";
52 return 0;
53 }
54 }
55 } // namespace
56 25
57 //static 26 // These nice values are taken from Android, which uses nice values like linux,
58 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, 27 // but defines some preset nice values.
59 ThreadPriority priority) { 28 // Process.THREAD_PRIORITY_AUDIO = -16
29 // Process.THREAD_PRIORITY_BACKGROUND = 10
30 // Process.THREAD_PRIORITY_DEFAULT = 0;
31 // Process.THREAD_PRIORITY_DISPLAY = -4;
32 // Process.THREAD_PRIORITY_FOREGROUND = -2;
33 // Process.THREAD_PRIORITY_LESS_FAVORABLE = 1;
34 // Process.THREAD_PRIORITY_LOWEST = 19;
35 // Process.THREAD_PRIORITY_MORE_FAVORABLE = -1;
36 // Process.THREAD_PRIORITY_URGENT_AUDIO = -19;
37 // Process.THREAD_PRIORITY_URGENT_DISPLAY = -8;
38 // We use -6 for display, but we may want to split this into urgent (-8) and
39 // non-urgent (-4).
40 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = {
41 {kThreadPriority_RealtimeAudio, -16},
rvargas (doing something else) 2015/03/17 22:15:21 nit: spaces after { and before }
gab 2015/03/18 19:01:05 I don't feel strongly either way, but this is the
rvargas (doing something else) 2015/03/18 22:54:22 Looks like this is the correct format these days.
42 {kThreadPriority_Background, 10},
43 {kThreadPriority_Normal, 0},
44 {kThreadPriority_Display, -6},
45 };
46
47 bool HandleSetThreadPriorityForPlatform(PlatformThreadHandle handle,
48 ThreadPriority priority) {
60 // On Android, we set the Audio priority through JNI as Audio priority 49 // On Android, we set the Audio priority through JNI as Audio priority
61 // will also allow the process to run while it is backgrounded. 50 // will also allow the process to run while it is backgrounded.
62 if (priority == kThreadPriority_RealtimeAudio) { 51 if (priority == kThreadPriority_RealtimeAudio) {
63 JNIEnv* env = base::android::AttachCurrentThread(); 52 JNIEnv* env = base::android::AttachCurrentThread();
64 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId()); 53 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId());
65 return; 54 return true;
66 } 55 }
56 return false;
57 }
67 58
68 // setpriority(2) should change the whole thread group's (i.e. process) 59 } // namespace internal
69 // priority. however, on linux it will only change the target thread's
70 // priority. see the bugs section in
71 // http://man7.org/linux/man-pages/man2/getpriority.2.html.
72 // we prefer using 0 rather than the current thread id since they are
73 // equivalent but it makes sandboxing easier (https://crbug.com/399473).
74 DCHECK_NE(handle.id_, kInvalidThreadId);
75 int kNiceSetting = ThreadNiceValue(priority);
76 const PlatformThreadId current_id = PlatformThread::CurrentId();
77 if (setpriority(PRIO_PROCESS,
78 handle.id_ == current_id ? 0 : handle.id_,
79 kNiceSetting)) {
80 LOG(ERROR) << "Failed to set nice value of thread to " << kNiceSetting;
81 }
82 }
83 60
84 void PlatformThread::SetName(const char* name) { 61 void PlatformThread::SetName(const char* name) {
85 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); 62 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
86 tracked_objects::ThreadData::InitializeThreadContext(name); 63 tracked_objects::ThreadData::InitializeThreadContext(name);
87 64
88 // Like linux, on android we can get the thread names to show up in the 65 // Like linux, on android we can get the thread names to show up in the
89 // debugger by setting the process name for the LWP. 66 // debugger by setting the process name for the LWP.
90 // We don't want to do this for the main thread because that would rename 67 // We don't want to do this for the main thread because that would rename
91 // the process, causing tools like killall to stop working. 68 // the process, causing tools like killall to stop working.
92 if (PlatformThread::CurrentId() == getpid()) 69 if (PlatformThread::CurrentId() == getpid())
(...skipping 28 matching lines...) Expand all
121 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example). 98 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example).
122 return 2 * (1 << 20); // 2Mb 99 return 2 * (1 << 20); // 2Mb
123 #endif 100 #endif
124 } 101 }
125 102
126 bool RegisterThreadUtils(JNIEnv* env) { 103 bool RegisterThreadUtils(JNIEnv* env) {
127 return RegisterNativesImpl(env); 104 return RegisterNativesImpl(env);
128 } 105 }
129 106
130 } // namespace base 107 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698