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

Side by Side Diff: base/threading/platform_thread_mac.mm

Issue 11438022: Add ability to retrieve a thread_name given a thread_id. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Make some of the try servers happier. Created 7 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « base/debug/trace_event_unittest.cc ('k') | base/threading/platform_thread_posix.cc » ('j') | 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) 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 #import <Foundation/Foundation.h> 7 #import <Foundation/Foundation.h>
8 #include <dlfcn.h> 8 #include <dlfcn.h>
9 #include <mach/mach.h> 9 #include <mach/mach.h>
10 #include <mach/mach_time.h> 10 #include <mach/mach_time.h>
11 #include <mach/thread_policy.h> 11 #include <mach/thread_policy.h>
12 12
13 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/threading/thread_local.h" 15 #include "base/threading/thread_id_name_manager.h"
16 #include "base/tracked_objects.h" 16 #include "base/tracked_objects.h"
17 17
18 namespace base { 18 namespace base {
19 19
20 namespace {
21
22 LazyInstance<ThreadLocalPointer<char> >::Leaky
23 current_thread_name = LAZY_INSTANCE_INITIALIZER;
24
25 } // namespace
26
27 // If Cocoa is to be used on more than one thread, it must know that the 20 // If Cocoa is to be used on more than one thread, it must know that the
28 // application is multithreaded. Since it's possible to enter Cocoa code 21 // application is multithreaded. Since it's possible to enter Cocoa code
29 // from threads created by pthread_thread_create, Cocoa won't necessarily 22 // from threads created by pthread_thread_create, Cocoa won't necessarily
30 // be aware that the application is multithreaded. Spawning an NSThread is 23 // be aware that the application is multithreaded. Spawning an NSThread is
31 // enough to get Cocoa to set up for multithreaded operation, so this is done 24 // enough to get Cocoa to set up for multithreaded operation, so this is done
32 // if necessary before pthread_thread_create spawns any threads. 25 // if necessary before pthread_thread_create spawns any threads.
33 // 26 //
34 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea tingThreads/chapter_4_section_4.html 27 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea tingThreads/chapter_4_section_4.html
35 void InitThreading() { 28 void InitThreading() {
36 static BOOL multithreaded = [NSThread isMultiThreaded]; 29 static BOOL multithreaded = [NSThread isMultiThreaded];
37 if (!multithreaded) { 30 if (!multithreaded) {
38 // +[NSObject class] is idempotent. 31 // +[NSObject class] is idempotent.
39 [NSThread detachNewThreadSelector:@selector(class) 32 [NSThread detachNewThreadSelector:@selector(class)
40 toTarget:[NSObject class] 33 toTarget:[NSObject class]
41 withObject:nil]; 34 withObject:nil];
42 multithreaded = YES; 35 multithreaded = YES;
43 36
44 DCHECK([NSThread isMultiThreaded]); 37 DCHECK([NSThread isMultiThreaded]);
45 } 38 }
46 } 39 }
47 40
48 // static 41 // static
49 void PlatformThread::SetName(const char* name) { 42 void PlatformThread::SetName(const char* name) {
50 current_thread_name.Pointer()->Set(const_cast<char*>(name)); 43 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
51 tracked_objects::ThreadData::InitializeThreadContext(name); 44 tracked_objects::ThreadData::InitializeThreadContext(name);
52 45
53 // pthread_setname_np is only available in 10.6 or later, so test 46 // pthread_setname_np is only available in 10.6 or later, so test
54 // for it at runtime. 47 // for it at runtime.
55 int (*dynamic_pthread_setname_np)(const char*); 48 int (*dynamic_pthread_setname_np)(const char*);
56 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = 49 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) =
57 dlsym(RTLD_DEFAULT, "pthread_setname_np"); 50 dlsym(RTLD_DEFAULT, "pthread_setname_np");
58 if (!dynamic_pthread_setname_np) 51 if (!dynamic_pthread_setname_np)
59 return; 52 return;
60 53
61 // Mac OS X does not expose the length limit of the name, so 54 // Mac OS X does not expose the length limit of the name, so
62 // hardcode it. 55 // hardcode it.
63 const int kMaxNameLength = 63; 56 const int kMaxNameLength = 63;
64 std::string shortened_name = std::string(name).substr(0, kMaxNameLength); 57 std::string shortened_name = std::string(name).substr(0, kMaxNameLength);
65 // pthread_setname() fails (harmlessly) in the sandbox, ignore when it does. 58 // pthread_setname() fails (harmlessly) in the sandbox, ignore when it does.
66 // See http://crbug.com/47058 59 // See http://crbug.com/47058
67 dynamic_pthread_setname_np(shortened_name.c_str()); 60 dynamic_pthread_setname_np(shortened_name.c_str());
68 } 61 }
69 62
70 // static
71 const char* PlatformThread::GetName() {
72 return current_thread_name.Pointer()->Get();
73 }
74
75 namespace { 63 namespace {
76 64
77 void SetPriorityNormal(mach_port_t mach_thread_id) { 65 void SetPriorityNormal(mach_port_t mach_thread_id) {
78 // Make thread standard policy. 66 // Make thread standard policy.
79 // Please note that this call could fail in rare cases depending 67 // Please note that this call could fail in rare cases depending
80 // on runtime conditions. 68 // on runtime conditions.
81 thread_standard_policy policy; 69 thread_standard_policy policy;
82 kern_return_t result = thread_policy_set(mach_thread_id, 70 kern_return_t result = thread_policy_set(mach_thread_id,
83 THREAD_STANDARD_POLICY, 71 THREAD_STANDARD_POLICY,
84 (thread_policy_t)&policy, 72 (thread_policy_t)&policy,
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 case kThreadPriority_Normal: 169 case kThreadPriority_Normal:
182 SetPriorityNormal(mach_thread_id); 170 SetPriorityNormal(mach_thread_id);
183 break; 171 break;
184 case kThreadPriority_RealtimeAudio: 172 case kThreadPriority_RealtimeAudio:
185 SetPriorityRealtimeAudio(mach_thread_id); 173 SetPriorityRealtimeAudio(mach_thread_id);
186 break; 174 break;
187 } 175 }
188 } 176 }
189 177
190 } // namespace base 178 } // namespace base
OLDNEW
« no previous file with comments | « base/debug/trace_event_unittest.cc ('k') | base/threading/platform_thread_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698