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/resource.h> |
| 9 |
| 10 #include "base/android/jni_android.h" |
| 11 #include "base/android/thread_utils.h" |
| 12 #include "base/lazy_instance.h" |
| 13 #include "base/logging.h" |
| 14 #include "jni/ThreadUtils_jni.h" |
| 15 |
| 16 namespace base { |
| 17 |
| 18 namespace { |
| 19 int ThreadNiceValue(ThreadPriority priority) { |
| 20 // These nice values are taken from Android, which uses nice |
| 21 // values like linux, but defines some preset nice values. |
| 22 // Process.THREAD_PRIORITY_AUDIO = -16 |
| 23 // Process.THREAD_PRIORITY_BACKGROUND = 10 |
| 24 // Process.THREAD_PRIORITY_DEFAULT = 0; |
| 25 // Process.THREAD_PRIORITY_DISPLAY = -4; |
| 26 // Process.THREAD_PRIORITY_FOREGROUND = -2; |
| 27 // Process.THREAD_PRIORITY_LESS_FAVORABLE = 1; |
| 28 // Process.THREAD_PRIORITY_LOWEST = 19; |
| 29 // Process.THREAD_PRIORITY_MORE_FAVORABLE = -1; |
| 30 // Process.THREAD_PRIORITY_URGENT_AUDIO = -19; |
| 31 // Process.THREAD_PRIORITY_URGENT_DISPLAY = -8; |
| 32 // We use -6 for display, but we may want to split this |
| 33 // into urgent (-8) and non-urgent (-4). |
| 34 static const int threadPriorityAudio = -16; |
| 35 static const int threadPriorityBackground = 10; |
| 36 static const int threadPriorityDefault = 0; |
| 37 static const int threadPriorityDisplay = -6; |
| 38 switch (priority) { |
| 39 case kThreadPriority_RealtimeAudio: |
| 40 return threadPriorityAudio; |
| 41 case kThreadPriority_Background: |
| 42 return threadPriorityBackground; |
| 43 case kThreadPriority_Normal: |
| 44 return threadPriorityDefault; |
| 45 case kThreadPriority_Display: |
| 46 return threadPriorityDisplay; |
| 47 default: |
| 48 NOTREACHED() << "Unknown priority."; |
| 49 return 0; |
| 50 } |
| 51 } |
| 52 } // namespace |
| 53 |
| 54 //static |
| 55 void PlatformThread::SetThreadPriority(PlatformThreadHandle, |
| 56 PlatformThreadId id, |
| 57 ThreadPriority priority) { |
| 58 // On Android, we set the Audio priority through JNI as Audio priority |
| 59 // will also allow the process to run while it is backgrounded. |
| 60 if (priority == kThreadPriority_RealtimeAudio) { |
| 61 JNIEnv* env = base::android::AttachCurrentThread(); |
| 62 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId()); |
| 63 return; |
| 64 } |
| 65 |
| 66 // setpriority(2) will set a thread's priority if it is passed a tid as |
| 67 // the 'process identifier', not affecting the rest of the threads in the |
| 68 // process. Setting this priority will only succeed if the user has been |
| 69 // granted permission to adjust nice values on the system. |
| 70 DCHECK_NE(id, kInvalidThreadId); |
| 71 int kNiceSetting = ThreadNiceValue(priority); |
| 72 if (setpriority(PRIO_PROCESS, id, kNiceSetting)) |
| 73 LOG(ERROR) << "Failed to set nice value of thread to " << kNiceSetting; |
| 74 } |
| 75 |
| 76 void InitOnThread() { |
| 77 // Threads on linux/android may inherit their priority from the thread |
| 78 // where they were created. This sets all new threads to the default. |
| 79 PlatformThread::SetThreadPriority(pthread_self(), |
| 80 PlatformThread::CurrentId(), |
| 81 kThreadPriority_Normal); |
| 82 } |
| 83 |
| 84 void TerminateOnThread() { |
| 85 base::android::DetachFromVM(); |
| 86 } |
| 87 |
| 88 bool RegisterThreadUtils(JNIEnv* env) { |
| 89 return RegisterNativesImpl(env); |
| 90 } |
| 91 |
| 92 } // namespace base |
OLD | NEW |