blob: e8482ce7a9e3da1e5a3ef03ece888c056edc38d2 [file] [log] [blame]
/*
* libjingle
* Copyright 2007, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Helper function for using Cocoa with Posix threading.
#import <assert.h>
#import <Foundation/Foundation.h>
#import "talk/base/maccocoathreadhelper.h"
// A dummy thread object which essentially does nothing.
// This is used in InitCocoaMultiThreading method.
@interface DummyThreadObject : NSObject {
}
+ (void)dummyFunc:(NSObject*)obj;
@end
@implementation DummyThreadObject
+ (void)dummyFunc:(NSObject*)obj {
// nothing to do
}
@end
namespace talk_base {
// Cocoa must be "put into multithreading mode" before Cocoa functionality can
// be used on POSIX threads. The way to do that is to spawn one thread that may
// immediately exit.
void InitCocoaMultiThreading() {
if ([NSThread isMultiThreaded] == NO) {
// The sole purpose of this autorelease pool is to avoid a console
// message on Leopard that tells us we're autoreleasing the thread
// with no autorelease pool in place; we can't set up an autorelease
// pool before this, because this is executed from an initializer,
// which is run before main. This means we leak an autorelease pool,
// and one thread, and if other objects are set up in initializers after
// this they'll be silently added to this pool and never released.
// Doing NSAutoreleasePool* hack = [[NSAutoreleasePool alloc] init];
// causes unused variable error.
NSAutoreleasePool* hack;
hack = [[NSAutoreleasePool alloc] init];
[NSThread detachNewThreadSelector:@selector(dummyFunc:)
toTarget:[DummyThreadObject class]
withObject:nil];
}
assert([NSThread isMultiThreaded]);
}
} // namespace talk_base