Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/platform_thread.h" |
michael@0 | 6 | |
michael@0 | 7 | #import <Foundation/Foundation.h> |
michael@0 | 8 | #include <dlfcn.h> |
michael@0 | 9 | |
michael@0 | 10 | #include "base/logging.h" |
michael@0 | 11 | #include "base/scoped_nsautorelease_pool.h" |
michael@0 | 12 | |
michael@0 | 13 | // A simple class that demonstrates our impressive ability to do nothing. |
michael@0 | 14 | @interface NoOp : NSObject |
michael@0 | 15 | |
michael@0 | 16 | // Does the deed. Or does it? |
michael@0 | 17 | + (void)noOp; |
michael@0 | 18 | |
michael@0 | 19 | @end |
michael@0 | 20 | |
michael@0 | 21 | @implementation NoOp |
michael@0 | 22 | |
michael@0 | 23 | + (void)noOp { |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | @end |
michael@0 | 27 | |
michael@0 | 28 | namespace base { |
michael@0 | 29 | |
michael@0 | 30 | // If Cocoa is to be used on more than one thread, it must know that the |
michael@0 | 31 | // application is multithreaded. Since it's possible to enter Cocoa code |
michael@0 | 32 | // from threads created by pthread_thread_create, Cocoa won't necessarily |
michael@0 | 33 | // be aware that the application is multithreaded. Spawning an NSThread is |
michael@0 | 34 | // enough to get Cocoa to set up for multithreaded operation, so this is done |
michael@0 | 35 | // if necessary before pthread_thread_create spawns any threads. |
michael@0 | 36 | // |
michael@0 | 37 | // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/chapter_4_section_4.html |
michael@0 | 38 | void InitThreading() { |
michael@0 | 39 | // this is called early in startup, before the event loop, so provide |
michael@0 | 40 | // an autorelease pool to prevent leaks here |
michael@0 | 41 | ScopedNSAutoreleasePool pool; |
michael@0 | 42 | |
michael@0 | 43 | static BOOL multithreaded = [NSThread isMultiThreaded]; |
michael@0 | 44 | if (!multithreaded) { |
michael@0 | 45 | [NSThread detachNewThreadSelector:@selector(noOp) |
michael@0 | 46 | toTarget:[NoOp class] |
michael@0 | 47 | withObject:nil]; |
michael@0 | 48 | multithreaded = YES; |
michael@0 | 49 | |
michael@0 | 50 | DCHECK([NSThread isMultiThreaded]); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | } // namespace base |
michael@0 | 55 | |
michael@0 | 56 | // static |
michael@0 | 57 | void PlatformThread::SetName(const char* name) { |
michael@0 | 58 | // pthread_setname_np is only available in 10.6 or later, so test |
michael@0 | 59 | // for it at runtime. |
michael@0 | 60 | int (*dynamic_pthread_setname_np)(const char*); |
michael@0 | 61 | *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
michael@0 | 62 | dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
michael@0 | 63 | if (!dynamic_pthread_setname_np) |
michael@0 | 64 | return; |
michael@0 | 65 | |
michael@0 | 66 | // Mac OS X does not expose the length limit of the name, so |
michael@0 | 67 | // hardcode it. |
michael@0 | 68 | const int kMaxNameLength = 63; |
michael@0 | 69 | std::string shortened_name = std::string(name).substr(0, kMaxNameLength); |
michael@0 | 70 | // pthread_setname() fails (harmlessly) in the sandbox, ignore when it does. |
michael@0 | 71 | // See http://crbug.com/47058 |
michael@0 | 72 | |
michael@0 | 73 | // The name parameter is copied thus it's safe to release it after calling. |
michael@0 | 74 | // Checked against the bionic implementation in bionic/libc/bionic/pthread.c |
michael@0 | 75 | dynamic_pthread_setname_np(shortened_name.c_str()); |
michael@0 | 76 | } |
michael@0 | 77 |