Skip to content

Commit

Permalink
Move some Threading classes over from NativeAOT (#47327)
Browse files Browse the repository at this point in the history
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
CoffeeFlux authored Jan 22, 2021
1 parent ea65638 commit 33c7c4d
Show file tree
Hide file tree
Showing 9 changed files with 2,346 additions and 0 deletions.
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;
}
}
}
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;
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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);
}
}
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);
}
}
}
}
Loading

0 comments on commit 33c7c4d

Please sign in to comment.