OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/threading/platform_thread.h" | 5 #include "base/threading/platform_thread.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/prctl.h> | 8 #include <sys/prctl.h> |
9 #include <sys/resource.h> | 9 #include <sys/resource.h> |
10 | 10 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | 58 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, |
59 ThreadPriority priority) { | 59 ThreadPriority priority) { |
60 // On Android, we set the Audio priority through JNI as Audio 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. | 61 // will also allow the process to run while it is backgrounded. |
62 if (priority == kThreadPriority_RealtimeAudio) { | 62 if (priority == kThreadPriority_RealtimeAudio) { |
63 JNIEnv* env = base::android::AttachCurrentThread(); | 63 JNIEnv* env = base::android::AttachCurrentThread(); |
64 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId()); | 64 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId()); |
65 return; | 65 return; |
66 } | 66 } |
67 | 67 |
68 // setpriority(2) will set a thread's priority if it is passed a tid as | 68 // setpriority(2) should change the whole thread group's (i.e. process) |
69 // the 'process identifier', not affecting the rest of the threads in the | 69 // priority. however, on linux it will only change the target thread's |
70 // process. Setting this priority will only succeed if the user has been | 70 // priority. see the bugs section in |
71 // granted permission to adjust nice values on the system. | 71 // http://man7.org/linux/man-pages/man2/getpriority.2.html. |
| 72 // we prefer using 0 rather than the current thread id since they are |
| 73 // equivalent but it makes sandboxing easier (https://crbug.com/399473). |
72 DCHECK_NE(handle.id_, kInvalidThreadId); | 74 DCHECK_NE(handle.id_, kInvalidThreadId); |
73 int kNiceSetting = ThreadNiceValue(priority); | 75 int kNiceSetting = ThreadNiceValue(priority); |
74 if (setpriority(PRIO_PROCESS, handle.id_, kNiceSetting)) | 76 const PlatformThreadId current_id = PlatformThread::CurrentId(); |
| 77 if (setpriority(PRIO_PROCESS, |
| 78 handle.id_ == current_id ? 0 : handle.id_, |
| 79 kNiceSetting)) { |
75 LOG(ERROR) << "Failed to set nice value of thread to " << kNiceSetting; | 80 LOG(ERROR) << "Failed to set nice value of thread to " << kNiceSetting; |
| 81 } |
76 } | 82 } |
77 | 83 |
78 void PlatformThread::SetName(const char* name) { | 84 void PlatformThread::SetName(const char* name) { |
79 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); | 85 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); |
80 tracked_objects::ThreadData::InitializeThreadContext(name); | 86 tracked_objects::ThreadData::InitializeThreadContext(name); |
81 | 87 |
82 // Like linux, on android we can get the thread names to show up in the | 88 // 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. | 89 // 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 | 90 // 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. | 91 // the process, causing tools like killall to stop working. |
(...skipping 29 matching lines...) Expand all Loading... |
115 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example). | 121 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example). |
116 return 2 * (1 << 20); // 2Mb | 122 return 2 * (1 << 20); // 2Mb |
117 #endif | 123 #endif |
118 } | 124 } |
119 | 125 |
120 bool RegisterThreadUtils(JNIEnv* env) { | 126 bool RegisterThreadUtils(JNIEnv* env) { |
121 return RegisterNativesImpl(env); | 127 return RegisterNativesImpl(env); |
122 } | 128 } |
123 | 129 |
124 } // namespace base | 130 } // namespace base |
OLD | NEW |