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 <pthread.h> |
9 #include <sched.h> | 9 #include <sched.h> |
10 #include <sys/resource.h> | 10 #include <sys/resource.h> |
11 #include <sys/time.h> | 11 #include <sys/time.h> |
12 | 12 |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/safe_strerror_posix.h" | 16 #include "base/safe_strerror_posix.h" |
| 17 #include "base/scoped_clear_errno.h" |
17 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
18 #include "base/threading/platform_thread_internal_posix.h" | 19 #include "base/threading/platform_thread_internal_posix.h" |
19 #include "base/threading/thread_id_name_manager.h" | 20 #include "base/threading/thread_id_name_manager.h" |
20 #include "base/threading/thread_restrictions.h" | 21 #include "base/threading/thread_restrictions.h" |
21 #include "base/tracked_objects.h" | 22 #include "base/tracked_objects.h" |
22 | 23 |
23 #if defined(OS_LINUX) | 24 #if defined(OS_LINUX) |
24 #include <sys/syscall.h> | 25 #include <sys/syscall.h> |
25 #elif defined(OS_ANDROID) | 26 #elif defined(OS_ANDROID) |
26 #include <sys/types.h> | 27 #include <sys/types.h> |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 226 |
226 // static | 227 // static |
227 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 228 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
228 // Joining another thread may block the current thread for a long time, since | 229 // Joining another thread may block the current thread for a long time, since |
229 // the thread referred to by |thread_handle| may still be running long-lived / | 230 // the thread referred to by |thread_handle| may still be running long-lived / |
230 // blocking tasks. | 231 // blocking tasks. |
231 base::ThreadRestrictions::AssertIOAllowed(); | 232 base::ThreadRestrictions::AssertIOAllowed(); |
232 CHECK_EQ(0, pthread_join(thread_handle.handle_, NULL)); | 233 CHECK_EQ(0, pthread_join(thread_handle.handle_, NULL)); |
233 } | 234 } |
234 | 235 |
235 // Mac has its own SetThreadPriority() implementation. | 236 // Mac has its own Set/GetThreadPriority() implementations. |
236 #if !defined(OS_MACOSX) | 237 #if !defined(OS_MACOSX) |
| 238 |
237 // static | 239 // static |
238 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | 240 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, |
239 ThreadPriority priority) { | 241 ThreadPriority priority) { |
240 #if !defined(OS_NACL) | 242 #if defined(OS_NACL) |
241 if (internal::HandleSetThreadPriorityForPlatform(handle, priority)) | 243 NOTIMPLEMENTED(); |
| 244 #else |
| 245 if (internal::SetThreadPriorityForPlatform(handle, priority)) |
242 return; | 246 return; |
243 | 247 |
244 // setpriority(2) should change the whole thread group's (i.e. process) | 248 // setpriority(2) should change the whole thread group's (i.e. process) |
245 // priority. However, as stated in the bugs section of | 249 // priority. However, as stated in the bugs section of |
246 // http://man7.org/linux/man-pages/man2/getpriority.2.html: "under the current | 250 // http://man7.org/linux/man-pages/man2/getpriority.2.html: "under the current |
247 // Linux/NPTL implementation of POSIX threads, the nice value is a per-thread | 251 // Linux/NPTL implementation of POSIX threads, the nice value is a per-thread |
248 // attribute". Also, 0 is prefered to the current thread id since it is | 252 // attribute". Also, 0 is prefered to the current thread id since it is |
249 // equivalent but makes sandboxing easier (https://crbug.com/399473). | 253 // equivalent but makes sandboxing easier (https://crbug.com/399473). |
250 DCHECK_NE(handle.id_, kInvalidThreadId); | 254 DCHECK_NE(handle.id_, kInvalidThreadId); |
251 const int nice_setting = internal::ThreadPriorityToNiceValue(priority); | 255 const int nice_setting = internal::ThreadPriorityToNiceValue(priority); |
252 const PlatformThreadId current_id = PlatformThread::CurrentId(); | 256 const PlatformThreadId current_id = PlatformThread::CurrentId(); |
253 if (setpriority(PRIO_PROCESS, handle.id_ == current_id ? 0 : handle.id_, | 257 if (setpriority(PRIO_PROCESS, handle.id_ == current_id ? 0 : handle.id_, |
254 nice_setting)) { | 258 nice_setting)) { |
255 DVPLOG(1) << "Failed to set nice value of thread (" << handle.id_ << ") to " | 259 DVPLOG(1) << "Failed to set nice value of thread (" << handle.id_ << ") to " |
256 << nice_setting; | 260 << nice_setting; |
257 } | 261 } |
| 262 #endif // defined(OS_NACL) |
| 263 } |
| 264 |
| 265 // static |
| 266 ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { |
| 267 #if defined(OS_NACL) |
| 268 NOTIMPLEMENTED(); |
| 269 return kThreadPriority_Normal; |
| 270 #else |
| 271 // Mirrors SetThreadPriority()'s implementation. |
| 272 ThreadPriority platform_specific_priority; |
| 273 if (internal::GetThreadPriorityForPlatform( |
| 274 handle, &platform_specific_priority)) { |
| 275 return platform_specific_priority; |
| 276 } |
| 277 |
| 278 DCHECK_NE(handle.id_, kInvalidThreadId); |
| 279 const PlatformThreadId current_id = PlatformThread::CurrentId(); |
| 280 // Need to clear errno before calling getpriority(): |
| 281 // http://man7.org/linux/man-pages/man2/getpriority.2.html |
| 282 ScopedClearErrno clear_errno; |
| 283 int nice_value = |
| 284 getpriority(PRIO_PROCESS, handle.id_ == current_id ? 0 : handle.id_); |
| 285 if (errno != 0) { |
| 286 DVPLOG(1) << "Failed to get nice value of thread (" << handle.id_ << ")"; |
| 287 return kThreadPriority_Normal; |
| 288 } |
| 289 |
| 290 return internal::NiceValueToThreadPriority(nice_value); |
258 #endif // !defined(OS_NACL) | 291 #endif // !defined(OS_NACL) |
259 } | 292 } |
| 293 |
260 #endif // !defined(OS_MACOSX) | 294 #endif // !defined(OS_MACOSX) |
261 | 295 |
262 } // namespace base | 296 } // namespace base |
OLD | NEW |