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