OLD | NEW |
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 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 #include <dlfcn.h> | 8 #include <dlfcn.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 | 11 |
12 // A simple class that demonstrates our impressive ability to do nothing. | |
13 @interface NoOp : NSObject | |
14 | |
15 // Does the deed. Or does it? | |
16 + (void)noOp; | |
17 | |
18 @end | |
19 | |
20 @implementation NoOp | |
21 | |
22 + (void)noOp { | |
23 } | |
24 | |
25 @end | |
26 | |
27 namespace base { | 12 namespace base { |
28 | 13 |
29 // If Cocoa is to be used on more than one thread, it must know that the | 14 // If Cocoa is to be used on more than one thread, it must know that the |
30 // application is multithreaded. Since it's possible to enter Cocoa code | 15 // application is multithreaded. Since it's possible to enter Cocoa code |
31 // from threads created by pthread_thread_create, Cocoa won't necessarily | 16 // from threads created by pthread_thread_create, Cocoa won't necessarily |
32 // be aware that the application is multithreaded. Spawning an NSThread is | 17 // be aware that the application is multithreaded. Spawning an NSThread is |
33 // enough to get Cocoa to set up for multithreaded operation, so this is done | 18 // enough to get Cocoa to set up for multithreaded operation, so this is done |
34 // if necessary before pthread_thread_create spawns any threads. | 19 // if necessary before pthread_thread_create spawns any threads. |
35 // | 20 // |
36 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea
tingThreads/chapter_4_section_4.html | 21 // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Crea
tingThreads/chapter_4_section_4.html |
37 void InitThreading() { | 22 void InitThreading() { |
38 static BOOL multithreaded = [NSThread isMultiThreaded]; | 23 static BOOL multithreaded = [NSThread isMultiThreaded]; |
39 if (!multithreaded) { | 24 if (!multithreaded) { |
40 [NSThread detachNewThreadSelector:@selector(noOp) | 25 // +[NSObject class] is idempotent. |
41 toTarget:[NoOp class] | 26 [NSThread detachNewThreadSelector:@selector(class) |
| 27 toTarget:[NSObject class] |
42 withObject:nil]; | 28 withObject:nil]; |
43 multithreaded = YES; | 29 multithreaded = YES; |
44 | 30 |
45 DCHECK([NSThread isMultiThreaded]); | 31 DCHECK([NSThread isMultiThreaded]); |
46 } | 32 } |
47 } | 33 } |
48 | 34 |
49 } // namespace base | 35 } // namespace base |
50 | 36 |
51 // static | 37 // static |
52 void PlatformThread::SetName(const char* name) { | 38 void PlatformThread::SetName(const char* name) { |
53 // pthread_setname_np is only available in 10.6 or later, so test | 39 // pthread_setname_np is only available in 10.6 or later, so test |
54 // for it at runtime. | 40 // for it at runtime. |
55 int (*dynamic_pthread_setname_np)(const char*); | 41 int (*dynamic_pthread_setname_np)(const char*); |
56 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = | 42 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
57 dlsym(RTLD_DEFAULT, "pthread_setname_np"); | 43 dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
58 if (!dynamic_pthread_setname_np) | 44 if (!dynamic_pthread_setname_np) |
59 return; | 45 return; |
60 | 46 |
61 // Mac OS X does not expose the length limit of the name, so | 47 // Mac OS X does not expose the length limit of the name, so |
62 // hardcode it. | 48 // hardcode it. |
63 const int kMaxNameLength = 63; | 49 const int kMaxNameLength = 63; |
64 std::string shortened_name = std::string(name).substr(0, kMaxNameLength); | 50 std::string shortened_name = std::string(name).substr(0, kMaxNameLength); |
65 // pthread_setname() fails (harmlessly) in the sandbox, ignore when it does. | 51 // pthread_setname() fails (harmlessly) in the sandbox, ignore when it does. |
66 // See http://crbug.com/47058 | 52 // See http://crbug.com/47058 |
67 dynamic_pthread_setname_np(shortened_name.c_str()); | 53 dynamic_pthread_setname_np(shortened_name.c_str()); |
68 } | 54 } |
OLD | NEW |