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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 }; | 51 }; |
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 } | |
65 | 63 |
66 // Stash the id in the handle so the calling thread has a complete | 64 // Stash the id in the handle so the calling thread has a complete |
67 // handle, and unblock the parent thread. | 65 // handle, and unblock the parent thread. |
68 *(thread_params->handle) = PlatformThreadHandle(pthread_self(), | 66 *(thread_params->handle) = PlatformThreadHandle(pthread_self()); |
69 PlatformThread::CurrentId()); | |
70 thread_params->handle_set.Signal(); | 67 thread_params->handle_set.Signal(); |
71 | 68 |
72 ThreadIdNameManager::GetInstance()->RegisterThread( | 69 ThreadIdNameManager::GetInstance()->RegisterThread( |
73 PlatformThread::CurrentHandle().platform_handle(), | 70 PlatformThread::CurrentHandle().platform_handle(), |
74 PlatformThread::CurrentId()); | 71 PlatformThread::CurrentId()); |
75 | 72 |
76 delegate->ThreadMain(); | 73 delegate->ThreadMain(); |
77 | 74 |
78 ThreadIdNameManager::GetInstance()->RemoveName( | 75 ThreadIdNameManager::GetInstance()->RemoveName( |
79 PlatformThread::CurrentHandle().platform_handle(), | 76 PlatformThread::CurrentHandle().platform_handle(), |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 #endif | 157 #endif |
161 } | 158 } |
162 | 159 |
163 // static | 160 // static |
164 PlatformThreadRef PlatformThread::CurrentRef() { | 161 PlatformThreadRef PlatformThread::CurrentRef() { |
165 return PlatformThreadRef(pthread_self()); | 162 return PlatformThreadRef(pthread_self()); |
166 } | 163 } |
167 | 164 |
168 // static | 165 // static |
169 PlatformThreadHandle PlatformThread::CurrentHandle() { | 166 PlatformThreadHandle PlatformThread::CurrentHandle() { |
170 return PlatformThreadHandle(pthread_self(), CurrentId()); | 167 return PlatformThreadHandle(pthread_self()); |
171 } | 168 } |
172 | 169 |
173 // static | 170 // static |
174 void PlatformThread::YieldCurrentThread() { | 171 void PlatformThread::YieldCurrentThread() { |
175 sched_yield(); | 172 sched_yield(); |
176 } | 173 } |
177 | 174 |
178 // static | 175 // static |
179 void PlatformThread::Sleep(TimeDelta duration) { | 176 void PlatformThread::Sleep(TimeDelta duration) { |
180 struct timespec sleep_time, remaining; | 177 struct timespec sleep_time, remaining; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 | 221 |
225 // static | 222 // static |
226 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 223 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
227 // Joining another thread may block the current thread for a long time, since | 224 // 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 / | 225 // the thread referred to by |thread_handle| may still be running long-lived / |
229 // blocking tasks. | 226 // blocking tasks. |
230 base::ThreadRestrictions::AssertIOAllowed(); | 227 base::ThreadRestrictions::AssertIOAllowed(); |
231 CHECK_EQ(0, pthread_join(thread_handle.platform_handle(), NULL)); | 228 CHECK_EQ(0, pthread_join(thread_handle.platform_handle(), NULL)); |
232 } | 229 } |
233 | 230 |
234 // Mac has its own Set/GetThreadPriority() implementations. | 231 // Mac has its own Set/GetCurrentThreadPriority() implementations. |
235 #if !defined(OS_MACOSX) | 232 #if !defined(OS_MACOSX) |
236 | 233 |
237 // static | 234 // static |
238 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | 235 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { |
239 ThreadPriority priority) { | |
240 #if defined(OS_NACL) | 236 #if defined(OS_NACL) |
241 NOTIMPLEMENTED(); | 237 NOTIMPLEMENTED(); |
242 #else | 238 #else |
243 if (internal::SetThreadPriorityForPlatform(handle, priority)) | 239 if (internal::SetCurrentThreadPriorityForPlatform(priority)) |
244 return; | 240 return; |
245 | 241 |
246 // setpriority(2) should change the whole thread group's (i.e. process) | 242 // setpriority(2) should change the whole thread group's (i.e. process) |
247 // priority. However, as stated in the bugs section of | 243 // priority. However, as stated in the bugs section of |
248 // http://man7.org/linux/man-pages/man2/getpriority.2.html: "under the current | 244 // 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 | 245 // 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 | 246 // attribute". Also, 0 is prefered to the current thread id since it is |
251 // equivalent but makes sandboxing easier (https://crbug.com/399473). | 247 // equivalent but makes sandboxing easier (https://crbug.com/399473). |
252 DCHECK_NE(handle.id(), kInvalidThreadId); | |
253 const int nice_setting = internal::ThreadPriorityToNiceValue(priority); | 248 const int nice_setting = internal::ThreadPriorityToNiceValue(priority); |
254 const PlatformThreadId current_id = PlatformThread::CurrentId(); | 249 if (setpriority(PRIO_PROCESS, 0, nice_setting)) { |
255 if (setpriority(PRIO_PROCESS, handle.id() == current_id ? 0 : handle.id(), | 250 DVPLOG(1) << "Failed to set nice value of thread (" |
256 nice_setting)) { | 251 << PlatformThread::CurrentId() << ") to " << nice_setting; |
257 DVPLOG(1) << "Failed to set nice value of thread (" << handle.id() | |
258 << ") to " << nice_setting; | |
259 } | 252 } |
260 #endif // defined(OS_NACL) | 253 #endif // defined(OS_NACL) |
261 } | 254 } |
262 | 255 |
263 // static | 256 // static |
264 ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { | 257 ThreadPriority PlatformThread::GetCurrentThreadPriority() { |
265 #if defined(OS_NACL) | 258 #if defined(OS_NACL) |
266 NOTIMPLEMENTED(); | 259 NOTIMPLEMENTED(); |
267 return ThreadPriority::NORMAL; | 260 return ThreadPriority::NORMAL; |
268 #else | 261 #else |
269 // Mirrors SetThreadPriority()'s implementation. | 262 // Mirrors SetCurrentThreadPriority()'s implementation. |
270 ThreadPriority platform_specific_priority; | 263 ThreadPriority platform_specific_priority; |
271 if (internal::GetThreadPriorityForPlatform(handle, | 264 if (internal::GetCurrentThreadPriorityForPlatform( |
272 &platform_specific_priority)) { | 265 &platform_specific_priority)) { |
273 return platform_specific_priority; | 266 return platform_specific_priority; |
274 } | 267 } |
275 | 268 |
276 DCHECK_NE(handle.id(), kInvalidThreadId); | |
277 const PlatformThreadId current_id = PlatformThread::CurrentId(); | |
278 // Need to clear errno before calling getpriority(): | 269 // Need to clear errno before calling getpriority(): |
279 // http://man7.org/linux/man-pages/man2/getpriority.2.html | 270 // http://man7.org/linux/man-pages/man2/getpriority.2.html |
280 errno = 0; | 271 errno = 0; |
281 int nice_value = | 272 int nice_value = getpriority(PRIO_PROCESS, 0); |
282 getpriority(PRIO_PROCESS, handle.id() == current_id ? 0 : handle.id()); | |
283 if (errno != 0) { | 273 if (errno != 0) { |
284 DVPLOG(1) << "Failed to get nice value of thread (" << handle.id() << ")"; | 274 DVPLOG(1) << "Failed to get nice value of thread (" |
| 275 << PlatformThread::CurrentId() << ")"; |
285 return ThreadPriority::NORMAL; | 276 return ThreadPriority::NORMAL; |
286 } | 277 } |
287 | 278 |
288 return internal::NiceValueToThreadPriority(nice_value); | 279 return internal::NiceValueToThreadPriority(nice_value); |
289 #endif // !defined(OS_NACL) | 280 #endif // !defined(OS_NACL) |
290 } | 281 } |
291 | 282 |
292 #endif // !defined(OS_MACOSX) | 283 #endif // !defined(OS_MACOSX) |
293 | 284 |
294 } // namespace base | 285 } // namespace base |
OLD | NEW |