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); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 sleep_time = remaining; | 164 sleep_time = remaining; |
162 } | 165 } |
163 | 166 |
164 // Linux SetName is currently disabled, as we need to distinguish between | 167 // 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 | 168 // 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 | 169 // (where making this call renames our process, causing tools like killall |
167 // to stop working). | 170 // to stop working). |
168 #if 0 && defined(OS_LINUX) | 171 #if 0 && defined(OS_LINUX) |
169 // static | 172 // static |
170 void PlatformThread::SetName(const char* name) { | 173 void PlatformThread::SetName(const char* name) { |
| 174 // have to cast away const because ThreadLocalPointer does not support const |
| 175 // void* |
| 176 current_thread_name.Set(const_cast<char*>(name)); |
| 177 |
171 // http://0pointer.de/blog/projects/name-your-threads.html | 178 // http://0pointer.de/blog/projects/name-your-threads.html |
172 | 179 |
173 // glibc recently added support for pthread_setname_np, but it's not | 180 // glibc recently added support for pthread_setname_np, but it's not |
174 // commonly available yet. So test for it at runtime. | 181 // commonly available yet. So test for it at runtime. |
175 int (*dynamic_pthread_setname_np)(pthread_t, const char*); | 182 int (*dynamic_pthread_setname_np)(pthread_t, const char*); |
176 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = | 183 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
177 dlsym(RTLD_DEFAULT, "pthread_setname_np"); | 184 dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
178 | 185 |
179 if (dynamic_pthread_setname_np) { | 186 if (dynamic_pthread_setname_np) { |
180 // This limit comes from glibc, which gets it from the kernel | 187 // 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).) | 198 // truncated by the callee (see TASK_COMM_LEN above).) |
192 int err = prctl(PR_SET_NAME, name); | 199 int err = prctl(PR_SET_NAME, name); |
193 if (err < 0) | 200 if (err < 0) |
194 PLOG(ERROR) << "prctl(PR_SET_NAME)"; | 201 PLOG(ERROR) << "prctl(PR_SET_NAME)"; |
195 } | 202 } |
196 } | 203 } |
197 #elif defined(OS_MACOSX) | 204 #elif defined(OS_MACOSX) |
198 // Mac is implemented in platform_thread_mac.mm. | 205 // Mac is implemented in platform_thread_mac.mm. |
199 #else | 206 #else |
200 // static | 207 // static |
201 void PlatformThread::SetName(const char* /*name*/) { | 208 void PlatformThread::SetName(const char* name) { |
202 // Leave it unimplemented. | 209 // have to cast away const because ThreadLocalPointer does not support const |
| 210 // void* |
| 211 current_thread_name.Set(const_cast<char*>(name)); |
203 | 212 |
204 // (This should be relatively simple to implement for the BSDs; I | 213 // (This should be relatively simple to implement for the BSDs; I |
205 // just don't have one handy to test the code on.) | 214 // just don't have one handy to test the code on.) |
206 } | 215 } |
207 #endif // defined(OS_LINUX) | 216 #endif // defined(OS_LINUX) |
208 | 217 |
| 218 |
| 219 #if !defined(OS_MACOSX) |
| 220 // Mac is implemented in platform_thread_mac.mm. |
| 221 // static |
| 222 const char* PlatformThread::GetName() { |
| 223 return current_thread_name.Get(); |
| 224 } |
| 225 #endif |
| 226 |
209 // static | 227 // static |
210 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 228 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
211 PlatformThreadHandle* thread_handle) { | 229 PlatformThreadHandle* thread_handle) { |
212 return CreateThread(stack_size, true /* joinable thread */, | 230 return CreateThread(stack_size, true /* joinable thread */, |
213 delegate, thread_handle); | 231 delegate, thread_handle); |
214 } | 232 } |
215 | 233 |
216 // static | 234 // static |
217 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 235 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
218 PlatformThreadHandle unused; | 236 PlatformThreadHandle unused; |
(...skipping 16 matching lines...) Expand all Loading... |
235 // Mac OS X uses lower-level mach APIs | 253 // Mac OS X uses lower-level mach APIs |
236 | 254 |
237 // static | 255 // static |
238 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { | 256 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { |
239 // TODO(crogers): implement | 257 // TODO(crogers): implement |
240 NOTIMPLEMENTED(); | 258 NOTIMPLEMENTED(); |
241 } | 259 } |
242 #endif | 260 #endif |
243 | 261 |
244 } // namespace base | 262 } // namespace base |
OLD | NEW |