Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: base/threading/platform_thread_posix.cc

Issue 8936015: Enable setting thread names on Linux (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 sleep_time.tv_sec = duration_ms / 1000; 159 sleep_time.tv_sec = duration_ms / 1000;
160 duration_ms -= sleep_time.tv_sec * 1000; 160 duration_ms -= sleep_time.tv_sec * 1000;
161 161
162 // Contains the portion of duration_ms < 1 sec. 162 // Contains the portion of duration_ms < 1 sec.
163 sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds. 163 sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds.
164 164
165 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) 165 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR)
166 sleep_time = remaining; 166 sleep_time = remaining;
167 } 167 }
168 168
169 // Linux SetName is currently disabled, as we need to distinguish between 169 #if defined(OS_LINUX)
170 // helper threads (where it's ok to make this call) and the main thread
171 // (where making this call renames our process, causing tools like killall
172 // to stop working).
173 #if 0 && defined(OS_LINUX)
174 // static 170 // static
175 void PlatformThread::SetName(const char* name) { 171 void PlatformThread::SetName(const char* name) {
176 // have to cast away const because ThreadLocalPointer does not support const 172 // have to cast away const because ThreadLocalPointer does not support const
177 // void* 173 // void*
178 current_thread_name.Pointer()->Set(const_cast<char*>(name)); 174 current_thread_name.Pointer()->Set(const_cast<char*>(name));
179 tracked_objects::ThreadData::InitializeThreadContext(name); 175 tracked_objects::ThreadData::InitializeThreadContext(name);
180 176
177 // On linux we can get the thread names to show up in the debugger by setting
178 // the process name for the LWP. We don't want to do this for the main
179 // thread because that would rename the process, causing tools like killall
180 // to stop working.
181 if (PlatformThread::CurrentId() == getpid())
182 return;
183
181 // http://0pointer.de/blog/projects/name-your-threads.html 184 // http://0pointer.de/blog/projects/name-your-threads.html
182 185
183 // glibc recently added support for pthread_setname_np, but it's not 186 // glibc recently added support for pthread_setname_np, but it's not
184 // commonly available yet. So test for it at runtime. 187 // commonly available yet. So test for it at runtime.
185 int (*dynamic_pthread_setname_np)(pthread_t, const char*); 188 int (*dynamic_pthread_setname_np)(pthread_t, const char*);
186 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = 189 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) =
187 dlsym(RTLD_DEFAULT, "pthread_setname_np"); 190 dlsym(RTLD_DEFAULT, "pthread_setname_np");
188 191
189 if (dynamic_pthread_setname_np) { 192 if (dynamic_pthread_setname_np) {
Rick Byers 2011/12/14 15:45:09 What's the benefit of using this API at all? Can'
190 // This limit comes from glibc, which gets it from the kernel 193 // This limit comes from glibc, which gets it from the kernel
191 // (TASK_COMM_LEN). 194 // (TASK_COMM_LEN).
192 const int kMaxNameLength = 15; 195 const int kMaxNameLength = 15;
193 std::string shortened_name = std::string(name).substr(0, kMaxNameLength); 196 std::string shortened_name = std::string(name).substr(0, kMaxNameLength);
194 int err = dynamic_pthread_setname_np(pthread_self(), 197 int err = dynamic_pthread_setname_np(pthread_self(),
195 shortened_name.c_str()); 198 shortened_name.c_str());
196 if (err < 0) 199 if (err < 0)
197 DLOG(ERROR) << "pthread_setname_np: " << safe_strerror(err); 200 DLOG(ERROR) << "pthread_setname_np: " << safe_strerror(err);
198 } else { 201 } else {
199 // Implementing this function without glibc is simple enough. (We 202 // Implementing this function without glibc is simple enough. (We
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 // Mac OS X uses lower-level mach APIs 260 // Mac OS X uses lower-level mach APIs
258 261
259 // static 262 // static
260 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { 263 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) {
261 // TODO(crogers): implement 264 // TODO(crogers): implement
262 NOTIMPLEMENTED(); 265 NOTIMPLEMENTED();
263 } 266 }
264 #endif 267 #endif
265 268
266 } // namespace base 269 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698