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 | 9 |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/safe_strerror_posix.h" |
10 | 12 |
11 // A simple class that demonstrates our impressive ability to do nothing. | 13 // A simple class that demonstrates our impressive ability to do nothing. |
12 @interface NoOp : NSObject | 14 @interface NoOp : NSObject |
13 | 15 |
14 // Does the deed. Or does it? | 16 // Does the deed. Or does it? |
15 + (void)noOp; | 17 + (void)noOp; |
16 | 18 |
17 @end | 19 @end |
18 | 20 |
19 @implementation NoOp | 21 @implementation NoOp |
(...skipping 19 matching lines...) Expand all Loading... |
39 [NSThread detachNewThreadSelector:@selector(noOp) | 41 [NSThread detachNewThreadSelector:@selector(noOp) |
40 toTarget:[NoOp class] | 42 toTarget:[NoOp class] |
41 withObject:nil]; | 43 withObject:nil]; |
42 multithreaded = YES; | 44 multithreaded = YES; |
43 | 45 |
44 DCHECK([NSThread isMultiThreaded]); | 46 DCHECK([NSThread isMultiThreaded]); |
45 } | 47 } |
46 } | 48 } |
47 | 49 |
48 } // namespace base | 50 } // namespace base |
| 51 |
| 52 // static |
| 53 void PlatformThread::SetName(const char* name) { |
| 54 // pthread_setname_np is only available in 10.6 or later, so test |
| 55 // for it at runtime. |
| 56 int (*dynamic_pthread_setname_np)(const char*); |
| 57 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
| 58 dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
| 59 if (!dynamic_pthread_setname_np) |
| 60 return; |
| 61 |
| 62 // Mac OS X does not expose the length limit of the name, so |
| 63 // hardcode it. |
| 64 const int kMaxNameLength = 63; |
| 65 std::string shortened_name = std::string(name).substr(0, kMaxNameLength); |
| 66 int err = dynamic_pthread_setname_np(shortened_name.c_str()); |
| 67 if (err < 0) |
| 68 LOG(ERROR) << "pthread_setname_np: " << safe_strerror(err); |
| 69 } |
OLD | NEW |