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.
4
5 #include "base/threading/platform_thread.h"
6
7 #include <errno.h>
8 #include <stddef.h>
9 #include <sys/prctl.h>
10 #include <sys/resource.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13
14 #include "base/android/jni_android.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 "jni/ThreadUtils_jni.h"
20
21 namespace base {
22
23 namespace internal {
24
25 // - BACKGROUND corresponds to Android's PRIORITY_BACKGROUND = 10 value and can
26 // result in heavy throttling and force the thread onto a little core on
27 // big.LITTLE devices.
28 // - DISPLAY corresponds to Android's PRIORITY_DISPLAY = -4 value.
29 // - REALTIME_AUDIO corresponds to Android's PRIORITY_AUDIO = -16 value.
30 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = {
31 {ThreadPriority::BACKGROUND, 10},
32 {ThreadPriority::NORMAL, 0},
33 {ThreadPriority::DISPLAY, -4},
34 {ThreadPriority::REALTIME_AUDIO, -16},
35 };
36
SetCurrentThreadPriorityForPlatform(ThreadPriority priority)37 bool SetCurrentThreadPriorityForPlatform(ThreadPriority priority) {
38 // On Android, we set the Audio priority through JNI as Audio priority
39 // will also allow the process to run while it is backgrounded.
40 if (priority == ThreadPriority::REALTIME_AUDIO) {
41 JNIEnv* env = base::android::AttachCurrentThread();
42 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId());
43 return true;
44 }
45 return false;
46 }
47
GetCurrentThreadPriorityForPlatform(ThreadPriority * priority)48 bool GetCurrentThreadPriorityForPlatform(ThreadPriority* priority) {
49 DCHECK(priority);
50 *priority = ThreadPriority::NORMAL;
51 JNIEnv* env = base::android::AttachCurrentThread();
52 if (Java_ThreadUtils_isThreadPriorityAudio(
53 env, PlatformThread::CurrentId())) {
54 *priority = ThreadPriority::REALTIME_AUDIO;
55 return true;
56 }
57 return false;
58 }
59
60 } // namespace internal
61
SetName(const std::string & name)62 void PlatformThread::SetName(const std::string& name) {
63 ThreadIdNameManager::GetInstance()->SetName(name);
64
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;
71
72 // Set the name for the LWP (which gets truncated to 15 characters).
73 int err = prctl(PR_SET_NAME, name.c_str());
74 if (err < 0 && errno != EPERM)
75 DPLOG(ERROR) << "prctl(PR_SET_NAME)";
76 }
77
78
InitThreading()79 void InitThreading() {
80 }
81
TerminateOnThread()82 void TerminateOnThread() {
83 base::android::DetachFromVM();
84 }
85
GetDefaultThreadStackSize(const pthread_attr_t & attributes)86 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
87 #if !defined(ADDRESS_SANITIZER)
88 return 0;
89 #else
90 // AddressSanitizer bloats the stack approximately 2x. Default stack size of
91 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example).
92 return 2 * (1 << 20); // 2Mb
93 #endif
94 }
95
96 } // namespace base
97