OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/platform_thread.h" | 5 #include "base/platform_thread.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sched.h> | 8 #include <sched.h> |
9 | 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/safe_strerror_posix.h" |
| 12 #include "base/scoped_ptr.h" |
| 13 #include "base/thread_restrictions.h" |
| 14 |
10 #if defined(OS_MACOSX) | 15 #if defined(OS_MACOSX) |
11 #include <mach/mach.h> | 16 #include <mach/mach.h> |
12 #include <sys/resource.h> | 17 #include <sys/resource.h> |
13 #include <algorithm> | 18 #include <algorithm> |
14 #endif | 19 #endif |
15 | 20 |
16 #if defined(OS_LINUX) | 21 #if defined(OS_LINUX) |
17 #include <dlfcn.h> | 22 #include <dlfcn.h> |
18 #include <sys/prctl.h> | 23 #include <sys/prctl.h> |
19 #include <sys/syscall.h> | 24 #include <sys/syscall.h> |
20 #include <unistd.h> | 25 #include <unistd.h> |
21 #endif | 26 #endif |
22 | 27 |
23 #if defined(OS_NACL) | 28 #if defined(OS_NACL) |
24 #include <sys/nacl_syscalls.h> | 29 #include <sys/nacl_syscalls.h> |
25 #endif | 30 #endif |
26 | 31 |
27 #include "base/logging.h" | |
28 #include "base/safe_strerror_posix.h" | |
29 | |
30 #if defined(OS_MACOSX) | 32 #if defined(OS_MACOSX) |
31 namespace base { | 33 namespace base { |
32 void InitThreading(); | 34 void InitThreading(); |
33 } // namespace base | 35 } // namespace base |
34 #endif | 36 #endif |
35 | 37 |
36 static void* ThreadFunc(void* closure) { | 38 namespace { |
37 PlatformThread::Delegate* delegate = | 39 |
38 static_cast<PlatformThread::Delegate*>(closure); | 40 struct ThreadParams { |
| 41 PlatformThread::Delegate* delegate; |
| 42 bool joinable; |
| 43 }; |
| 44 |
| 45 } // namespace |
| 46 |
| 47 static void* ThreadFunc(void* params) { |
| 48 ThreadParams* thread_params = static_cast<ThreadParams*>(params); |
| 49 PlatformThread::Delegate* delegate = thread_params->delegate; |
| 50 if (!thread_params->joinable) |
| 51 base::ThreadRestrictions::SetSingletonAllowed(false); |
| 52 delete thread_params; |
39 delegate->ThreadMain(); | 53 delegate->ThreadMain(); |
40 return NULL; | 54 return NULL; |
41 } | 55 } |
42 | 56 |
43 // static | 57 // static |
44 PlatformThreadId PlatformThread::CurrentId() { | 58 PlatformThreadId PlatformThread::CurrentId() { |
45 // Pthreads doesn't have the concept of a thread ID, so we have to reach down | 59 // Pthreads doesn't have the concept of a thread ID, so we have to reach down |
46 // into the kernel. | 60 // into the kernel. |
47 #if defined(OS_MACOSX) | 61 #if defined(OS_MACOSX) |
48 return mach_thread_self(); | 62 return mach_thread_self(); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 stack_size = std::max(std::max(default_stack_size, | 181 stack_size = std::max(std::max(default_stack_size, |
168 static_cast<size_t>(PTHREAD_STACK_MIN)), | 182 static_cast<size_t>(PTHREAD_STACK_MIN)), |
169 static_cast<size_t>(stack_rlimit.rlim_cur)); | 183 static_cast<size_t>(stack_rlimit.rlim_cur)); |
170 } | 184 } |
171 } | 185 } |
172 #endif // OS_MACOSX | 186 #endif // OS_MACOSX |
173 | 187 |
174 if (stack_size > 0) | 188 if (stack_size > 0) |
175 pthread_attr_setstacksize(&attributes, stack_size); | 189 pthread_attr_setstacksize(&attributes, stack_size); |
176 | 190 |
177 success = !pthread_create(thread_handle, &attributes, ThreadFunc, delegate); | 191 ThreadParams* params = new ThreadParams; |
| 192 params->delegate = delegate; |
| 193 params->joinable = joinable; |
| 194 success = !pthread_create(thread_handle, &attributes, ThreadFunc, params); |
178 | 195 |
179 pthread_attr_destroy(&attributes); | 196 pthread_attr_destroy(&attributes); |
| 197 if (!success) |
| 198 delete params; |
180 return success; | 199 return success; |
181 } | 200 } |
182 | 201 |
183 } // anonymous namespace | 202 } // anonymous namespace |
184 | 203 |
185 // static | 204 // static |
186 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 205 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
187 PlatformThreadHandle* thread_handle) { | 206 PlatformThreadHandle* thread_handle) { |
188 return CreateThread(stack_size, true /* joinable thread */, | 207 return CreateThread(stack_size, true /* joinable thread */, |
189 delegate, thread_handle); | 208 delegate, thread_handle); |
190 } | 209 } |
191 | 210 |
192 // static | 211 // static |
193 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 212 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
194 PlatformThreadHandle unused; | 213 PlatformThreadHandle unused; |
195 | 214 |
196 bool result = CreateThread(stack_size, false /* non-joinable thread */, | 215 bool result = CreateThread(stack_size, false /* non-joinable thread */, |
197 delegate, &unused); | 216 delegate, &unused); |
198 return result; | 217 return result; |
199 } | 218 } |
200 | 219 |
201 // static | 220 // static |
202 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 221 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
203 pthread_join(thread_handle, NULL); | 222 pthread_join(thread_handle, NULL); |
204 } | 223 } |
OLD | NEW |