James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 <sys/prctl.h> |
| 9 | #include <sys/resource.h> |
Alhaad Gokhale | 4f51307 | 2015-03-24 10:49:34 -0700 | [diff] [blame] | 10 | #include <sys/types.h> |
| 11 | #include <unistd.h> |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 12 | |
| 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" |
Alhaad Gokhale | 4f51307 | 2015-03-24 10:49:34 -0700 | [diff] [blame] | 17 | #include "base/threading/platform_thread_internal_posix.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 18 | #include "base/threading/thread_id_name_manager.h" |
| 19 | #include "base/tracked_objects.h" |
| 20 | #include "jni/ThreadUtils_jni.h" |
| 21 | |
| 22 | namespace base { |
| 23 | |
Alhaad Gokhale | 4f51307 | 2015-03-24 10:49:34 -0700 | [diff] [blame] | 24 | namespace internal { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 25 | |
Alhaad Gokhale | 4f51307 | 2015-03-24 10:49:34 -0700 | [diff] [blame] | 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] = { |
James Robinson | 0fae000 | 2015-05-05 16:31:51 -0700 | [diff] [blame] | 41 | {ThreadPriority::BACKGROUND, 10}, |
| 42 | {ThreadPriority::NORMAL, 0}, |
| 43 | {ThreadPriority::DISPLAY, -6}, |
| 44 | {ThreadPriority::REALTIME_AUDIO, -16}, |
Alhaad Gokhale | 4f51307 | 2015-03-24 10:49:34 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
James Robinson | 0fae000 | 2015-05-05 16:31:51 -0700 | [diff] [blame] | 47 | bool SetThreadPriorityForPlatform(PlatformThreadHandle handle, |
| 48 | ThreadPriority priority) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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. |
James Robinson | 0fae000 | 2015-05-05 16:31:51 -0700 | [diff] [blame] | 51 | if (priority == ThreadPriority::REALTIME_AUDIO) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 52 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 53 | Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId()); |
Alhaad Gokhale | 4f51307 | 2015-03-24 10:49:34 -0700 | [diff] [blame] | 54 | return true; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 55 | } |
Alhaad Gokhale | 4f51307 | 2015-03-24 10:49:34 -0700 | [diff] [blame] | 56 | return false; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 57 | } |
| 58 | |
James Robinson | 0fae000 | 2015-05-05 16:31:51 -0700 | [diff] [blame] | 59 | bool GetThreadPriorityForPlatform(PlatformThreadHandle handle, |
| 60 | ThreadPriority* priority) { |
| 61 | NOTIMPLEMENTED(); |
| 62 | return false; |
| 63 | } |
| 64 | |
Alhaad Gokhale | 4f51307 | 2015-03-24 10:49:34 -0700 | [diff] [blame] | 65 | } // namespace internal |
| 66 | |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 67 | void PlatformThread::SetName(const std::string& name) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 68 | ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); |
| 69 | tracked_objects::ThreadData::InitializeThreadContext(name); |
| 70 | |
| 71 | // Like linux, on android we can get the thread names to show up in the |
| 72 | // debugger by setting the process name for the LWP. |
| 73 | // We don't want to do this for the main thread because that would rename |
| 74 | // the process, causing tools like killall to stop working. |
| 75 | if (PlatformThread::CurrentId() == getpid()) |
| 76 | return; |
| 77 | |
| 78 | // Set the name for the LWP (which gets truncated to 15 characters). |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 79 | int err = prctl(PR_SET_NAME, name.c_str()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 80 | if (err < 0 && errno != EPERM) |
| 81 | DPLOG(ERROR) << "prctl(PR_SET_NAME)"; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | void InitThreading() { |
| 86 | } |
| 87 | |
| 88 | void InitOnThread() { |
| 89 | // Threads on linux/android may inherit their priority from the thread |
| 90 | // where they were created. This sets all new threads to the default. |
| 91 | PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(), |
James Robinson | 0fae000 | 2015-05-05 16:31:51 -0700 | [diff] [blame] | 92 | ThreadPriority::NORMAL); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void TerminateOnThread() { |
| 96 | base::android::DetachFromVM(); |
| 97 | } |
| 98 | |
| 99 | size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { |
| 100 | #if !defined(ADDRESS_SANITIZER) |
| 101 | return 0; |
| 102 | #else |
| 103 | // AddressSanitizer bloats the stack approximately 2x. Default stack size of |
| 104 | // 1Mb is not enough for some tests (see http://crbug.com/263749 for example). |
| 105 | return 2 * (1 << 20); // 2Mb |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | bool RegisterThreadUtils(JNIEnv* env) { |
| 110 | return RegisterNativesImpl(env); |
| 111 | } |
| 112 | |
| 113 | } // namespace base |