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

Side by Side Diff: base/platform_thread_posix.cc

Issue 2774001: posix: set thread names (Closed)
Patch Set: disable linux Created 10 years, 6 months 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
« no previous file with comments | « base/platform_thread_mac.mm ('k') | 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/platform_thread.h" 5 #include "base/platform_thread.h"
6 6
7 #include <dlfcn.h>
7 #include <errno.h> 8 #include <errno.h>
8 #include <sched.h> 9 #include <sched.h>
9 10
10 #if defined(OS_MACOSX) 11 #if defined(OS_MACOSX)
11 #include <mach/mach.h> 12 #include <mach/mach.h>
12 #include <sys/resource.h> 13 #include <sys/resource.h>
13 #include <algorithm> 14 #include <algorithm>
14 #else 15 #endif
16
17 #if defined(OS_LINUX)
18 #include <sys/prctl.h>
15 #include <sys/syscall.h> 19 #include <sys/syscall.h>
16 #include <unistd.h> 20 #include <unistd.h>
17 #endif 21 #endif
18 22
23 #include "base/logging.h"
24 #include "base/safe_strerror_posix.h"
25
19 #if defined(OS_MACOSX) 26 #if defined(OS_MACOSX)
20 namespace base { 27 namespace base {
21 void InitThreading(); 28 void InitThreading();
22 } // namespace 29 } // namespace base
23 #endif 30 #endif
24 31
25 static void* ThreadFunc(void* closure) { 32 static void* ThreadFunc(void* closure) {
26 PlatformThread::Delegate* delegate = 33 PlatformThread::Delegate* delegate =
27 static_cast<PlatformThread::Delegate*>(closure); 34 static_cast<PlatformThread::Delegate*>(closure);
28 delegate->ThreadMain(); 35 delegate->ThreadMain();
29 return NULL; 36 return NULL;
30 } 37 }
31 38
32 // static 39 // static
(...skipping 23 matching lines...) Expand all
56 sleep_time.tv_sec = duration_ms / 1000; 63 sleep_time.tv_sec = duration_ms / 1000;
57 duration_ms -= sleep_time.tv_sec * 1000; 64 duration_ms -= sleep_time.tv_sec * 1000;
58 65
59 // Contains the portion of duration_ms < 1 sec. 66 // Contains the portion of duration_ms < 1 sec.
60 sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds. 67 sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds.
61 68
62 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) 69 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR)
63 sleep_time = remaining; 70 sleep_time = remaining;
64 } 71 }
65 72
73 // Linux SetName is currently disabled, as we need to distinguish between
74 // helper threads (where it's ok to make this call) and the main thread
75 // (where making this call renames our process, causing tools like killall
76 // to stop working).
77 #if 0 && defined(OS_LINUX)
66 // static 78 // static
67 void PlatformThread::SetName(const char* name) { 79 void PlatformThread::SetName(const char* name) {
68 // The POSIX standard does not provide for naming threads, and neither Linux 80 // http://0pointer.de/blog/projects/name-your-threads.html
69 // nor Mac OS X (our two POSIX targets) provide any non-portable way of doing 81
70 // it either. (Some BSDs provide pthread_set_name_np but that isn't much of a 82 // glibc recently added support for pthread_setname_np, but it's not
71 // consolation prize.) 83 // commonly available yet. So test for it at runtime.
72 // TODO(darin): decide whether stuffing the name in TLS or other in-memory 84 int (*dynamic_pthread_setname_np)(pthread_t, const char*);
73 // structure would be useful for debugging or not. 85 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) =
86 dlsym(RTLD_DEFAULT, "pthread_setname_np");
87
88 if (dynamic_pthread_setname_np) {
89 // This limit comes from glibc, which gets it from the kernel
90 // (TASK_COMM_LEN).
91 const int kMaxNameLength = 15;
92 std::string shortened_name = std::string(name).substr(0, kMaxNameLength);
93 int err = dynamic_pthread_setname_np(pthread_self(),
94 shortened_name.c_str());
95 if (err < 0)
96 LOG(ERROR) << "pthread_setname_np: " << safe_strerror(err);
97 } else {
98 // Implementing this function without glibc is simple enough. (We
99 // don't do the name length clipping as above because it will be
100 // truncated by the callee (see TASK_COMM_LEN above).)
101 int err = prctl(PR_SET_NAME, name);
102 if (err < 0)
103 PLOG(ERROR) << "prctl(PR_SET_NAME)";
104 }
74 } 105 }
106 #elif defined(OS_MACOSX)
107 // Mac is implemented in platform_thread_mac.mm.
108 #else
109 // static
110 void PlatformThread::SetName(const char* name) {
111 // Leave it unimplemented.
112
113 // (This should be relatively simple to implement for the BSDs; I
114 // just don't have one handy to test the code on.)
115 }
116 #endif // defined(OS_LINUX)
75 117
76 namespace { 118 namespace {
77 119
78 bool CreateThread(size_t stack_size, bool joinable, 120 bool CreateThread(size_t stack_size, bool joinable,
79 PlatformThread::Delegate* delegate, 121 PlatformThread::Delegate* delegate,
80 PlatformThreadHandle* thread_handle) { 122 PlatformThreadHandle* thread_handle) {
81 #if defined(OS_MACOSX) 123 #if defined(OS_MACOSX)
82 base::InitThreading(); 124 base::InitThreading();
83 #endif // OS_MACOSX 125 #endif // OS_MACOSX
84 126
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 189
148 bool result = CreateThread(stack_size, false /* non-joinable thread */, 190 bool result = CreateThread(stack_size, false /* non-joinable thread */,
149 delegate, &unused); 191 delegate, &unused);
150 return result; 192 return result;
151 } 193 }
152 194
153 // static 195 // static
154 void PlatformThread::Join(PlatformThreadHandle thread_handle) { 196 void PlatformThread::Join(PlatformThreadHandle thread_handle) {
155 pthread_join(thread_handle, NULL); 197 pthread_join(thread_handle, NULL);
156 } 198 }
OLDNEW
« no previous file with comments | « base/platform_thread_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698