OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <sched.h> | 8 #include <sched.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/safe_strerror_posix.h" | 12 #include "base/safe_strerror_posix.h" |
13 #include "base/threading/thread_local.h" | |
13 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
14 | 15 |
15 #if defined(OS_MACOSX) | 16 #if defined(OS_MACOSX) |
16 #include <mach/mach.h> | 17 #include <mach/mach.h> |
17 #include <sys/resource.h> | 18 #include <sys/resource.h> |
18 #include <algorithm> | 19 #include <algorithm> |
19 #endif | 20 #endif |
20 | 21 |
21 #if defined(OS_LINUX) | 22 #if defined(OS_LINUX) |
22 #include <dlfcn.h> | 23 #include <dlfcn.h> |
(...skipping 11 matching lines...) Expand all Loading... | |
34 #endif | 35 #endif |
35 | 36 |
36 namespace base { | 37 namespace base { |
37 | 38 |
38 #if defined(OS_MACOSX) | 39 #if defined(OS_MACOSX) |
39 void InitThreading(); | 40 void InitThreading(); |
40 #endif | 41 #endif |
41 | 42 |
42 namespace { | 43 namespace { |
43 | 44 |
45 static ThreadLocalPointer<char> current_thread_name; | |
46 | |
44 struct ThreadParams { | 47 struct ThreadParams { |
45 PlatformThread::Delegate* delegate; | 48 PlatformThread::Delegate* delegate; |
46 bool joinable; | 49 bool joinable; |
47 }; | 50 }; |
48 | 51 |
49 void* ThreadFunc(void* params) { | 52 void* ThreadFunc(void* params) { |
50 ThreadParams* thread_params = static_cast<ThreadParams*>(params); | 53 ThreadParams* thread_params = static_cast<ThreadParams*>(params); |
51 PlatformThread::Delegate* delegate = thread_params->delegate; | 54 PlatformThread::Delegate* delegate = thread_params->delegate; |
52 if (!thread_params->joinable) | 55 if (!thread_params->joinable) |
53 base::ThreadRestrictions::SetSingletonAllowed(false); | 56 base::ThreadRestrictions::SetSingletonAllowed(false); |
54 delete thread_params; | 57 delete thread_params; |
55 delegate->ThreadMain(); | 58 delegate->ThreadMain(); |
56 #if defined(OS_ANDROID) | 59 #if defined(OS_ANDROID) |
57 base::android::DetachFromVM(); | 60 base::android::DetachFromVM(); |
58 #endif | 61 #endif |
59 return NULL; | 62 return NULL; |
60 } | 63 } |
61 | 64 |
65 | |
Sigurður Ásgeirsson
2011/08/04 12:31:39
nit: spurious space?
nduca
2011/08/04 18:17:38
Done.
| |
66 | |
62 bool CreateThread(size_t stack_size, bool joinable, | 67 bool CreateThread(size_t stack_size, bool joinable, |
63 PlatformThread::Delegate* delegate, | 68 PlatformThread::Delegate* delegate, |
64 PlatformThreadHandle* thread_handle) { | 69 PlatformThreadHandle* thread_handle) { |
65 #if defined(OS_MACOSX) | 70 #if defined(OS_MACOSX) |
66 base::InitThreading(); | 71 base::InitThreading(); |
67 #endif // OS_MACOSX | 72 #endif // OS_MACOSX |
68 | 73 |
69 bool success = false; | 74 bool success = false; |
70 pthread_attr_t attributes; | 75 pthread_attr_t attributes; |
71 pthread_attr_init(&attributes); | 76 pthread_attr_init(&attributes); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 sleep_time = remaining; | 166 sleep_time = remaining; |
162 } | 167 } |
163 | 168 |
164 // Linux SetName is currently disabled, as we need to distinguish between | 169 // Linux SetName is currently disabled, as we need to distinguish between |
165 // helper threads (where it's ok to make this call) and the main thread | 170 // helper threads (where it's ok to make this call) and the main thread |
166 // (where making this call renames our process, causing tools like killall | 171 // (where making this call renames our process, causing tools like killall |
167 // to stop working). | 172 // to stop working). |
168 #if 0 && defined(OS_LINUX) | 173 #if 0 && defined(OS_LINUX) |
169 // static | 174 // static |
170 void PlatformThread::SetName(const char* name) { | 175 void PlatformThread::SetName(const char* name) { |
176 // have to cast away const because ThreadLocalPointer does not support const | |
177 // void* | |
178 current_thread_name.Set(const_cast<char*>(name)); | |
179 | |
171 // http://0pointer.de/blog/projects/name-your-threads.html | 180 // http://0pointer.de/blog/projects/name-your-threads.html |
172 | 181 |
173 // glibc recently added support for pthread_setname_np, but it's not | 182 // glibc recently added support for pthread_setname_np, but it's not |
174 // commonly available yet. So test for it at runtime. | 183 // commonly available yet. So test for it at runtime. |
175 int (*dynamic_pthread_setname_np)(pthread_t, const char*); | 184 int (*dynamic_pthread_setname_np)(pthread_t, const char*); |
176 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = | 185 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
177 dlsym(RTLD_DEFAULT, "pthread_setname_np"); | 186 dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
178 | 187 |
179 if (dynamic_pthread_setname_np) { | 188 if (dynamic_pthread_setname_np) { |
180 // This limit comes from glibc, which gets it from the kernel | 189 // This limit comes from glibc, which gets it from the kernel |
(...skipping 10 matching lines...) Expand all Loading... | |
191 // truncated by the callee (see TASK_COMM_LEN above).) | 200 // truncated by the callee (see TASK_COMM_LEN above).) |
192 int err = prctl(PR_SET_NAME, name); | 201 int err = prctl(PR_SET_NAME, name); |
193 if (err < 0) | 202 if (err < 0) |
194 PLOG(ERROR) << "prctl(PR_SET_NAME)"; | 203 PLOG(ERROR) << "prctl(PR_SET_NAME)"; |
195 } | 204 } |
196 } | 205 } |
197 #elif defined(OS_MACOSX) | 206 #elif defined(OS_MACOSX) |
198 // Mac is implemented in platform_thread_mac.mm. | 207 // Mac is implemented in platform_thread_mac.mm. |
199 #else | 208 #else |
200 // static | 209 // static |
201 void PlatformThread::SetName(const char* /*name*/) { | 210 void PlatformThread::SetName(const char* name) { |
202 // Leave it unimplemented. | 211 // have to cast away const because ThreadLocalPointer does not support const |
212 // void* | |
213 current_thread_name.Set(const_cast<char*>(name)); | |
203 | 214 |
204 // (This should be relatively simple to implement for the BSDs; I | 215 // (This should be relatively simple to implement for the BSDs; I |
205 // just don't have one handy to test the code on.) | 216 // just don't have one handy to test the code on.) |
206 } | 217 } |
207 #endif // defined(OS_LINUX) | 218 #endif // defined(OS_LINUX) |
208 | 219 |
209 // static | 220 // static |
221 const char* PlatformThread::GetName() { | |
222 return current_thread_name.Get(); | |
223 } | |
224 | |
225 // static | |
210 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 226 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
211 PlatformThreadHandle* thread_handle) { | 227 PlatformThreadHandle* thread_handle) { |
212 return CreateThread(stack_size, true /* joinable thread */, | 228 return CreateThread(stack_size, true /* joinable thread */, |
213 delegate, thread_handle); | 229 delegate, thread_handle); |
214 } | 230 } |
215 | 231 |
216 // static | 232 // static |
217 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 233 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
218 PlatformThreadHandle unused; | 234 PlatformThreadHandle unused; |
219 | 235 |
(...skipping 15 matching lines...) Expand all Loading... | |
235 // Mac OS X uses lower-level mach APIs | 251 // Mac OS X uses lower-level mach APIs |
236 | 252 |
237 // static | 253 // static |
238 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { | 254 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { |
239 // TODO(crogers): implement | 255 // TODO(crogers): implement |
240 NOTIMPLEMENTED(); | 256 NOTIMPLEMENTED(); |
241 } | 257 } |
242 #endif | 258 #endif |
243 | 259 |
244 } // namespace base | 260 } // namespace base |
OLD | NEW |