1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/platform_thread_mac.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,77 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "base/platform_thread.h" 1.9 + 1.10 +#import <Foundation/Foundation.h> 1.11 +#include <dlfcn.h> 1.12 + 1.13 +#include "base/logging.h" 1.14 +#include "base/scoped_nsautorelease_pool.h" 1.15 + 1.16 +// A simple class that demonstrates our impressive ability to do nothing. 1.17 +@interface NoOp : NSObject 1.18 + 1.19 +// Does the deed. Or does it? 1.20 ++ (void)noOp; 1.21 + 1.22 +@end 1.23 + 1.24 +@implementation NoOp 1.25 + 1.26 ++ (void)noOp { 1.27 +} 1.28 + 1.29 +@end 1.30 + 1.31 +namespace base { 1.32 + 1.33 +// If Cocoa is to be used on more than one thread, it must know that the 1.34 +// application is multithreaded. Since it's possible to enter Cocoa code 1.35 +// from threads created by pthread_thread_create, Cocoa won't necessarily 1.36 +// be aware that the application is multithreaded. Spawning an NSThread is 1.37 +// enough to get Cocoa to set up for multithreaded operation, so this is done 1.38 +// if necessary before pthread_thread_create spawns any threads. 1.39 +// 1.40 +// http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/chapter_4_section_4.html 1.41 +void InitThreading() { 1.42 + // this is called early in startup, before the event loop, so provide 1.43 + // an autorelease pool to prevent leaks here 1.44 + ScopedNSAutoreleasePool pool; 1.45 + 1.46 + static BOOL multithreaded = [NSThread isMultiThreaded]; 1.47 + if (!multithreaded) { 1.48 + [NSThread detachNewThreadSelector:@selector(noOp) 1.49 + toTarget:[NoOp class] 1.50 + withObject:nil]; 1.51 + multithreaded = YES; 1.52 + 1.53 + DCHECK([NSThread isMultiThreaded]); 1.54 + } 1.55 +} 1.56 + 1.57 +} // namespace base 1.58 + 1.59 +// static 1.60 +void PlatformThread::SetName(const char* name) { 1.61 + // pthread_setname_np is only available in 10.6 or later, so test 1.62 + // for it at runtime. 1.63 + int (*dynamic_pthread_setname_np)(const char*); 1.64 + *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = 1.65 + dlsym(RTLD_DEFAULT, "pthread_setname_np"); 1.66 + if (!dynamic_pthread_setname_np) 1.67 + return; 1.68 + 1.69 + // Mac OS X does not expose the length limit of the name, so 1.70 + // hardcode it. 1.71 + const int kMaxNameLength = 63; 1.72 + std::string shortened_name = std::string(name).substr(0, kMaxNameLength); 1.73 + // pthread_setname() fails (harmlessly) in the sandbox, ignore when it does. 1.74 + // See http://crbug.com/47058 1.75 + 1.76 + // The name parameter is copied thus it's safe to release it after calling. 1.77 + // Checked against the bionic implementation in bionic/libc/bionic/pthread.c 1.78 + dynamic_pthread_setname_np(shortened_name.c_str()); 1.79 +} 1.80 +