blob: 7bf9e9206bd4950f6860c240f77a3a1e66fda882 [file] [log] [blame]
henrike@webrtc.orgf7795df2014-05-13 18:00:26 +00001/*
2 * Copyright 2007 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10// Helper function for using Cocoa with Posix threading.
11
12#import <assert.h>
13#import <Foundation/Foundation.h>
14
15#import "webrtc/base/maccocoathreadhelper.h"
16
17namespace rtc {
18
19// Cocoa must be "put into multithreading mode" before Cocoa functionality can
20// be used on POSIX threads. The way to do that is to spawn one thread that may
21// immediately exit.
22void InitCocoaMultiThreading() {
23 if ([NSThread isMultiThreaded] == NO) {
24 // The sole purpose of this autorelease pool is to avoid a console
25 // message on Leopard that tells us we're autoreleasing the thread
26 // with no autorelease pool in place; we can't set up an autorelease
27 // pool before this, because this is executed from an initializer,
28 // which is run before main. This means we leak an autorelease pool,
29 // and one thread, and if other objects are set up in initializers after
30 // this they'll be silently added to this pool and never released.
31
32 // Doing NSAutoreleasePool* hack = [[NSAutoreleasePool alloc] init];
33 // causes unused variable error.
34 NSAutoreleasePool* hack;
35 hack = [[NSAutoreleasePool alloc] init];
36 [NSThread detachNewThreadSelector:@selector(class)
37 toTarget:[NSObject class]
38 withObject:nil];
39 }
40
41 assert([NSThread isMultiThreaded]);
42}
43
44} // namespace rtc