| 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_id_name_manager.h" |
| 14 #include "base/threading/thread_local.h" | 15 #include "base/threading/thread_local.h" |
| 15 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
| 16 #include "base/tracked_objects.h" | 17 #include "base/tracked_objects.h" |
| 17 | 18 |
| 18 #if defined(OS_MACOSX) | 19 #if defined(OS_MACOSX) |
| 19 #include <sys/resource.h> | 20 #include <sys/resource.h> |
| 20 #include <algorithm> | 21 #include <algorithm> |
| 21 #endif | 22 #endif |
| 22 | 23 |
| 23 #if defined(OS_LINUX) | 24 #if defined(OS_LINUX) |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 } | 194 } |
| 194 | 195 |
| 195 #if defined(OS_LINUX) | 196 #if defined(OS_LINUX) |
| 196 // static | 197 // static |
| 197 void PlatformThread::SetName(const char* name) { | 198 void PlatformThread::SetName(const char* name) { |
| 198 // have to cast away const because ThreadLocalPointer does not support const | 199 // have to cast away const because ThreadLocalPointer does not support const |
| 199 // void* | 200 // void* |
| 200 current_thread_name.Pointer()->Set(const_cast<char*>(name)); | 201 current_thread_name.Pointer()->Set(const_cast<char*>(name)); |
| 201 tracked_objects::ThreadData::InitializeThreadContext(name); | 202 tracked_objects::ThreadData::InitializeThreadContext(name); |
| 202 | 203 |
| 204 ThreadIdNameManager::GetInstance()->SetNameForId(CurrentId(), name); |
| 205 |
| 203 // On linux we can get the thread names to show up in the debugger by setting | 206 // 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 | 207 // 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 | 208 // thread because that would rename the process, causing tools like killall |
| 206 // to stop working. | 209 // to stop working. |
| 207 if (PlatformThread::CurrentId() == getpid()) | 210 if (PlatformThread::CurrentId() == getpid()) |
| 208 return; | 211 return; |
| 209 | 212 |
| 210 // http://0pointer.de/blog/projects/name-your-threads.html | 213 // http://0pointer.de/blog/projects/name-your-threads.html |
| 211 // Set the name for the LWP (which gets truncated to 15 characters). | 214 // 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 | 215 // 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 | 216 // 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. | 217 // that it can set the name of threads other than the current thread. |
| 215 int err = prctl(PR_SET_NAME, name); | 218 int err = prctl(PR_SET_NAME, name); |
| 216 // We expect EPERM failures in sandboxed processes, just ignore those. | 219 // We expect EPERM failures in sandboxed processes, just ignore those. |
| 217 if (err < 0 && errno != EPERM) | 220 if (err < 0 && errno != EPERM) |
| 218 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; | 221 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; |
| 219 } | 222 } |
| 220 #elif defined(OS_MACOSX) | 223 #elif defined(OS_MACOSX) |
| 221 // Mac is implemented in platform_thread_mac.mm. | 224 // Mac is implemented in platform_thread_mac.mm. |
| 222 #else | 225 #else |
| 223 // static | 226 // static |
| 224 void PlatformThread::SetName(const char* name) { | 227 void PlatformThread::SetName(const char* name) { |
| 225 // have to cast away const because ThreadLocalPointer does not support const | 228 // have to cast away const because ThreadLocalPointer does not support const |
| 226 // void* | 229 // void* |
| 227 current_thread_name.Pointer()->Set(const_cast<char*>(name)); | 230 current_thread_name.Pointer()->Set(const_cast<char*>(name)); |
| 228 tracked_objects::ThreadData::InitializeThreadContext(name); | 231 tracked_objects::ThreadData::InitializeThreadContext(name); |
| 229 | 232 |
| 233 ThreadIdNameManager::GetInstance()->SetNameForId(CurrentId(), name); |
| 234 |
| 230 // (This should be relatively simple to implement for the BSDs; I | 235 // (This should be relatively simple to implement for the BSDs; I |
| 231 // just don't have one handy to test the code on.) | 236 // just don't have one handy to test the code on.) |
| 232 } | 237 } |
| 233 #endif // defined(OS_LINUX) | 238 #endif // defined(OS_LINUX) |
| 234 | 239 |
| 235 | 240 |
| 236 #if !defined(OS_MACOSX) | 241 #if !defined(OS_MACOSX) |
| 237 // Mac is implemented in platform_thread_mac.mm. | 242 // Mac is implemented in platform_thread_mac.mm. |
| 238 // static | 243 // static |
| 239 const char* PlatformThread::GetName() { | 244 const char* PlatformThread::GetName() { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 #if !defined(OS_MACOSX) | 282 #if !defined(OS_MACOSX) |
| 278 // Mac OS X uses lower-level mach APIs. | 283 // Mac OS X uses lower-level mach APIs. |
| 279 | 284 |
| 280 // static | 285 // static |
| 281 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { | 286 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { |
| 282 // TODO(crogers): Implement, see http://crbug.com/116172 | 287 // TODO(crogers): Implement, see http://crbug.com/116172 |
| 283 } | 288 } |
| 284 #endif | 289 #endif |
| 285 | 290 |
| 286 } // namespace base | 291 } // namespace base |
| OLD | NEW |