-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move some Threading classes over from NativeAOT (#47327)
This PR intentionally does not include any of the modifications on top of them or other files that will be needed to wire them up to the build.
- Loading branch information
1 parent
ea65638
commit 33c7c4d
Showing
9 changed files
with
2,346 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeWaitHandle.Unix.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,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Threading; | ||
|
||
namespace Microsoft.Win32.SafeHandles | ||
{ | ||
public sealed partial class SafeWaitHandle : SafeHandleZeroOrMinusOneIsInvalid | ||
{ | ||
protected override bool ReleaseHandle() | ||
{ | ||
WaitSubsystem.DeleteHandle(handle); | ||
return true; | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/libraries/System.Private.CoreLib/src/System/Threading/EventWaitHandle.Unix.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,83 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.Win32.SafeHandles; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Threading | ||
{ | ||
public partial class EventWaitHandle | ||
{ | ||
private void CreateEventCore(bool initialState, EventResetMode mode, string name, out bool createdNew) | ||
{ | ||
if (name != null) | ||
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives); | ||
|
||
SafeWaitHandle = WaitSubsystem.NewEvent(initialState, mode); | ||
createdNew = true; | ||
} | ||
|
||
private static OpenExistingResult OpenExistingWorker(string name, out EventWaitHandle result) | ||
{ | ||
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives); | ||
} | ||
|
||
public bool Reset() | ||
{ | ||
SafeWaitHandle waitHandle = ValidateHandle(); | ||
try | ||
{ | ||
WaitSubsystem.ResetEvent(waitHandle.DangerousGetHandle()); | ||
return true; | ||
} | ||
finally | ||
{ | ||
waitHandle.DangerousRelease(); | ||
} | ||
} | ||
|
||
public bool Set() | ||
{ | ||
SafeWaitHandle waitHandle = ValidateHandle(); | ||
try | ||
{ | ||
WaitSubsystem.SetEvent(waitHandle.DangerousGetHandle()); | ||
return true; | ||
} | ||
finally | ||
{ | ||
waitHandle.DangerousRelease(); | ||
} | ||
} | ||
|
||
internal static bool Set(SafeWaitHandle waitHandle) | ||
{ | ||
waitHandle.DangerousAddRef(); | ||
try | ||
{ | ||
WaitSubsystem.SetEvent(waitHandle.DangerousGetHandle()); | ||
return true; | ||
} | ||
finally | ||
{ | ||
waitHandle.DangerousRelease(); | ||
} | ||
} | ||
|
||
private SafeWaitHandle ValidateHandle() | ||
{ | ||
// The field value is modifiable via the public <see cref="WaitHandle.SafeWaitHandle"/> property, save it locally | ||
// to ensure that one instance is used in all places in this method | ||
SafeWaitHandle waitHandle = SafeWaitHandle; | ||
if (waitHandle.IsInvalid) | ||
{ | ||
ThrowInvalidHandleException(); | ||
} | ||
|
||
waitHandle.DangerousAddRef(); | ||
return waitHandle; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/libraries/System.Private.CoreLib/src/System/Threading/Mutex.Unix.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,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.Win32.SafeHandles; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Threading | ||
{ | ||
public sealed partial class Mutex | ||
{ | ||
private void CreateMutexCore(bool initiallyOwned, string name, out bool createdNew) | ||
{ | ||
if (name != null) | ||
{ | ||
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives); | ||
} | ||
|
||
SafeWaitHandle = WaitSubsystem.NewMutex(initiallyOwned); | ||
createdNew = true; | ||
} | ||
|
||
private static OpenExistingResult OpenExistingWorker(string name, out Mutex result) | ||
{ | ||
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives); | ||
} | ||
|
||
public void ReleaseMutex() | ||
{ | ||
// The field value is modifiable via the public <see cref="WaitHandle.SafeWaitHandle"/> property, save it locally | ||
// to ensure that one instance is used in all places in this method | ||
SafeWaitHandle waitHandle = SafeWaitHandle; | ||
if (waitHandle.IsInvalid) | ||
{ | ||
ThrowInvalidHandleException(); | ||
} | ||
|
||
waitHandle.DangerousAddRef(); | ||
try | ||
{ | ||
WaitSubsystem.ReleaseMutex(waitHandle.DangerousGetHandle()); | ||
} | ||
finally | ||
{ | ||
waitHandle.DangerousRelease(); | ||
} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.Unix.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,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.Win32.SafeHandles; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Threading | ||
{ | ||
public sealed partial class Semaphore | ||
{ | ||
private void CreateSemaphoreCore(int initialCount, int maximumCount, string name, out bool createdNew) | ||
{ | ||
if (name != null) | ||
{ | ||
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives); | ||
} | ||
|
||
SafeWaitHandle = WaitSubsystem.NewSemaphore(initialCount, maximumCount); | ||
createdNew = true; | ||
} | ||
|
||
private static OpenExistingResult OpenExistingWorker(string name, out Semaphore result) | ||
{ | ||
throw new PlatformNotSupportedException(SR.PlatformNotSupported_NamedSynchronizationPrimitives); | ||
} | ||
|
||
private int ReleaseCore(int releaseCount) | ||
{ | ||
// The field value is modifiable via the public <see cref="WaitHandle.SafeWaitHandle"/> property, save it locally | ||
// to ensure that one instance is used in all places in this method | ||
SafeWaitHandle waitHandle = SafeWaitHandle; | ||
if (waitHandle.IsInvalid) | ||
{ | ||
ThrowInvalidHandleException(); | ||
} | ||
|
||
waitHandle.DangerousAddRef(); | ||
try | ||
{ | ||
return WaitSubsystem.ReleaseSemaphore(waitHandle.DangerousGetHandle(), releaseCount); | ||
} | ||
finally | ||
{ | ||
waitHandle.DangerousRelease(); | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Unix.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. | ||
|
||
using Microsoft.Win32.SafeHandles; | ||
|
||
namespace System.Threading | ||
{ | ||
public abstract partial class WaitHandle | ||
{ | ||
private static int WaitOneCore(IntPtr handle, int millisecondsTimeout) => | ||
WaitSubsystem.Wait(handle, millisecondsTimeout, true); | ||
|
||
internal static int WaitMultipleIgnoringSyncContext(Span<IntPtr> handles, bool waitAll, int millisecondsTimeout) => | ||
WaitSubsystem.Wait(handles, waitAll, millisecondsTimeout); | ||
|
||
private static int SignalAndWaitCore(IntPtr handleToSignal, IntPtr handleToWaitOn, int millisecondsTimeout) => | ||
WaitSubsystem.SignalAndWait(handleToSignal, handleToWaitOn, millisecondsTimeout); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...libraries/System.Private.CoreLib/src/System/Threading/WaitSubsystem.HandleManager.Unix.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,56 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Threading | ||
{ | ||
internal static partial class WaitSubsystem | ||
{ | ||
private static class HandleManager | ||
{ | ||
public static IntPtr NewHandle(WaitableObject waitableObject) | ||
{ | ||
Debug.Assert(waitableObject != null); | ||
|
||
IntPtr handle = RuntimeImports.RhHandleAlloc(waitableObject, GCHandleType.Normal); | ||
|
||
// SafeWaitHandle treats -1 and 0 as invalid, and the handle should not be these values anyway | ||
Debug.Assert(handle != IntPtr.Zero); | ||
Debug.Assert(handle != new IntPtr(-1)); | ||
return handle; | ||
} | ||
|
||
public static WaitableObject FromHandle(IntPtr handle) | ||
{ | ||
if (handle == IntPtr.Zero || handle == new IntPtr(-1)) | ||
{ | ||
WaitHandle.ThrowInvalidHandleException(); | ||
} | ||
|
||
// We don't know if any other handles are invalid, and this may crash or otherwise do bad things, that is by | ||
// design, IntPtr is unsafe by nature. | ||
return (WaitableObject)RuntimeImports.RhHandleGet(handle); | ||
} | ||
|
||
/// <summary> | ||
/// Unlike on Windows, a handle may not be deleted more than once with this implementation | ||
/// </summary> | ||
public static void DeleteHandle(IntPtr handle) | ||
{ | ||
if (handle == IntPtr.Zero || handle == new IntPtr(-1)) | ||
{ | ||
return; | ||
} | ||
|
||
// We don't know if any other handles are invalid, and this may crash or otherwise do bad things, that is by | ||
// design, IntPtr is unsafe by nature. | ||
((WaitableObject)RuntimeImports.RhHandleGet(handle)).OnDeleteHandle(); | ||
RuntimeImports.RhHandleFree(handle); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.