Port Android relocation packer to chromium build
[chromium-blink-merge.git] / base / threading / platform_thread_android.cc
blobaab4c1917dc9bf1c871267dac63e680b9b935d04
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"
7 #include <errno.h>
8 #include <sys/prctl.h>
9 #include <sys/resource.h>
10 #include <sys/types.h>
11 #include <unistd.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"
22 namespace base {
24 namespace internal {
26 // These nice values are taken from Android, which uses nice values like linux,
27 // but defines some preset nice values.
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},
42 {kThreadPriority_Background, 10},
43 {kThreadPriority_Normal, 0},
44 {kThreadPriority_Display, -6},
47 bool HandleSetThreadPriorityForPlatform(PlatformThreadHandle handle,
48 ThreadPriority priority) {
49 // On Android, we set the Audio priority through JNI as Audio priority
50 // will also allow the process to run while it is backgrounded.
51 if (priority == kThreadPriority_RealtimeAudio) {
52 JNIEnv* env = base::android::AttachCurrentThread();
53 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId());
54 return true;
56 return false;
59 } // namespace internal
61 void PlatformThread::SetName(const char* name) {
62 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
63 tracked_objects::ThreadData::InitializeThreadContext(name);
65 // Like linux, on android we can get the thread names to show up in the
66 // debugger by setting the process name for the LWP.
67 // We don't want to do this for the main thread because that would rename
68 // the process, causing tools like killall to stop working.
69 if (PlatformThread::CurrentId() == getpid())
70 return;
72 // Set the name for the LWP (which gets truncated to 15 characters).
73 int err = prctl(PR_SET_NAME, name);
74 if (err < 0 && errno != EPERM)
75 DPLOG(ERROR) << "prctl(PR_SET_NAME)";
79 void InitThreading() {
82 void InitOnThread() {
83 // Threads on linux/android may inherit their priority from the thread
84 // where they were created. This sets all new threads to the default.
85 PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(),
86 kThreadPriority_Normal);
89 void TerminateOnThread() {
90 base::android::DetachFromVM();
93 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
94 #if !defined(ADDRESS_SANITIZER)
95 return 0;
96 #else
97 // AddressSanitizer bloats the stack approximately 2x. Default stack size of
98 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example).
99 return 2 * (1 << 20); // 2Mb
100 #endif
103 bool RegisterThreadUtils(JNIEnv* env) {
104 return RegisterNativesImpl(env);
107 } // namespace base