Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: base/threading/platform_thread_posix.cc

Issue 12741012: base: Support setting thread priorities generically. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <sched.h> 8 #include <sched.h>
9 9
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/safe_strerror_posix.h" 13 #include "base/safe_strerror_posix.h"
14 #include "base/synchronization/waitable_event.h"
14 #include "base/threading/thread_id_name_manager.h" 15 #include "base/threading/thread_id_name_manager.h"
15 #include "base/threading/thread_restrictions.h" 16 #include "base/threading/thread_restrictions.h"
16 #include "base/tracked_objects.h" 17 #include "base/tracked_objects.h"
17 18
18 #if defined(OS_MACOSX) 19 #if defined(OS_MACOSX)
19 #include <sys/resource.h> 20 #include <sys/resource.h>
20 #include <algorithm> 21 #include <algorithm>
21 #endif 22 #endif
22 23
23 #if defined(OS_LINUX) 24 #if defined(OS_LINUX)
24 #include <sys/prctl.h> 25 #include <sys/prctl.h>
25 #include <sys/resource.h> 26 #include <sys/resource.h>
26 #include <sys/syscall.h> 27 #include <sys/syscall.h>
27 #include <sys/time.h> 28 #include <sys/time.h>
28 #include <unistd.h> 29 #include <unistd.h>
29 #endif 30 #endif
30 31
31 #if defined(OS_ANDROID)
32 #include <sys/resource.h>
33 #include "base/android/thread_utils.h"
34 #include "jni/ThreadUtils_jni.h"
35 #endif
36
37 namespace base { 32 namespace base {
38 33
39 #if defined(OS_MACOSX)
40 void InitThreading(); 34 void InitThreading();
41 #endif 35 void InitOnThread();
36 void TerminateOnThread();
37 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes);
42 38
43 namespace { 39 namespace {
44 40
45 struct ThreadParams { 41 struct ThreadParams {
42 ThreadParams()
43 : delegate(NULL),
jar (doing other things) 2013/05/17 22:51:48 nit: indent colon by 4 (2 more than currently), an
epenner 2013/05/17 23:46:35 Done.
44 joinable(false),
45 priority(kThreadPriority_Normal),
46 handle(NULL),
47 thread_id_set(false, false) {
48 }
49
46 PlatformThread::Delegate* delegate; 50 PlatformThread::Delegate* delegate;
47 bool joinable; 51 bool joinable;
48 ThreadPriority priority; 52 ThreadPriority priority;
53 PlatformThreadHandle* handle;
54 WaitableEvent thread_id_set;
49 }; 55 };
50 56
51 void SetCurrentThreadPriority(ThreadPriority priority) { 57 void* ThreadFunc(void* params) {
52 #if defined(OS_LINUX) || defined(OS_ANDROID) 58 base::InitOnThread();
53 switch (priority) { 59 ThreadParams* thread_params = static_cast<ThreadParams*>(params);
54 case kThreadPriority_Normal:
55 NOTREACHED() << "Don't reset priority as not all processes can.";
56 break;
57 case kThreadPriority_RealtimeAudio:
58 #if defined(OS_LINUX)
59 const int kNiceSetting = -10;
60 // Linux isn't posix compliant with setpriority(2), it will set a thread
61 // priority if it is passed a tid, not affecting the rest of the threads
62 // in the process. Setting this priority will only succeed if the user
63 // has been granted permission to adjust nice values on the system.
64 if (setpriority(PRIO_PROCESS, PlatformThread::CurrentId(), kNiceSetting))
65 DVLOG(1) << "Failed to set nice value of thread to " << kNiceSetting;
66 #elif defined(OS_ANDROID)
67 JNIEnv* env = base::android::AttachCurrentThread();
68 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId());
69 #endif // defined(OS_LINUX)
70 break;
71 }
72 #else // !defined(OS_LINUX) && !defined(OS_ANDROID)
73 PlatformThread::SetThreadPriority(pthread_self(), priority);
74 #endif
75 }
76 60
77 void* ThreadFunc(void* params) {
78 #if defined(OS_ANDROID)
79 // Threads on linux/android may inherit their priority from the thread
80 // where they were created. This sets all threads to the default.
81 // TODO(epenner): Move thread priorities to base. (crbug.com/170549)
82 if (setpriority(PRIO_PROCESS, PlatformThread::CurrentId(), 0))
83 DVLOG(1) << "Failed to reset initial thread nice value to zero.";
84 #endif
85 ThreadParams* thread_params = static_cast<ThreadParams*>(params);
86 PlatformThread::Delegate* delegate = thread_params->delegate; 61 PlatformThread::Delegate* delegate = thread_params->delegate;
87 if (!thread_params->joinable) 62 if (!thread_params->joinable)
88 base::ThreadRestrictions::SetSingletonAllowed(false); 63 base::ThreadRestrictions::SetSingletonAllowed(false);
89 64
90 // If there is a non-default priority for this thread, set it now. 65 if (thread_params->priority != kThreadPriority_Normal) {
91 if (thread_params->priority != kThreadPriority_Normal) 66 PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(),
92 SetCurrentThreadPriority(thread_params->priority); 67 thread_params->priority);
68 }
93 69
94 delete thread_params; 70 // Stash the id in the handle so the calling thread has a complete
71 // handle, and unblock the parent thread.
72 thread_params->handle->id_ = PlatformThread::CurrentId();
73 thread_params->thread_id_set.Signal();
74
95 delegate->ThreadMain(); 75 delegate->ThreadMain();
96 #if defined(OS_ANDROID) 76
97 base::android::DetachFromVM(); 77 base::TerminateOnThread();
98 #endif
99 return NULL; 78 return NULL;
100 } 79 }
101 80
102 bool CreateThread(size_t stack_size, bool joinable, 81 bool CreateThread(size_t stack_size, bool joinable,
103 PlatformThread::Delegate* delegate, 82 PlatformThread::Delegate* delegate,
104 PlatformThreadHandle* thread_handle, 83 PlatformThreadHandle* thread_handle,
105 ThreadPriority priority) { 84 ThreadPriority priority) {
106 #if defined(OS_MACOSX)
107 base::InitThreading(); 85 base::InitThreading();
108 #endif // OS_MACOSX
109 86
110 bool success = false; 87 bool success = false;
111 pthread_attr_t attributes; 88 pthread_attr_t attributes;
112 pthread_attr_init(&attributes); 89 pthread_attr_init(&attributes);
113 90
114 // Pthreads are joinable by default, so only specify the detached attribute if 91 // Pthreads are joinable by default, so only specify the detached
115 // the thread should be non-joinable. 92 // attribute if the thread should be non-joinable.
116 if (!joinable) { 93 if (!joinable) {
117 pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); 94 pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
118 } 95 }
119 96
120 #if defined(OS_MACOSX) && !defined(OS_IOS) 97 // Get a better default if available.
121 // The Mac OS X default for a pthread stack size is 512kB. 98 if (stack_size == 0)
122 // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses 99 stack_size = base::GetDefaultThreadStackSize(attributes);
123 // DEFAULT_STACK_SIZE for this purpose.
124 //
125 // 512kB isn't quite generous enough for some deeply recursive threads that
126 // otherwise request the default stack size by specifying 0. Here, adopt
127 // glibc's behavior as on Linux, which is to use the current stack size
128 // limit (ulimit -s) as the default stack size. See
129 // glibc-2.11.1/nptl/nptl-init.c's __pthread_initialize_minimal_internal. To
130 // avoid setting the limit below the Mac OS X default or the minimum usable
131 // stack size, these values are also considered. If any of these values
132 // can't be determined, or if stack size is unlimited (ulimit -s unlimited),
133 // stack_size is left at 0 to get the system default.
134 //
135 // Mac OS X normally only applies ulimit -s to the main thread stack. On
136 // contemporary OS X and Linux systems alike, this value is generally 8MB
137 // or in that neighborhood.
138 if (stack_size == 0) {
139 size_t default_stack_size;
140 struct rlimit stack_rlimit;
141 if (pthread_attr_getstacksize(&attributes, &default_stack_size) == 0 &&
142 getrlimit(RLIMIT_STACK, &stack_rlimit) == 0 &&
143 stack_rlimit.rlim_cur != RLIM_INFINITY) {
144 stack_size = std::max(std::max(default_stack_size,
145 static_cast<size_t>(PTHREAD_STACK_MIN)),
146 static_cast<size_t>(stack_rlimit.rlim_cur));
147 }
148 }
149 #endif // OS_MACOSX && !OS_IOS
150 100
151 if (stack_size > 0) 101 if (stack_size > 0)
152 pthread_attr_setstacksize(&attributes, stack_size); 102 pthread_attr_setstacksize(&attributes, stack_size);
153 103
154 ThreadParams* params = new ThreadParams; 104 ThreadParams params;
155 params->delegate = delegate; 105 params.delegate = delegate;
156 params->joinable = joinable; 106 params.joinable = joinable;
157 params->priority = priority; 107 params.priority = priority;
108 params.handle = thread_handle;
158 109
159 int err = pthread_create(thread_handle, &attributes, ThreadFunc, params); 110 int err = pthread_create(&thread_handle->handle_,
111 &attributes,
112 ThreadFunc,
113 &params);
160 success = !err; 114 success = !err;
161 if (!success) { 115 if (!success) {
162 errno = err; 116 errno = err;
163 PLOG(ERROR) << "pthread_create"; 117 PLOG(ERROR) << "pthread_create";
164 } 118 }
165 119
166 pthread_attr_destroy(&attributes); 120 pthread_attr_destroy(&attributes);
167 if (!success) 121
168 delete params; 122 // Don't let this call complete until the thread id
123 // is set in the handle.
124 if (success)
125 params.thread_id_set.Wait();
126
169 return success; 127 return success;
170 } 128 }
171 129
172 } // namespace 130 } // namespace
173 131
174 // static 132 // static
175 PlatformThreadId PlatformThread::CurrentId() { 133 PlatformThreadId PlatformThread::CurrentId() {
176 // Pthreads doesn't have the concept of a thread ID, so we have to reach down 134 // Pthreads doesn't have the concept of a thread ID, so we have to reach down
177 // into the kernel. 135 // into the kernel.
178 #if defined(OS_MACOSX) 136 #if defined(OS_MACOSX)
179 return pthread_mach_thread_np(pthread_self()); 137 return pthread_mach_thread_np(pthread_self());
180 #elif defined(OS_LINUX) 138 #elif defined(OS_LINUX)
181 return syscall(__NR_gettid); 139 return syscall(__NR_gettid);
182 #elif defined(OS_ANDROID) 140 #elif defined(OS_ANDROID)
183 return gettid(); 141 return gettid();
184 #elif defined(OS_SOLARIS) 142 #elif defined(OS_SOLARIS)
185 return pthread_self(); 143 return pthread_self();
186 #elif defined(OS_NACL) && defined(__GLIBC__) 144 #elif defined(OS_NACL) && defined(__GLIBC__)
187 return pthread_self(); 145 return pthread_self();
188 #elif defined(OS_NACL) && !defined(__GLIBC__) 146 #elif defined(OS_NACL) && !defined(__GLIBC__)
189 // Pointers are 32-bits in NaCl. 147 // Pointers are 32-bits in NaCl.
190 return reinterpret_cast<int32>(pthread_self()); 148 return reinterpret_cast<int32>(pthread_self());
191 #elif defined(OS_POSIX) 149 #elif defined(OS_POSIX)
192 return reinterpret_cast<int64>(pthread_self()); 150 return reinterpret_cast<int64>(pthread_self());
193 #endif 151 #endif
194 } 152 }
195 153
154 //static
155 PlatformThreadHandle PlatformThread::CurrentHandle() {
156 return PlatformThreadHandle(pthread_self(), CurrentId());
157 }
158
196 // static 159 // static
197 void PlatformThread::YieldCurrentThread() { 160 void PlatformThread::YieldCurrentThread() {
198 sched_yield(); 161 sched_yield();
199 } 162 }
200 163
201 // static 164 // static
202 void PlatformThread::Sleep(TimeDelta duration) { 165 void PlatformThread::Sleep(TimeDelta duration) {
203 struct timespec sleep_time, remaining; 166 struct timespec sleep_time, remaining;
204 167
205 // Break the duration into seconds and nanoseconds. 168 // Break the duration into seconds and nanoseconds.
206 // NOTE: TimeDelta's microseconds are int64s while timespec's 169 // NOTE: TimeDelta's microseconds are int64s while timespec's
207 // nanoseconds are longs, so this unpacking must prevent overflow. 170 // nanoseconds are longs, so this unpacking must prevent overflow.
208 sleep_time.tv_sec = duration.InSeconds(); 171 sleep_time.tv_sec = duration.InSeconds();
209 duration -= TimeDelta::FromSeconds(sleep_time.tv_sec); 172 duration -= TimeDelta::FromSeconds(sleep_time.tv_sec);
210 sleep_time.tv_nsec = duration.InMicroseconds() * 1000; // nanoseconds 173 sleep_time.tv_nsec = duration.InMicroseconds() * 1000; // nanoseconds
211 174
212 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) 175 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR)
213 sleep_time = remaining; 176 sleep_time = remaining;
214 } 177 }
215 178
216 #if defined(OS_LINUX)
217 // static
218 void PlatformThread::SetName(const char* name) {
219 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
220 tracked_objects::ThreadData::InitializeThreadContext(name);
221
222 // On linux we can get the thread names to show up in the debugger by setting
223 // the process name for the LWP. We don't want to do this for the main
224 // thread because that would rename the process, causing tools like killall
225 // to stop working.
226 if (PlatformThread::CurrentId() == getpid())
227 return;
228
229 // http://0pointer.de/blog/projects/name-your-threads.html
230 // Set the name for the LWP (which gets truncated to 15 characters).
231 // Note that glibc also has a 'pthread_setname_np' api, but it may not be
232 // available everywhere and it's only benefit over using prctl directly is
233 // that it can set the name of threads other than the current thread.
234 int err = prctl(PR_SET_NAME, name);
235 // We expect EPERM failures in sandboxed processes, just ignore those.
236 if (err < 0 && errno != EPERM)
237 DPLOG(ERROR) << "prctl(PR_SET_NAME)";
238 }
239 #elif defined(OS_MACOSX)
240 // Mac is implemented in platform_thread_mac.mm.
241 #else
242 // static
243 void PlatformThread::SetName(const char* name) {
244 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
245 tracked_objects::ThreadData::InitializeThreadContext(name);
246
247 // (This should be relatively simple to implement for the BSDs; I
248 // just don't have one handy to test the code on.)
249 }
250 #endif // defined(OS_LINUX)
251
252 // static 179 // static
253 const char* PlatformThread::GetName() { 180 const char* PlatformThread::GetName() {
254 return ThreadIdNameManager::GetInstance()->GetName(CurrentId()); 181 return ThreadIdNameManager::GetInstance()->GetName(CurrentId());
255 } 182 }
256 183
257 // static 184 // static
258 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, 185 bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
259 PlatformThreadHandle* thread_handle) { 186 PlatformThreadHandle* thread_handle) {
187 base::ThreadRestrictions::ScopedAllowWait allow_wait;
260 return CreateThread(stack_size, true /* joinable thread */, 188 return CreateThread(stack_size, true /* joinable thread */,
261 delegate, thread_handle, kThreadPriority_Normal); 189 delegate, thread_handle, kThreadPriority_Normal);
262 } 190 }
263 191
264 // static 192 // static
265 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, 193 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate,
266 PlatformThreadHandle* thread_handle, 194 PlatformThreadHandle* thread_handle,
267 ThreadPriority priority) { 195 ThreadPriority priority) {
196 base::ThreadRestrictions::ScopedAllowWait allow_wait;
268 return CreateThread(stack_size, true, // joinable thread 197 return CreateThread(stack_size, true, // joinable thread
269 delegate, thread_handle, priority); 198 delegate, thread_handle, priority);
270 } 199 }
271 200
272 // static 201 // static
273 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { 202 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) {
274 PlatformThreadHandle unused; 203 PlatformThreadHandle unused;
275 204
205 base::ThreadRestrictions::ScopedAllowWait allow_wait;
276 bool result = CreateThread(stack_size, false /* non-joinable thread */, 206 bool result = CreateThread(stack_size, false /* non-joinable thread */,
277 delegate, &unused, kThreadPriority_Normal); 207 delegate, &unused, kThreadPriority_Normal);
278 return result; 208 return result;
279 } 209 }
280 210
281 // static 211 // static
282 void PlatformThread::Join(PlatformThreadHandle thread_handle) { 212 void PlatformThread::Join(PlatformThreadHandle thread_handle) {
283 // Joining another thread may block the current thread for a long time, since 213 // Joining another thread may block the current thread for a long time, since
284 // the thread referred to by |thread_handle| may still be running long-lived / 214 // the thread referred to by |thread_handle| may still be running long-lived /
285 // blocking tasks. 215 // blocking tasks.
286 base::ThreadRestrictions::AssertIOAllowed(); 216 base::ThreadRestrictions::AssertIOAllowed();
287 pthread_join(thread_handle, NULL); 217 pthread_join(thread_handle.handle_, NULL);
288 } 218 }
289 219
290 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
291 // Mac OS X uses lower-level mach APIs and Android uses Java APIs.
292 // static
293 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) {
294 // TODO(crogers): Implement, see http://crbug.com/116172
295 }
296 #endif
297
298 #if defined(OS_ANDROID)
299 bool RegisterThreadUtils(JNIEnv* env) {
300 return RegisterNativesImpl(env);
301 }
302 #endif // defined(OS_ANDROID)
303
304 } // namespace base 220 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698