Chromium Code Reviews| 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> |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 | 52 |
| 53 void* ThreadFunc(void* params) { | 53 void* ThreadFunc(void* params) { |
| 54 base::InitOnThread(); | 54 base::InitOnThread(); |
| 55 ThreadParams* thread_params = static_cast<ThreadParams*>(params); | 55 ThreadParams* thread_params = static_cast<ThreadParams*>(params); |
| 56 | 56 |
| 57 PlatformThread::Delegate* delegate = thread_params->delegate; | 57 PlatformThread::Delegate* delegate = thread_params->delegate; |
| 58 if (!thread_params->joinable) | 58 if (!thread_params->joinable) |
| 59 base::ThreadRestrictions::SetSingletonAllowed(false); | 59 base::ThreadRestrictions::SetSingletonAllowed(false); |
| 60 | 60 |
| 61 if (thread_params->priority != ThreadPriority::NORMAL) { | 61 if (thread_params->priority != ThreadPriority::NORMAL) { |
| 62 PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(), | 62 PlatformThread::SetCurrentThreadPriority(thread_params->priority); |
| 63 thread_params->priority); | |
| 64 } | 63 } |
| 65 | 64 |
| 66 // Stash the id in the handle so the calling thread has a complete | 65 // Stash the id in the handle so the calling thread has a complete |
| 67 // handle, and unblock the parent thread. | 66 // handle, and unblock the parent thread. |
| 68 *(thread_params->handle) = PlatformThreadHandle(pthread_self(), | 67 *(thread_params->handle) = PlatformThreadHandle(pthread_self(), |
| 69 PlatformThread::CurrentId()); | 68 PlatformThread::CurrentId()); |
| 70 thread_params->handle_set.Signal(); | 69 thread_params->handle_set.Signal(); |
| 71 | 70 |
| 72 ThreadIdNameManager::GetInstance()->RegisterThread( | 71 ThreadIdNameManager::GetInstance()->RegisterThread( |
| 73 PlatformThread::CurrentHandle().platform_handle(), | 72 PlatformThread::CurrentHandle().platform_handle(), |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 | 223 |
| 225 // static | 224 // static |
| 226 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 225 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
| 227 // Joining another thread may block the current thread for a long time, since | 226 // Joining another thread may block the current thread for a long time, since |
| 228 // the thread referred to by |thread_handle| may still be running long-lived / | 227 // the thread referred to by |thread_handle| may still be running long-lived / |
| 229 // blocking tasks. | 228 // blocking tasks. |
| 230 base::ThreadRestrictions::AssertIOAllowed(); | 229 base::ThreadRestrictions::AssertIOAllowed(); |
| 231 CHECK_EQ(0, pthread_join(thread_handle.platform_handle(), NULL)); | 230 CHECK_EQ(0, pthread_join(thread_handle.platform_handle(), NULL)); |
| 232 } | 231 } |
| 233 | 232 |
| 234 // Mac has its own Set/GetThreadPriority() implementations. | 233 // Mac has its own Set/GetCurrentThreadPriority() implementations. |
| 235 #if !defined(OS_MACOSX) | 234 #if !defined(OS_MACOSX) |
| 236 | 235 |
| 237 // static | 236 // static |
| 238 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | 237 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { |
| 239 ThreadPriority priority) { | |
| 240 #if defined(OS_NACL) | 238 #if defined(OS_NACL) |
| 241 NOTIMPLEMENTED(); | 239 NOTIMPLEMENTED(); |
| 242 #else | 240 #else |
| 243 if (internal::SetThreadPriorityForPlatform(handle, priority)) | 241 if (internal::SetCurrentThreadPriorityForPlatform(priority)) |
| 244 return; | 242 return; |
| 245 | 243 |
| 246 // setpriority(2) should change the whole thread group's (i.e. process) | 244 // setpriority(2) should change the whole thread group's (i.e. process) |
| 247 // priority. However, as stated in the bugs section of | 245 // priority. However, as stated in the bugs section of |
| 248 // http://man7.org/linux/man-pages/man2/getpriority.2.html: "under the current | 246 // http://man7.org/linux/man-pages/man2/getpriority.2.html: "under the current |
| 249 // Linux/NPTL implementation of POSIX threads, the nice value is a per-thread | 247 // Linux/NPTL implementation of POSIX threads, the nice value is a per-thread |
| 250 // attribute". Also, 0 is prefered to the current thread id since it is | 248 // attribute". Also, 0 is prefered to the current thread id since it is |
| 251 // equivalent but makes sandboxing easier (https://crbug.com/399473). | 249 // equivalent but makes sandboxing easier (https://crbug.com/399473). |
| 252 DCHECK_NE(handle.id(), kInvalidThreadId); | |
| 253 const int nice_setting = internal::ThreadPriorityToNiceValue(priority); | 250 const int nice_setting = internal::ThreadPriorityToNiceValue(priority); |
| 254 const PlatformThreadId current_id = PlatformThread::CurrentId(); | 251 if (setpriority(PRIO_PROCESS, 0, nice_setting)) { |
| 255 if (setpriority(PRIO_PROCESS, handle.id() == current_id ? 0 : handle.id(), | 252 DVPLOG(1) << "Failed to set nice value of thread (" |
| 256 nice_setting)) { | 253 << PlatformThread::CurrentHandle().id() |
|
gab
2015/06/23 17:01:32
If I'm not mistaken this is the only use of Platfo
Takashi Toyoshima
2015/06/24 04:15:39
Thread::thread_id() still calls it, so it isn't as
| |
| 257 DVPLOG(1) << "Failed to set nice value of thread (" << handle.id() | |
| 258 << ") to " << nice_setting; | 254 << ") to " << nice_setting; |
| 259 } | 255 } |
| 260 #endif // defined(OS_NACL) | 256 #endif // defined(OS_NACL) |
| 261 } | 257 } |
| 262 | 258 |
| 263 // static | 259 // static |
| 264 ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { | 260 ThreadPriority PlatformThread::GetCurrentThreadPriority() { |
| 265 #if defined(OS_NACL) | 261 #if defined(OS_NACL) |
| 266 NOTIMPLEMENTED(); | 262 NOTIMPLEMENTED(); |
| 267 return ThreadPriority::NORMAL; | 263 return ThreadPriority::NORMAL; |
| 268 #else | 264 #else |
| 269 // Mirrors SetThreadPriority()'s implementation. | 265 // Mirrors SetCurrentThreadPriority()'s implementation. |
| 270 ThreadPriority platform_specific_priority; | 266 ThreadPriority platform_specific_priority; |
| 271 if (internal::GetThreadPriorityForPlatform(handle, | 267 if (internal::GetCurrentThreadPriorityForPlatform( |
| 272 &platform_specific_priority)) { | 268 &platform_specific_priority)) { |
| 273 return platform_specific_priority; | 269 return platform_specific_priority; |
| 274 } | 270 } |
| 275 | 271 |
| 276 DCHECK_NE(handle.id(), kInvalidThreadId); | |
| 277 const PlatformThreadId current_id = PlatformThread::CurrentId(); | |
| 278 // Need to clear errno before calling getpriority(): | 272 // Need to clear errno before calling getpriority(): |
| 279 // http://man7.org/linux/man-pages/man2/getpriority.2.html | 273 // http://man7.org/linux/man-pages/man2/getpriority.2.html |
| 280 errno = 0; | 274 errno = 0; |
| 281 int nice_value = | 275 int nice_value = getpriority(PRIO_PROCESS, 0); |
| 282 getpriority(PRIO_PROCESS, handle.id() == current_id ? 0 : handle.id()); | |
| 283 if (errno != 0) { | 276 if (errno != 0) { |
| 284 DVPLOG(1) << "Failed to get nice value of thread (" << handle.id() << ")"; | 277 DVPLOG(1) << "Failed to get nice value of thread (" |
| 278 << PlatformThread::CurrentHandle().id() | |
| 279 << ")"; | |
| 285 return ThreadPriority::NORMAL; | 280 return ThreadPriority::NORMAL; |
| 286 } | 281 } |
| 287 | 282 |
| 288 return internal::NiceValueToThreadPriority(nice_value); | 283 return internal::NiceValueToThreadPriority(nice_value); |
| 289 #endif // !defined(OS_NACL) | 284 #endif // !defined(OS_NACL) |
| 290 } | 285 } |
| 291 | 286 |
| 292 #endif // !defined(OS_MACOSX) | 287 #endif // !defined(OS_MACOSX) |
| 293 | 288 |
| 294 } // namespace base | 289 } // namespace base |
| OLD | NEW |