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 <sched.h> | 8 #include <sched.h> |
9 | 9 |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/safe_strerror_posix.h" | 13 #include "base/safe_strerror_posix.h" |
14 #include "base/threading/thread_local.h" | 14 #include "base/threading/thread_id_name_manager.h" |
15 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
16 #include "base/tracked_objects.h" | 16 #include "base/tracked_objects.h" |
17 | 17 |
18 #if defined(OS_MACOSX) | 18 #if defined(OS_MACOSX) |
19 #include <sys/resource.h> | 19 #include <sys/resource.h> |
20 #include <algorithm> | 20 #include <algorithm> |
21 #endif | 21 #endif |
22 | 22 |
23 #if defined(OS_LINUX) | 23 #if defined(OS_LINUX) |
24 #include <sys/prctl.h> | 24 #include <sys/prctl.h> |
(...skipping 13 matching lines...) Expand all Loading... |
38 #endif | 38 #endif |
39 | 39 |
40 namespace base { | 40 namespace base { |
41 | 41 |
42 #if defined(OS_MACOSX) | 42 #if defined(OS_MACOSX) |
43 void InitThreading(); | 43 void InitThreading(); |
44 #endif | 44 #endif |
45 | 45 |
46 namespace { | 46 namespace { |
47 | 47 |
48 #if !defined(OS_MACOSX) | |
49 // Mac name code is in in platform_thread_mac.mm. | |
50 LazyInstance<ThreadLocalPointer<char> >::Leaky | |
51 current_thread_name = LAZY_INSTANCE_INITIALIZER; | |
52 #endif | |
53 | |
54 struct ThreadParams { | 48 struct ThreadParams { |
55 PlatformThread::Delegate* delegate; | 49 PlatformThread::Delegate* delegate; |
56 bool joinable; | 50 bool joinable; |
57 }; | 51 }; |
58 | 52 |
59 void* ThreadFunc(void* params) { | 53 void* ThreadFunc(void* params) { |
60 ThreadParams* thread_params = static_cast<ThreadParams*>(params); | 54 ThreadParams* thread_params = static_cast<ThreadParams*>(params); |
61 PlatformThread::Delegate* delegate = thread_params->delegate; | 55 PlatformThread::Delegate* delegate = thread_params->delegate; |
62 if (!thread_params->joinable) | 56 if (!thread_params->joinable) |
63 base::ThreadRestrictions::SetSingletonAllowed(false); | 57 base::ThreadRestrictions::SetSingletonAllowed(false); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 duration -= TimeDelta::FromSeconds(sleep_time.tv_sec); | 182 duration -= TimeDelta::FromSeconds(sleep_time.tv_sec); |
189 sleep_time.tv_nsec = duration.InMicroseconds() * 1000; // nanoseconds | 183 sleep_time.tv_nsec = duration.InMicroseconds() * 1000; // nanoseconds |
190 | 184 |
191 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) | 185 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) |
192 sleep_time = remaining; | 186 sleep_time = remaining; |
193 } | 187 } |
194 | 188 |
195 #if defined(OS_LINUX) | 189 #if defined(OS_LINUX) |
196 // static | 190 // static |
197 void PlatformThread::SetName(const char* name) { | 191 void PlatformThread::SetName(const char* name) { |
198 // have to cast away const because ThreadLocalPointer does not support const | 192 ThreadIdNameManager::GetInstance()->SetNameForId(CurrentId(), name); |
199 // void* | |
200 current_thread_name.Pointer()->Set(const_cast<char*>(name)); | |
201 tracked_objects::ThreadData::InitializeThreadContext(name); | 193 tracked_objects::ThreadData::InitializeThreadContext(name); |
202 | 194 |
203 // On linux we can get the thread names to show up in the debugger by setting | 195 // On linux we can get the thread names to show up in the debugger by setting |
204 // the process name for the LWP. We don't want to do this for the main | 196 // the process name for the LWP. We don't want to do this for the main |
205 // thread because that would rename the process, causing tools like killall | 197 // thread because that would rename the process, causing tools like killall |
206 // to stop working. | 198 // to stop working. |
207 if (PlatformThread::CurrentId() == getpid()) | 199 if (PlatformThread::CurrentId() == getpid()) |
208 return; | 200 return; |
209 | 201 |
210 // http://0pointer.de/blog/projects/name-your-threads.html | 202 // http://0pointer.de/blog/projects/name-your-threads.html |
211 // Set the name for the LWP (which gets truncated to 15 characters). | 203 // Set the name for the LWP (which gets truncated to 15 characters). |
212 // Note that glibc also has a 'pthread_setname_np' api, but it may not be | 204 // Note that glibc also has a 'pthread_setname_np' api, but it may not be |
213 // available everywhere and it's only benefit over using prctl directly is | 205 // available everywhere and it's only benefit over using prctl directly is |
214 // that it can set the name of threads other than the current thread. | 206 // that it can set the name of threads other than the current thread. |
215 int err = prctl(PR_SET_NAME, name); | 207 int err = prctl(PR_SET_NAME, name); |
216 // We expect EPERM failures in sandboxed processes, just ignore those. | 208 // We expect EPERM failures in sandboxed processes, just ignore those. |
217 if (err < 0 && errno != EPERM) | 209 if (err < 0 && errno != EPERM) |
218 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; | 210 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; |
219 } | 211 } |
220 #elif defined(OS_MACOSX) | 212 #elif defined(OS_MACOSX) |
221 // Mac is implemented in platform_thread_mac.mm. | 213 // Mac is implemented in platform_thread_mac.mm. |
222 #else | 214 #else |
223 // static | 215 // static |
224 void PlatformThread::SetName(const char* name) { | 216 void PlatformThread::SetName(const char* name) { |
225 // have to cast away const because ThreadLocalPointer does not support const | 217 ThreadIdNameManager::GetInstance()->SetNameForId(CurrentId(), name); |
226 // void* | |
227 current_thread_name.Pointer()->Set(const_cast<char*>(name)); | |
228 tracked_objects::ThreadData::InitializeThreadContext(name); | 218 tracked_objects::ThreadData::InitializeThreadContext(name); |
229 | 219 |
230 // (This should be relatively simple to implement for the BSDs; I | 220 // (This should be relatively simple to implement for the BSDs; I |
231 // just don't have one handy to test the code on.) | 221 // just don't have one handy to test the code on.) |
232 } | 222 } |
233 #endif // defined(OS_LINUX) | 223 #endif // defined(OS_LINUX) |
234 | 224 |
235 | |
236 #if !defined(OS_MACOSX) | |
237 // Mac is implemented in platform_thread_mac.mm. | |
238 // static | 225 // static |
239 const char* PlatformThread::GetName() { | 226 const char* PlatformThread::GetName() { |
240 return current_thread_name.Pointer()->Get(); | 227 return ThreadIdNameManager::GetInstance()->GetNameForId(CurrentId()); |
241 } | 228 } |
242 #endif | 229 |
| 230 // static |
| 231 uint32 PlatformThread::GetVersionForName() { |
| 232 return ThreadIdNameManager::GetInstance()->GetVersionForId(CurrentId()); |
| 233 } |
243 | 234 |
244 // static | 235 // static |
245 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 236 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
246 PlatformThreadHandle* thread_handle) { | 237 PlatformThreadHandle* thread_handle) { |
247 return CreateThread(stack_size, true /* joinable thread */, | 238 return CreateThread(stack_size, true /* joinable thread */, |
248 delegate, thread_handle, kThreadPriority_Normal); | 239 delegate, thread_handle, kThreadPriority_Normal); |
249 } | 240 } |
250 | 241 |
251 // static | 242 // static |
252 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, | 243 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, |
(...skipping 24 matching lines...) Expand all Loading... |
277 #if !defined(OS_MACOSX) | 268 #if !defined(OS_MACOSX) |
278 // Mac OS X uses lower-level mach APIs. | 269 // Mac OS X uses lower-level mach APIs. |
279 | 270 |
280 // static | 271 // static |
281 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { | 272 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { |
282 // TODO(crogers): Implement, see http://crbug.com/116172 | 273 // TODO(crogers): Implement, see http://crbug.com/116172 |
283 } | 274 } |
284 #endif | 275 #endif |
285 | 276 |
286 } // namespace base | 277 } // namespace base |
OLD | NEW |