michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/platform_thread.h" michael@0: michael@0: #import michael@0: #include michael@0: michael@0: #include "base/logging.h" michael@0: #include "base/scoped_nsautorelease_pool.h" michael@0: michael@0: // A simple class that demonstrates our impressive ability to do nothing. michael@0: @interface NoOp : NSObject michael@0: michael@0: // Does the deed. Or does it? michael@0: + (void)noOp; michael@0: michael@0: @end michael@0: michael@0: @implementation NoOp michael@0: michael@0: + (void)noOp { michael@0: } michael@0: michael@0: @end michael@0: michael@0: namespace base { michael@0: michael@0: // If Cocoa is to be used on more than one thread, it must know that the michael@0: // application is multithreaded. Since it's possible to enter Cocoa code michael@0: // from threads created by pthread_thread_create, Cocoa won't necessarily michael@0: // be aware that the application is multithreaded. Spawning an NSThread is michael@0: // enough to get Cocoa to set up for multithreaded operation, so this is done michael@0: // if necessary before pthread_thread_create spawns any threads. michael@0: // michael@0: // http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/chapter_4_section_4.html michael@0: void InitThreading() { michael@0: // this is called early in startup, before the event loop, so provide michael@0: // an autorelease pool to prevent leaks here michael@0: ScopedNSAutoreleasePool pool; michael@0: michael@0: static BOOL multithreaded = [NSThread isMultiThreaded]; michael@0: if (!multithreaded) { michael@0: [NSThread detachNewThreadSelector:@selector(noOp) michael@0: toTarget:[NoOp class] michael@0: withObject:nil]; michael@0: multithreaded = YES; michael@0: michael@0: DCHECK([NSThread isMultiThreaded]); michael@0: } michael@0: } michael@0: michael@0: } // namespace base michael@0: michael@0: // static michael@0: void PlatformThread::SetName(const char* name) { michael@0: // pthread_setname_np is only available in 10.6 or later, so test michael@0: // for it at runtime. michael@0: int (*dynamic_pthread_setname_np)(const char*); michael@0: *reinterpret_cast(&dynamic_pthread_setname_np) = michael@0: dlsym(RTLD_DEFAULT, "pthread_setname_np"); michael@0: if (!dynamic_pthread_setname_np) michael@0: return; michael@0: michael@0: // Mac OS X does not expose the length limit of the name, so michael@0: // hardcode it. michael@0: const int kMaxNameLength = 63; michael@0: std::string shortened_name = std::string(name).substr(0, kMaxNameLength); michael@0: // pthread_setname() fails (harmlessly) in the sandbox, ignore when it does. michael@0: // See http://crbug.com/47058 michael@0: michael@0: // The name parameter is copied thus it's safe to release it after calling. michael@0: // Checked against the bionic implementation in bionic/libc/bionic/pthread.c michael@0: dynamic_pthread_setname_np(shortened_name.c_str()); michael@0: } michael@0: