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 <pthread.h> | |
8 #include <sched.h> | 9 #include <sched.h> |
10 #include <sys/resource.h> | |
11 #include <sys/time.h> | |
9 | 12 |
10 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 14 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
13 #include "base/safe_strerror_posix.h" | 16 #include "base/safe_strerror_posix.h" |
14 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
18 #include "base/threading/platform_thread_internal_posix.h" | |
15 #include "base/threading/thread_id_name_manager.h" | 19 #include "base/threading/thread_id_name_manager.h" |
16 #include "base/threading/thread_restrictions.h" | 20 #include "base/threading/thread_restrictions.h" |
17 #include "base/tracked_objects.h" | 21 #include "base/tracked_objects.h" |
18 | 22 |
19 #if defined(OS_MACOSX) | |
20 #include <sys/resource.h> | |
21 #include <algorithm> | |
22 #endif | |
23 | |
24 #if defined(OS_LINUX) | 23 #if defined(OS_LINUX) |
25 #include <sys/prctl.h> | |
26 #include <sys/resource.h> | |
27 #include <sys/syscall.h> | 24 #include <sys/syscall.h> |
28 #include <sys/time.h> | 25 #elif defined(OS_ANDROID) |
29 #include <unistd.h> | 26 #include <sys/types.h> |
30 #endif | 27 #endif |
31 | 28 |
32 namespace base { | 29 namespace base { |
33 | 30 |
34 void InitThreading(); | 31 void InitThreading(); |
35 void InitOnThread(); | 32 void InitOnThread(); |
36 void TerminateOnThread(); | 33 void TerminateOnThread(); |
37 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes); | 34 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes); |
38 | 35 |
39 namespace { | 36 namespace { |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 | 225 |
229 // static | 226 // static |
230 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 227 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
231 // Joining another thread may block the current thread for a long time, since | 228 // Joining another thread may block the current thread for a long time, since |
232 // the thread referred to by |thread_handle| may still be running long-lived / | 229 // the thread referred to by |thread_handle| may still be running long-lived / |
233 // blocking tasks. | 230 // blocking tasks. |
234 base::ThreadRestrictions::AssertIOAllowed(); | 231 base::ThreadRestrictions::AssertIOAllowed(); |
235 CHECK_EQ(0, pthread_join(thread_handle.handle_, NULL)); | 232 CHECK_EQ(0, pthread_join(thread_handle.handle_, NULL)); |
236 } | 233 } |
237 | 234 |
235 // Mac has its own SetThreadPriority() implementation. | |
236 #if !defined(OS_MACOSX) | |
237 // static | |
238 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | |
239 ThreadPriority priority) { | |
240 #if !defined(OS_NACL) | |
241 if (internal::HandleSetThreadPriorityForPlatform(handle, priority)) | |
242 return; | |
243 | |
244 // setpriority(2) should change the whole thread group's (i.e. process) | |
245 // priority. However, under the current Linux/NPTL implementation of POSIX | |
rvargas (doing something else)
2015/03/17 22:15:21
tiny nit: Remove the Linux part?
gab
2015/03/18 19:01:06
This was copy-pasted verbatim from http://linux.di
rvargas (doing something else)
2015/03/18 22:54:23
Yes, and that's what the comment said on the _linu
gab
2015/03/19 15:32:50
Fair enough, tweaked comment.
| |
246 // threads, the nice value is a per-thread attribute. See the bugs section in | |
247 // http://man7.org/linux/man-pages/man2/getpriority.2.html. We prefer using 0 | |
248 // rather than the current thread id since they are equivalent but it makes | |
rvargas (doing something else)
2015/03/17 22:15:22
is using 0 also equivalent on bsd? just checking
gab
2015/03/18 19:01:06
I asssume so, the only reason the current implemen
rvargas (doing something else)
2015/03/18 22:54:23
Acknowledged.
| |
249 // sandboxing easier (https://crbug.com/399473). | |
250 DCHECK_NE(handle.id_, kInvalidThreadId); | |
251 const int nice_setting = internal::ThreadPriorityToNiceValue(priority); | |
rvargas (doing something else)
2015/03/17 22:15:21
nit: either remove const (preferred) or name the v
gab
2015/03/18 19:01:06
If by "name the variables as constant" you mean kN
rvargas (doing something else)
2015/03/18 22:54:23
ok, up to you.
That's kind of a pet peeve of mine
| |
252 const PlatformThreadId current_id = PlatformThread::CurrentId(); | |
253 if (setpriority(PRIO_PROCESS, handle.id_ == current_id ? 0 : handle.id_, | |
254 nice_setting)) { | |
255 DVPLOG(1) << "Failed to set nice value of thread (" << handle.id_ << ") to " | |
256 << nice_setting; | |
257 } | |
258 #endif // !defined(OS_NACL) | |
259 } | |
260 #endif // !defined(OS_MACOSX) | |
261 | |
238 } // namespace base | 262 } // namespace base |
OLD | NEW |