Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pal_autoreleasepool.m « System.Native « Unix « Native « libraries « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 77030b07bf381ce2ab91bccd24e5f1f2bbbbe812 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "pal_autoreleasepool.h"
#include <Foundation/Foundation.h>

@interface PlaceholderObject : NSObject
- (void)noop:(id)_;
@end

@implementation PlaceholderObject : NSObject
- (void)noop:(id)_
{
    [self release];
}
@end

void* SystemNative_CreateAutoreleasePool(void)
{
    if (![NSThread isMultiThreaded])
    {
        // Start another no-op thread with the NSThread APIs to get NSThread into multithreaded mode.
        // The NSAutoReleasePool APIs can't be used on secondary threads until NSThread is in multithreaded mode.
        // See https://developer.apple.com/documentation/foundation/nsautoreleasepool for more information.
        PlaceholderObject* placeholderObject = [[PlaceholderObject alloc] init];

        // We need to use detachNewThreadSelector to put NSThread into multithreaded mode.
        // We can't use detachNewThreadWithBlock since it doesn't change NSThread into multithreaded mode for some reason.
        // See https://developer.apple.com/documentation/foundation/nswillbecomemultithreadednotification for more information.
        [NSThread detachNewThreadSelector:@selector(noop:) toTarget:placeholderObject withObject:nil];
    }
    assert([NSThread isMultiThreaded]);

    return [[NSAutoreleasePool alloc] init];
}

void SystemNative_DrainAutoreleasePool(void* pool)
{
    [((NSAutoreleasePool*)pool) drain];
}