OLD | NEW |
---|---|
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 #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/logging.h" | 13 #include "base/logging.h" |
14 #include "base/threading/thread_local.h" | |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 | 17 |
18 namespace { | |
19 | |
20 static ThreadLocalPointer<char> current_thread_name; | |
21 | |
22 } | |
brettw
2011/08/08 20:10:37
Nee // namespace
| |
23 | |
17 // If Cocoa is to be used on more than one thread, it must know that the | 24 // If Cocoa is to be used on more than one thread, it must know that the |
18 // application is multithreaded. Since it's possible to enter Cocoa code | 25 // application is multithreaded. Since it's possible to enter Cocoa code |
19 // from threads created by pthread_thread_create, Cocoa won't necessarily | 26 // from threads created by pthread_thread_create, Cocoa won't necessarily |
20 // be aware that the application is multithreaded. Spawning an NSThread is | 27 // be aware that the application is multithreaded. Spawning an NSThread is |
21 // enough to get Cocoa to set up for multithreaded operation, so this is done | 28 // enough to get Cocoa to set up for multithreaded operation, so this is done |
22 // if necessary before pthread_thread_create spawns any threads. | 29 // if necessary before pthread_thread_create spawns any threads. |
23 // | 30 // |
24 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea tingThreads/chapter_4_section_4.html | 31 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea tingThreads/chapter_4_section_4.html |
25 void InitThreading() { | 32 void InitThreading() { |
26 static BOOL multithreaded = [NSThread isMultiThreaded]; | 33 static BOOL multithreaded = [NSThread isMultiThreaded]; |
27 if (!multithreaded) { | 34 if (!multithreaded) { |
28 // +[NSObject class] is idempotent. | 35 // +[NSObject class] is idempotent. |
29 [NSThread detachNewThreadSelector:@selector(class) | 36 [NSThread detachNewThreadSelector:@selector(class) |
30 toTarget:[NSObject class] | 37 toTarget:[NSObject class] |
31 withObject:nil]; | 38 withObject:nil]; |
32 multithreaded = YES; | 39 multithreaded = YES; |
33 | 40 |
34 DCHECK([NSThread isMultiThreaded]); | 41 DCHECK([NSThread isMultiThreaded]); |
35 } | 42 } |
36 } | 43 } |
37 | 44 |
38 // static | 45 // static |
39 void PlatformThread::SetName(const char* name) { | 46 void PlatformThread::SetName(const char* name) { |
47 current_thread_name.Set(const_cast<char*>(name)); | |
48 | |
40 // pthread_setname_np is only available in 10.6 or later, so test | 49 // pthread_setname_np is only available in 10.6 or later, so test |
41 // for it at runtime. | 50 // for it at runtime. |
42 int (*dynamic_pthread_setname_np)(const char*); | 51 int (*dynamic_pthread_setname_np)(const char*); |
43 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = | 52 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
44 dlsym(RTLD_DEFAULT, "pthread_setname_np"); | 53 dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
45 if (!dynamic_pthread_setname_np) | 54 if (!dynamic_pthread_setname_np) |
46 return; | 55 return; |
47 | 56 |
48 // Mac OS X does not expose the length limit of the name, so | 57 // Mac OS X does not expose the length limit of the name, so |
49 // hardcode it. | 58 // hardcode it. |
50 const int kMaxNameLength = 63; | 59 const int kMaxNameLength = 63; |
51 std::string shortened_name = std::string(name).substr(0, kMaxNameLength); | 60 std::string shortened_name = std::string(name).substr(0, kMaxNameLength); |
52 // pthread_setname() fails (harmlessly) in the sandbox, ignore when it does. | 61 // pthread_setname() fails (harmlessly) in the sandbox, ignore when it does. |
53 // See http://crbug.com/47058 | 62 // See http://crbug.com/47058 |
54 dynamic_pthread_setname_np(shortened_name.c_str()); | 63 dynamic_pthread_setname_np(shortened_name.c_str()); |
55 } | 64 } |
56 | 65 |
66 // static | |
67 const char* PlatformThread::GetName() { | |
68 return current_thread_name.Get(); | |
69 } | |
70 | |
57 namespace { | 71 namespace { |
58 | 72 |
59 void SetPriorityNormal(mach_port_t mach_thread_id) { | 73 void SetPriorityNormal(mach_port_t mach_thread_id) { |
60 // Make thread standard policy. | 74 // Make thread standard policy. |
61 thread_standard_policy policy; | 75 thread_standard_policy policy; |
62 kern_return_t result = thread_policy_set(mach_thread_id, | 76 kern_return_t result = thread_policy_set(mach_thread_id, |
63 THREAD_STANDARD_POLICY, | 77 THREAD_STANDARD_POLICY, |
64 (thread_policy_t)&policy, | 78 (thread_policy_t)&policy, |
65 THREAD_STANDARD_POLICY_COUNT); | 79 THREAD_STANDARD_POLICY_COUNT); |
66 | 80 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 case kThreadPriority_Normal: | 161 case kThreadPriority_Normal: |
148 SetPriorityNormal(mach_thread_id); | 162 SetPriorityNormal(mach_thread_id); |
149 break; | 163 break; |
150 case kThreadPriority_RealtimeAudio: | 164 case kThreadPriority_RealtimeAudio: |
151 SetPriorityRealtimeAudio(mach_thread_id); | 165 SetPriorityRealtimeAudio(mach_thread_id); |
152 break; | 166 break; |
153 } | 167 } |
154 } | 168 } |
155 | 169 |
156 } // namespace base | 170 } // namespace base |
OLD | NEW |