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 21 matching lines...) Expand all Loading... |
32 void InitOnThread(); | 32 void InitOnThread(); |
33 void TerminateOnThread(); | 33 void TerminateOnThread(); |
34 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes); | 34 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes); |
35 | 35 |
36 namespace { | 36 namespace { |
37 | 37 |
38 struct ThreadParams { | 38 struct ThreadParams { |
39 ThreadParams() | 39 ThreadParams() |
40 : delegate(NULL), | 40 : delegate(NULL), |
41 joinable(false), | 41 joinable(false), |
42 priority(kThreadPriority_Normal), | 42 priority(ThreadPriority::NORMAL), |
43 handle(NULL), | 43 handle(NULL), |
44 handle_set(false, false) { | 44 handle_set(false, false) { |
45 } | 45 } |
46 | 46 |
47 PlatformThread::Delegate* delegate; | 47 PlatformThread::Delegate* delegate; |
48 bool joinable; | 48 bool joinable; |
49 ThreadPriority priority; | 49 ThreadPriority priority; |
50 PlatformThreadHandle* handle; | 50 PlatformThreadHandle* handle; |
51 WaitableEvent handle_set; | 51 WaitableEvent handle_set; |
52 }; | 52 }; |
53 | 53 |
54 void* ThreadFunc(void* params) { | 54 void* ThreadFunc(void* params) { |
55 base::InitOnThread(); | 55 base::InitOnThread(); |
56 ThreadParams* thread_params = static_cast<ThreadParams*>(params); | 56 ThreadParams* thread_params = static_cast<ThreadParams*>(params); |
57 | 57 |
58 PlatformThread::Delegate* delegate = thread_params->delegate; | 58 PlatformThread::Delegate* delegate = thread_params->delegate; |
59 if (!thread_params->joinable) | 59 if (!thread_params->joinable) |
60 base::ThreadRestrictions::SetSingletonAllowed(false); | 60 base::ThreadRestrictions::SetSingletonAllowed(false); |
61 | 61 |
62 if (thread_params->priority != kThreadPriority_Normal) { | 62 if (thread_params->priority != ThreadPriority::NORMAL) { |
63 PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(), | 63 PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(), |
64 thread_params->priority); | 64 thread_params->priority); |
65 } | 65 } |
66 | 66 |
67 // Stash the id in the handle so the calling thread has a complete | 67 // Stash the id in the handle so the calling thread has a complete |
68 // handle, and unblock the parent thread. | 68 // handle, and unblock the parent thread. |
69 *(thread_params->handle) = PlatformThreadHandle(pthread_self(), | 69 *(thread_params->handle) = PlatformThreadHandle(pthread_self(), |
70 PlatformThread::CurrentId()); | 70 PlatformThread::CurrentId()); |
71 thread_params->handle_set.Signal(); | 71 thread_params->handle_set.Signal(); |
72 | 72 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 // static | 194 // static |
195 const char* PlatformThread::GetName() { | 195 const char* PlatformThread::GetName() { |
196 return ThreadIdNameManager::GetInstance()->GetName(CurrentId()); | 196 return ThreadIdNameManager::GetInstance()->GetName(CurrentId()); |
197 } | 197 } |
198 | 198 |
199 // static | 199 // static |
200 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 200 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
201 PlatformThreadHandle* thread_handle) { | 201 PlatformThreadHandle* thread_handle) { |
202 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 202 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
203 return CreateThread(stack_size, true /* joinable thread */, | 203 return CreateThread(stack_size, true /* joinable thread */, |
204 delegate, thread_handle, kThreadPriority_Normal); | 204 delegate, thread_handle, ThreadPriority::NORMAL); |
205 } | 205 } |
206 | 206 |
207 // static | 207 // static |
208 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, | 208 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, |
209 PlatformThreadHandle* thread_handle, | 209 PlatformThreadHandle* thread_handle, |
210 ThreadPriority priority) { | 210 ThreadPriority priority) { |
211 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 211 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
212 return CreateThread(stack_size, true, // joinable thread | 212 return CreateThread(stack_size, true, // joinable thread |
213 delegate, thread_handle, priority); | 213 delegate, thread_handle, priority); |
214 } | 214 } |
215 | 215 |
216 // static | 216 // static |
217 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 217 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
218 PlatformThreadHandle unused; | 218 PlatformThreadHandle unused; |
219 | 219 |
220 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 220 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
221 bool result = CreateThread(stack_size, false /* non-joinable thread */, | 221 bool result = CreateThread(stack_size, false /* non-joinable thread */, |
222 delegate, &unused, kThreadPriority_Normal); | 222 delegate, &unused, ThreadPriority::NORMAL); |
223 return result; | 223 return result; |
224 } | 224 } |
225 | 225 |
226 // static | 226 // static |
227 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 227 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
228 // 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 |
229 // 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 / |
230 // blocking tasks. | 230 // blocking tasks. |
231 base::ThreadRestrictions::AssertIOAllowed(); | 231 base::ThreadRestrictions::AssertIOAllowed(); |
232 CHECK_EQ(0, pthread_join(thread_handle.handle_, NULL)); | 232 CHECK_EQ(0, pthread_join(thread_handle.handle_, NULL)); |
(...skipping 25 matching lines...) Expand all Loading... |
258 DVPLOG(1) << "Failed to set nice value of thread (" << handle.id_ << ") to " | 258 DVPLOG(1) << "Failed to set nice value of thread (" << handle.id_ << ") to " |
259 << nice_setting; | 259 << nice_setting; |
260 } | 260 } |
261 #endif // defined(OS_NACL) | 261 #endif // defined(OS_NACL) |
262 } | 262 } |
263 | 263 |
264 // static | 264 // static |
265 ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { | 265 ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { |
266 #if defined(OS_NACL) | 266 #if defined(OS_NACL) |
267 NOTIMPLEMENTED(); | 267 NOTIMPLEMENTED(); |
268 return kThreadPriority_Normal; | 268 return ThreadPriority::NORMAL; |
269 #else | 269 #else |
270 // Mirrors SetThreadPriority()'s implementation. | 270 // Mirrors SetThreadPriority()'s implementation. |
271 ThreadPriority platform_specific_priority; | 271 ThreadPriority platform_specific_priority; |
272 if (internal::GetThreadPriorityForPlatform(handle, | 272 if (internal::GetThreadPriorityForPlatform(handle, |
273 &platform_specific_priority)) { | 273 &platform_specific_priority)) { |
274 return platform_specific_priority; | 274 return platform_specific_priority; |
275 } | 275 } |
276 | 276 |
277 DCHECK_NE(handle.id_, kInvalidThreadId); | 277 DCHECK_NE(handle.id_, kInvalidThreadId); |
278 const PlatformThreadId current_id = PlatformThread::CurrentId(); | 278 const PlatformThreadId current_id = PlatformThread::CurrentId(); |
279 // Need to clear errno before calling getpriority(): | 279 // Need to clear errno before calling getpriority(): |
280 // http://man7.org/linux/man-pages/man2/getpriority.2.html | 280 // http://man7.org/linux/man-pages/man2/getpriority.2.html |
281 errno = 0; | 281 errno = 0; |
282 int nice_value = | 282 int nice_value = |
283 getpriority(PRIO_PROCESS, handle.id_ == current_id ? 0 : handle.id_); | 283 getpriority(PRIO_PROCESS, handle.id_ == current_id ? 0 : handle.id_); |
284 if (errno != 0) { | 284 if (errno != 0) { |
285 DVPLOG(1) << "Failed to get nice value of thread (" << handle.id_ << ")"; | 285 DVPLOG(1) << "Failed to get nice value of thread (" << handle.id_ << ")"; |
286 return kThreadPriority_Normal; | 286 return ThreadPriority::NORMAL; |
287 } | 287 } |
288 | 288 |
289 return internal::NiceValueToThreadPriority(nice_value); | 289 return internal::NiceValueToThreadPriority(nice_value); |
290 #endif // !defined(OS_NACL) | 290 #endif // !defined(OS_NACL) |
291 } | 291 } |
292 | 292 |
293 #endif // !defined(OS_MACOSX) | 293 #endif // !defined(OS_MACOSX) |
294 | 294 |
295 } // namespace base | 295 } // namespace base |
OLD | NEW |