-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b9e50d
commit 86eacff
Showing
18 changed files
with
331 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/libraries/Common/src/Interop/OSX/System.Native/Interop.AutoreleasePool.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Versioning; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Sys | ||
{ | ||
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_CreateAutoreleasePool")] | ||
internal static extern IntPtr CreateAutoreleasePool(); | ||
|
||
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_DrainAutoreleasePool")] | ||
internal static extern void DrainAutoreleasePool(IntPtr ptr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/libraries/Native/Unix/System.Native/pal_autoreleasepool.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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 <stdlib.h> | ||
#include "pal_utilities.h" | ||
|
||
#ifndef _MSC_VER | ||
// Don't warning about not declaring a function with [[noreturn]] since it's only true in Debug mode. | ||
#pragma GCC diagnostic ignored "-Wmissing-noreturn" | ||
#endif | ||
|
||
// These functions should not be used, but they need to be defined | ||
// to satisfy the tooling we used to enable redirecting P/Invokes | ||
// for the single file scenario. | ||
void* SystemNative_CreateAutoreleasePool(void) | ||
{ | ||
assert_err(false, "Autorelease pools not supported on this platform.", EINVAL); | ||
return NULL; | ||
} | ||
|
||
void SystemNative_DrainAutoreleasePool(void* pool) | ||
{ | ||
(void)pool; | ||
assert_err(false, "Autorelease pools not supported on this platform.", EINVAL); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/libraries/Native/Unix/System.Native/pal_autoreleasepool.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#pragma once | ||
|
||
#include "pal_compiler.h" | ||
#include "pal_types.h" | ||
|
||
/** | ||
* Creates an pool to automatically release applicable ref-counted resources. | ||
*/ | ||
PALEXPORT void* SystemNative_CreateAutoreleasePool(void); | ||
|
||
/** | ||
* Drains and releases a pool created by SystemNative_CreateAutoreleasePool. | ||
*/ | ||
PALEXPORT void SystemNative_DrainAutoreleasePool(void* pool); |
40 changes: 40 additions & 0 deletions
40
src/libraries/Native/Unix/System.Native/pal_autoreleasepool.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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]; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.OSX.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<linker> | ||
<assembly fullname="System.Private.CoreLib"> | ||
<type fullname="System.Threading.ThreadPool" feature="System.Threading.ThreadPool.EnableDispatchAutoreleasePool" featurevalue="false"> | ||
<method signature="System.Boolean get_EnableDispatchAutoreleasePool()" body="stub" value="false" /> | ||
</type> | ||
</assembly> | ||
</linker> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...es/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.AutoreleasePool.OSX.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Versioning; | ||
|
||
namespace System.Threading | ||
{ | ||
public static partial class ThreadPool | ||
{ | ||
internal static bool EnableDispatchAutoreleasePool { get; } = | ||
AppContextConfigHelper.GetBooleanConfig("System.Threading.ThreadPool.EnableDispatchAutoreleasePool", false); | ||
} | ||
|
||
internal sealed partial class ThreadPoolWorkQueue | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static void DispatchItemWithAutoreleasePool(object workItem, Thread currentThread) | ||
{ | ||
IntPtr autoreleasePool = Interop.Sys.CreateAutoreleasePool(); | ||
try | ||
{ | ||
#pragma warning disable CS0162 // Unreachable code detected. EnableWorkerTracking may be a constant in some runtimes. | ||
if (ThreadPool.EnableWorkerTracking) | ||
{ | ||
DispatchWorkItemWithWorkerTracking(workItem, currentThread); | ||
} | ||
else | ||
{ | ||
DispatchWorkItem(workItem, currentThread); | ||
} | ||
#pragma warning restore CS0162 | ||
} | ||
finally | ||
{ | ||
Interop.Sys.DrainAutoreleasePool(autoreleasePool); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.