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 11#include "base/android/jni_android.h" 12#include "base/android/thread_utils.h" 13#include "base/lazy_instance.h" 14#include "base/logging.h" 15#include "base/threading/thread_id_name_manager.h" 16#include "base/tracked_objects.h" 17#include "jni/ThreadUtils_jni.h" 18 19namespace base { 20 21namespace { 22int ThreadNiceValue(ThreadPriority priority) { 23 // These nice values are taken from Android, which uses nice 24 // values like linux, but defines some preset nice values. 25 // Process.THREAD_PRIORITY_AUDIO = -16 26 // Process.THREAD_PRIORITY_BACKGROUND = 10 27 // Process.THREAD_PRIORITY_DEFAULT = 0; 28 // Process.THREAD_PRIORITY_DISPLAY = -4; 29 // Process.THREAD_PRIORITY_FOREGROUND = -2; 30 // Process.THREAD_PRIORITY_LESS_FAVORABLE = 1; 31 // Process.THREAD_PRIORITY_LOWEST = 19; 32 // Process.THREAD_PRIORITY_MORE_FAVORABLE = -1; 33 // Process.THREAD_PRIORITY_URGENT_AUDIO = -19; 34 // Process.THREAD_PRIORITY_URGENT_DISPLAY = -8; 35 // We use -6 for display, but we may want to split this 36 // into urgent (-8) and non-urgent (-4). 37 static const int threadPriorityAudio = -16; 38 static const int threadPriorityBackground = 10; 39 static const int threadPriorityDefault = 0; 40 static const int threadPriorityDisplay = -6; 41 switch (priority) { 42 case kThreadPriority_RealtimeAudio: 43 return threadPriorityAudio; 44 case kThreadPriority_Background: 45 return threadPriorityBackground; 46 case kThreadPriority_Normal: 47 return threadPriorityDefault; 48 case kThreadPriority_Display: 49 return threadPriorityDisplay; 50 default: 51 NOTREACHED() << "Unknown priority."; 52 return 0; 53 } 54} 55} // namespace 56 57//static 58void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, 59 ThreadPriority priority) { 60 // On Android, we set the Audio priority through JNI as Audio priority 61 // will also allow the process to run while it is backgrounded. 62 if (priority == kThreadPriority_RealtimeAudio) { 63 JNIEnv* env = base::android::AttachCurrentThread(); 64 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId()); 65 return; 66 } 67 68 // setpriority(2) will set a thread's priority if it is passed a tid as 69 // the 'process identifier', not affecting the rest of the threads in the 70 // process. Setting this priority will only succeed if the user has been 71 // granted permission to adjust nice values on the system. 72 DCHECK_NE(handle.id_, kInvalidThreadId); 73 int kNiceSetting = ThreadNiceValue(priority); 74 if (setpriority(PRIO_PROCESS, handle.id_, kNiceSetting)) 75 LOG(ERROR) << "Failed to set nice value of thread to " << kNiceSetting; 76} 77 78void PlatformThread::SetName(const char* name) { 79 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); 80 tracked_objects::ThreadData::InitializeThreadContext(name); 81 82 // Like linux, on android we can get the thread names to show up in the 83 // debugger by setting the process name for the LWP. 84 // We don't want to do this for the main thread because that would rename 85 // the process, causing tools like killall to stop working. 86 if (PlatformThread::CurrentId() == getpid()) 87 return; 88 89 // Set the name for the LWP (which gets truncated to 15 characters). 90 int err = prctl(PR_SET_NAME, name); 91 if (err < 0 && errno != EPERM) 92 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; 93} 94 95 96void InitThreading() { 97} 98 99void InitOnThread() { 100 // Threads on linux/android may inherit their priority from the thread 101 // where they were created. This sets all new threads to the default. 102 PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(), 103 kThreadPriority_Normal); 104} 105 106void TerminateOnThread() { 107 base::android::DetachFromVM(); 108} 109 110size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { 111 return 0; 112} 113 114bool RegisterThreadUtils(JNIEnv* env) { 115 return RegisterNativesImpl(env); 116} 117 118} // namespace base 119