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>
10 #include <sys/types.h>
13 #include "base/android/jni_android.h"
14 #include "base/android/thread_utils.h"
15 #include "base/lazy_instance.h"
16 #include "base/logging.h"
17 #include "base/threading/platform_thread_internal_posix.h"
18 #include "base/threading/thread_id_name_manager.h"
19 #include "base/tracked_objects.h"
20 #include "jni/ThreadUtils_jni.h"
26 // - BACKGROUND is 9 due to it being the nicest value we can use that's still
27 // above an Android system threshold that enables heavy throttling starting at
28 // 10; we want to be lower-priority than Chrome's other threads without
29 // incurring this behavior.
30 // - DISPLAY is -6 due to being midway between Android's DISPLAY (-4) and
31 // URGENT_DISPLAY (-8).
32 // - REALTIME_AUDIO corresponds to Android's THREAD_PRIORITY_AUDIO = -16 value.
33 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap
[4] = {
34 {ThreadPriority::BACKGROUND
, 9},
35 {ThreadPriority::NORMAL
, 0},
36 {ThreadPriority::DISPLAY
, -6},
37 {ThreadPriority::REALTIME_AUDIO
, -16},
40 bool SetThreadPriorityForPlatform(PlatformThreadHandle handle
,
41 ThreadPriority priority
) {
42 // On Android, we set the Audio priority through JNI as Audio priority
43 // will also allow the process to run while it is backgrounded.
44 if (priority
== ThreadPriority::REALTIME_AUDIO
) {
45 JNIEnv
* env
= base::android::AttachCurrentThread();
46 Java_ThreadUtils_setThreadPriorityAudio(env
, PlatformThread::CurrentId());
52 bool GetThreadPriorityForPlatform(PlatformThreadHandle handle
,
53 ThreadPriority
* priority
) {
58 } // namespace internal
60 void PlatformThread::SetName(const std::string
& name
) {
61 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name
);
62 tracked_objects::ThreadData::InitializeThreadContext(name
);
64 // Like linux, on android we can get the thread names to show up in the
65 // debugger by setting the process name for the LWP.
66 // We don't want to do this for the main thread because that would rename
67 // the process, causing tools like killall to stop working.
68 if (PlatformThread::CurrentId() == getpid())
71 // Set the name for the LWP (which gets truncated to 15 characters).
72 int err
= prctl(PR_SET_NAME
, name
.c_str());
73 if (err
< 0 && errno
!= EPERM
)
74 DPLOG(ERROR
) << "prctl(PR_SET_NAME)";
78 void InitThreading() {
82 // Threads on linux/android may inherit their priority from the thread
83 // where they were created. This sets all new threads to the default.
84 PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(),
85 ThreadPriority::NORMAL
);
88 void TerminateOnThread() {
89 base::android::DetachFromVM();
92 size_t GetDefaultThreadStackSize(const pthread_attr_t
& attributes
) {
93 #if !defined(ADDRESS_SANITIZER)
96 // AddressSanitizer bloats the stack approximately 2x. Default stack size of
97 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example).
98 return 2 * (1 << 20); // 2Mb
102 bool RegisterThreadUtils(JNIEnv
* env
) {
103 return RegisterNativesImpl(env
);