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 14 matching lines...) Expand all Loading... |
39 #endif | 39 #endif |
40 | 40 |
41 namespace base { | 41 namespace base { |
42 | 42 |
43 #if defined(OS_MACOSX) | 43 #if defined(OS_MACOSX) |
44 void InitThreading(); | 44 void InitThreading(); |
45 #endif | 45 #endif |
46 | 46 |
47 namespace { | 47 namespace { |
48 | 48 |
49 #if !defined(OS_MACOSX) | |
50 // Mac name code is in in platform_thread_mac.mm. | |
51 LazyInstance<ThreadLocalPointer<char> >::Leaky | |
52 current_thread_name = LAZY_INSTANCE_INITIALIZER; | |
53 #endif | |
54 | |
55 struct ThreadParams { | 49 struct ThreadParams { |
56 PlatformThread::Delegate* delegate; | 50 PlatformThread::Delegate* delegate; |
57 bool joinable; | 51 bool joinable; |
58 }; | 52 }; |
59 | 53 |
60 void* ThreadFunc(void* params) { | 54 void* ThreadFunc(void* params) { |
61 #if defined(OS_ANDROID) | 55 #if defined(OS_ANDROID) |
62 // Threads on linux/android may inherit their priority from the thread | 56 // Threads on linux/android may inherit their priority from the thread |
63 // where they were created. This sets all threads to the default. | 57 // where they were created. This sets all threads to the default. |
64 // TODO(epenner): Move thread priorities to base. (crbug.com/170549) | 58 // TODO(epenner): Move thread priorities to base. (crbug.com/170549) |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 duration -= TimeDelta::FromSeconds(sleep_time.tv_sec); | 190 duration -= TimeDelta::FromSeconds(sleep_time.tv_sec); |
197 sleep_time.tv_nsec = duration.InMicroseconds() * 1000; // nanoseconds | 191 sleep_time.tv_nsec = duration.InMicroseconds() * 1000; // nanoseconds |
198 | 192 |
199 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) | 193 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) |
200 sleep_time = remaining; | 194 sleep_time = remaining; |
201 } | 195 } |
202 | 196 |
203 #if defined(OS_LINUX) | 197 #if defined(OS_LINUX) |
204 // static | 198 // static |
205 void PlatformThread::SetName(const char* name) { | 199 void PlatformThread::SetName(const char* name) { |
206 // have to cast away const because ThreadLocalPointer does not support const | 200 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); |
207 // void* | |
208 current_thread_name.Pointer()->Set(const_cast<char*>(name)); | |
209 tracked_objects::ThreadData::InitializeThreadContext(name); | 201 tracked_objects::ThreadData::InitializeThreadContext(name); |
210 | 202 |
211 // On linux we can get the thread names to show up in the debugger by setting | 203 // On linux we can get the thread names to show up in the debugger by setting |
212 // the process name for the LWP. We don't want to do this for the main | 204 // the process name for the LWP. We don't want to do this for the main |
213 // thread because that would rename the process, causing tools like killall | 205 // thread because that would rename the process, causing tools like killall |
214 // to stop working. | 206 // to stop working. |
215 if (PlatformThread::CurrentId() == getpid()) | 207 if (PlatformThread::CurrentId() == getpid()) |
216 return; | 208 return; |
217 | 209 |
218 // http://0pointer.de/blog/projects/name-your-threads.html | 210 // http://0pointer.de/blog/projects/name-your-threads.html |
219 // Set the name for the LWP (which gets truncated to 15 characters). | 211 // Set the name for the LWP (which gets truncated to 15 characters). |
220 // Note that glibc also has a 'pthread_setname_np' api, but it may not be | 212 // Note that glibc also has a 'pthread_setname_np' api, but it may not be |
221 // available everywhere and it's only benefit over using prctl directly is | 213 // available everywhere and it's only benefit over using prctl directly is |
222 // that it can set the name of threads other than the current thread. | 214 // that it can set the name of threads other than the current thread. |
223 int err = prctl(PR_SET_NAME, name); | 215 int err = prctl(PR_SET_NAME, name); |
224 // We expect EPERM failures in sandboxed processes, just ignore those. | 216 // We expect EPERM failures in sandboxed processes, just ignore those. |
225 if (err < 0 && errno != EPERM) | 217 if (err < 0 && errno != EPERM) |
226 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; | 218 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; |
227 } | 219 } |
228 #elif defined(OS_MACOSX) | 220 #elif defined(OS_MACOSX) |
229 // Mac is implemented in platform_thread_mac.mm. | 221 // Mac is implemented in platform_thread_mac.mm. |
230 #else | 222 #else |
231 // static | 223 // static |
232 void PlatformThread::SetName(const char* name) { | 224 void PlatformThread::SetName(const char* name) { |
233 // have to cast away const because ThreadLocalPointer does not support const | 225 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); |
234 // void* | |
235 current_thread_name.Pointer()->Set(const_cast<char*>(name)); | |
236 tracked_objects::ThreadData::InitializeThreadContext(name); | 226 tracked_objects::ThreadData::InitializeThreadContext(name); |
237 | 227 |
238 // (This should be relatively simple to implement for the BSDs; I | 228 // (This should be relatively simple to implement for the BSDs; I |
239 // just don't have one handy to test the code on.) | 229 // just don't have one handy to test the code on.) |
240 } | 230 } |
241 #endif // defined(OS_LINUX) | 231 #endif // defined(OS_LINUX) |
242 | 232 |
243 | |
244 #if !defined(OS_MACOSX) | |
245 // Mac is implemented in platform_thread_mac.mm. | |
246 // static | 233 // static |
247 const char* PlatformThread::GetName() { | 234 const char* PlatformThread::GetName() { |
248 return current_thread_name.Pointer()->Get(); | 235 return ThreadIdNameManager::GetInstance()->GetName(CurrentId()); |
249 } | 236 } |
250 #endif | |
251 | 237 |
252 // static | 238 // static |
253 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 239 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
254 PlatformThreadHandle* thread_handle) { | 240 PlatformThreadHandle* thread_handle) { |
255 return CreateThread(stack_size, true /* joinable thread */, | 241 return CreateThread(stack_size, true /* joinable thread */, |
256 delegate, thread_handle, kThreadPriority_Normal); | 242 delegate, thread_handle, kThreadPriority_Normal); |
257 } | 243 } |
258 | 244 |
259 // static | 245 // static |
260 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, | 246 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, |
(...skipping 24 matching lines...) Expand all Loading... |
285 #if !defined(OS_MACOSX) | 271 #if !defined(OS_MACOSX) |
286 // Mac OS X uses lower-level mach APIs. | 272 // Mac OS X uses lower-level mach APIs. |
287 | 273 |
288 // static | 274 // static |
289 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { | 275 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { |
290 // TODO(crogers): Implement, see http://crbug.com/116172 | 276 // TODO(crogers): Implement, see http://crbug.com/116172 |
291 } | 277 } |
292 #endif | 278 #endif |
293 | 279 |
294 } // namespace base | 280 } // namespace base |
OLD | NEW |