| OLD | NEW |
| (Empty) |
| 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> | |
| 10 #include <sys/types.h> | |
| 11 #include <unistd.h> | |
| 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" | |
| 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" | |
| 21 | |
| 22 namespace base { | |
| 23 | |
| 24 namespace internal { | |
| 25 | |
| 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}, | |
| 38 }; | |
| 39 | |
| 40 bool SetCurrentThreadPriorityForPlatform(ThreadPriority priority) { | |
| 41 // On Android, we set the Audio priority through JNI as Audio priority | |
| 42 // will also allow the process to run while it is backgrounded. | |
| 43 if (priority == ThreadPriority::REALTIME_AUDIO) { | |
| 44 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 45 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId()); | |
| 46 return true; | |
| 47 } | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 bool GetCurrentThreadPriorityForPlatform(ThreadPriority* priority) { | |
| 52 // See http://crbug.com/505474. | |
| 53 NOTIMPLEMENTED(); | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 } // namespace internal | |
| 58 | |
| 59 void PlatformThread::SetName(const std::string& name) { | |
| 60 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); | |
| 61 tracked_objects::ThreadData::InitializeThreadContext(name); | |
| 62 | |
| 63 // Like linux, on android we can get the thread names to show up in the | |
| 64 // debugger by setting the process name for the LWP. | |
| 65 // We don't want to do this for the main thread because that would rename | |
| 66 // the process, causing tools like killall to stop working. | |
| 67 if (PlatformThread::CurrentId() == getpid()) | |
| 68 return; | |
| 69 | |
| 70 // Set the name for the LWP (which gets truncated to 15 characters). | |
| 71 int err = prctl(PR_SET_NAME, name.c_str()); | |
| 72 if (err < 0 && errno != EPERM) | |
| 73 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; | |
| 74 } | |
| 75 | |
| 76 | |
| 77 void InitThreading() { | |
| 78 } | |
| 79 | |
| 80 void InitOnThread() { | |
| 81 // Threads on linux/android may inherit their priority from the thread | |
| 82 // where they were created. This sets all new threads to the default. | |
| 83 PlatformThread::SetCurrentThreadPriority(ThreadPriority::NORMAL); | |
| 84 } | |
| 85 | |
| 86 void TerminateOnThread() { | |
| 87 base::android::DetachFromVM(); | |
| 88 } | |
| 89 | |
| 90 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { | |
| 91 #if !defined(ADDRESS_SANITIZER) | |
| 92 return 0; | |
| 93 #else | |
| 94 // AddressSanitizer bloats the stack approximately 2x. Default stack size of | |
| 95 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example). | |
| 96 return 2 * (1 << 20); // 2Mb | |
| 97 #endif | |
| 98 } | |
| 99 | |
| 100 bool RegisterThreadUtils(JNIEnv* env) { | |
| 101 return RegisterNativesImpl(env); | |
| 102 } | |
| 103 | |
| 104 } // namespace base | |
| OLD | NEW |