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
3 // found in the LICENSE file.
5 #include "base/threading/platform_thread.h"
9 #include <sys/resource.h>
11 #include "base/android/jni_android.h"
12 #include "base/android/thread_utils.h"
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/threading/thread_id_name_manager.h"
16 #include "base/tracked_objects.h"
17 #include "jni/ThreadUtils_jni.h"
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;
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
;
51 NOTREACHED() << "Unknown priority.";
58 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle
,
59 ThreadPriority priority
) {
60 // On Android, we set the Audio priority through JNI as Audio priority
61 // will also allow the process to run while it is backgrounded.
62 if (priority
== kThreadPriority_RealtimeAudio
) {
63 JNIEnv
* env
= base::android::AttachCurrentThread();
64 Java_ThreadUtils_setThreadPriorityAudio(env
, PlatformThread::CurrentId());
68 // setpriority(2) should change the whole thread group's (i.e. process)
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_
,
80 LOG(ERROR
) << "Failed to set nice value of thread to " << kNiceSetting
;
84 void PlatformThread::SetName(const char* name
) {
85 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name
);
86 tracked_objects::ThreadData::InitializeThreadContext(name
);
88 // 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.
90 // 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.
92 if (PlatformThread::CurrentId() == getpid())
95 // Set the name for the LWP (which gets truncated to 15 characters).
96 int err
= prctl(PR_SET_NAME
, name
);
97 if (err
< 0 && errno
!= EPERM
)
98 DPLOG(ERROR
) << "prctl(PR_SET_NAME)";
102 void InitThreading() {
105 void InitOnThread() {
106 // Threads on linux/android may inherit their priority from the thread
107 // where they were created. This sets all new threads to the default.
108 PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(),
109 kThreadPriority_Normal
);
112 void TerminateOnThread() {
113 base::android::DetachFromVM();
116 size_t GetDefaultThreadStackSize(const pthread_attr_t
& attributes
) {
117 #if !defined(ADDRESS_SANITIZER)
120 // AddressSanitizer bloats the stack approximately 2x. Default stack size of
121 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example).
122 return 2 * (1 << 20); // 2Mb
126 bool RegisterThreadUtils(JNIEnv
* env
) {
127 return RegisterNativesImpl(env
);