From d437066392aebdb3b147a07538b2c57d00cfae89 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Fri, 25 Aug 2023 19:54:18 -0700 Subject: [PATCH] Moved ThanksPool to corlib --- .../src/System/Runtime/InternalCalls.cs | 28 ---------- .../src/System/Runtime/RuntimeExports.cs | 36 ------------ .../Runtime/Augments/RuntimeAugments.cs | 12 ++-- .../src/System.Private.CoreLib.csproj | 4 +- .../src/System/Runtime/RuntimeImports.cs | 56 +++++++++++-------- .../src/System/Runtime/ThunkPool.cs | 22 ++++---- .../src/System/Threading/Monitor.cs | 34 ----------- .../Test.CoreLib/src/Test.CoreLib.csproj | 4 -- 8 files changed, 50 insertions(+), 146 deletions(-) rename src/coreclr/nativeaot/{Runtime.Base => System.Private.CoreLib}/src/System/Runtime/ThunkPool.cs (93%) delete mode 100644 src/coreclr/nativeaot/Test.CoreLib/src/System/Threading/Monitor.cs diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs index 05494c84d0a1b4..01475cf9addef4 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs @@ -257,30 +257,6 @@ internal static extern unsafe IntPtr RhpCallPropagateExceptionCallback( [MethodImpl(MethodImplOptions.InternalCall)] internal static extern unsafe void RhpCopyContextFromExInfo(void* pOSContext, int cbOSContext, EH.PAL_LIMITED_CONTEXT* pPalContext); - [RuntimeImport(Redhawk.BaseName, "RhpGetNumThunkBlocksPerMapping")] - [MethodImpl(MethodImplOptions.InternalCall)] - internal static extern int RhpGetNumThunkBlocksPerMapping(); - - [RuntimeImport(Redhawk.BaseName, "RhpGetNumThunksPerBlock")] - [MethodImpl(MethodImplOptions.InternalCall)] - internal static extern int RhpGetNumThunksPerBlock(); - - [RuntimeImport(Redhawk.BaseName, "RhpGetThunkSize")] - [MethodImpl(MethodImplOptions.InternalCall)] - internal static extern int RhpGetThunkSize(); - - [RuntimeImport(Redhawk.BaseName, "RhpGetThunkDataBlockAddress")] - [MethodImpl(MethodImplOptions.InternalCall)] - internal static extern IntPtr RhpGetThunkDataBlockAddress(IntPtr thunkStubAddress); - - [RuntimeImport(Redhawk.BaseName, "RhpGetThunkStubsBlockAddress")] - [MethodImpl(MethodImplOptions.InternalCall)] - internal static extern IntPtr RhpGetThunkStubsBlockAddress(IntPtr thunkDataAddress); - - [RuntimeImport(Redhawk.BaseName, "RhpGetThunkBlockSize")] - [MethodImpl(MethodImplOptions.InternalCall)] - internal static extern int RhpGetThunkBlockSize(); - [RuntimeImport(Redhawk.BaseName, "RhpGetThreadAbortException")] [MethodImpl(MethodImplOptions.InternalCall)] internal static extern Exception RhpGetThreadAbortException(); @@ -314,10 +290,6 @@ internal static extern unsafe IntPtr RhpCallPropagateExceptionCallback( [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })] internal static extern ulong RhpGetTickCount64(); - [DllImport(Redhawk.BaseName)] - [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })] - internal static extern IntPtr RhAllocateThunksMapping(); - // Enters a no GC region, possibly doing a blocking GC if there is not enough // memory available to satisfy the caller's request. [DllImport(Redhawk.BaseName)] diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/RuntimeExports.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/RuntimeExports.cs index 359c0919363581..1fa421ec3e2454 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/RuntimeExports.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/RuntimeExports.cs @@ -332,42 +332,6 @@ private static unsafe int RhpCalculateStackTraceWorker(IntPtr* pOutputBuffer, ui return success ? (int)nFrames : -(int)nFrames; } - [RuntimeExport("RhCreateThunksHeap")] - public static object RhCreateThunksHeap(IntPtr commonStubAddress) - { - return ThunksHeap.CreateThunksHeap(commonStubAddress); - } - - [RuntimeExport("RhAllocateThunk")] - public static IntPtr RhAllocateThunk(object thunksHeap) - { - return ((ThunksHeap)thunksHeap).AllocateThunk(); - } - - [RuntimeExport("RhFreeThunk")] - public static void RhFreeThunk(object thunksHeap, IntPtr thunkAddress) - { - ((ThunksHeap)thunksHeap).FreeThunk(thunkAddress); - } - - [RuntimeExport("RhSetThunkData")] - public static void RhSetThunkData(object thunksHeap, IntPtr thunkAddress, IntPtr context, IntPtr target) - { - ((ThunksHeap)thunksHeap).SetThunkData(thunkAddress, context, target); - } - - [RuntimeExport("RhTryGetThunkData")] - public static bool RhTryGetThunkData(object thunksHeap, IntPtr thunkAddress, out IntPtr context, out IntPtr target) - { - return ((ThunksHeap)thunksHeap).TryGetThunkData(thunkAddress, out context, out target); - } - - [RuntimeExport("RhGetThunkSize")] - public static int RhGetThunkSize() - { - return InternalCalls.RhpGetThunkSize(); - } - [RuntimeExport("RhGetRuntimeHelperForType")] internal static unsafe IntPtr RhGetRuntimeHelperForType(MethodTable* pEEType, RuntimeHelperKind kind) { diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs index 3aaf11fd0ef336..43b23737f503eb 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs @@ -819,7 +819,7 @@ public static string TryGetMethodDisplayStringFromIp(IntPtr ip) public static object CreateThunksHeap(IntPtr commonStubAddress) { - object newHeap = RuntimeImports.RhCreateThunksHeap(commonStubAddress); + object? newHeap = ThunksHeap.CreateThunksHeap(commonStubAddress); if (newHeap == null) throw new OutOfMemoryException(); return newHeap; @@ -827,7 +827,7 @@ public static object CreateThunksHeap(IntPtr commonStubAddress) public static IntPtr AllocateThunk(object thunksHeap) { - IntPtr newThunk = RuntimeImports.RhAllocateThunk(thunksHeap); + IntPtr newThunk = ((ThunksHeap)thunksHeap).AllocateThunk(); if (newThunk == IntPtr.Zero) throw new OutOfMemoryException(); return newThunk; @@ -835,22 +835,22 @@ public static IntPtr AllocateThunk(object thunksHeap) public static void FreeThunk(object thunksHeap, IntPtr thunkAddress) { - RuntimeImports.RhFreeThunk(thunksHeap, thunkAddress); + ((ThunksHeap)thunksHeap).FreeThunk(thunkAddress); } public static void SetThunkData(object thunksHeap, IntPtr thunkAddress, IntPtr context, IntPtr target) { - RuntimeImports.RhSetThunkData(thunksHeap, thunkAddress, context, target); + ((ThunksHeap)thunksHeap).SetThunkData(thunkAddress, context, target); } public static bool TryGetThunkData(object thunksHeap, IntPtr thunkAddress, out IntPtr context, out IntPtr target) { - return RuntimeImports.RhTryGetThunkData(thunksHeap, thunkAddress, out context, out target); + return ((ThunksHeap)thunksHeap).TryGetThunkData(thunkAddress, out context, out target); } public static int GetThunkSize() { - return RuntimeImports.RhGetThunkSize(); + return RuntimeImports.RhpGetThunkSize(); } public static Delegate CreateObjectArrayDelegate(Type delegateType, Func invoker) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj index 5b97cc9d8464c4..a6f249eed8dd91 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj @@ -201,6 +201,7 @@ + @@ -538,9 +539,6 @@ Runtime.Base\src\System\Runtime\StackFrameIterator.cs - - Runtime.Base\src\System\Runtime\ThunkPool.cs - Runtime.Base\src\System\Runtime\TypeCast.cs diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs index 6c6d682cda5d62..236ef9ebfe3793 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs @@ -342,6 +342,38 @@ internal static IntPtr RhHandleAllocDependent(object primary, object secondary) [RuntimeImport(RuntimeLibrary, "RhHandleSetDependentSecondary")] internal static extern void RhHandleSetDependentSecondary(IntPtr handle, object secondary); + // + // calls to runtime for thunk pool + // + + [RuntimeImport(Redhawk.BaseName, "RhpGetNumThunkBlocksPerMapping")] + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern int RhpGetNumThunkBlocksPerMapping(); + + [RuntimeImport(Redhawk.BaseName, "RhpGetNumThunksPerBlock")] + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern int RhpGetNumThunksPerBlock(); + + [RuntimeImport(Redhawk.BaseName, "RhpGetThunkSize")] + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern int RhpGetThunkSize(); + + [RuntimeImport(Redhawk.BaseName, "RhpGetThunkDataBlockAddress")] + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern IntPtr RhpGetThunkDataBlockAddress(IntPtr thunkStubAddress); + + [RuntimeImport(Redhawk.BaseName, "RhpGetThunkStubsBlockAddress")] + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern IntPtr RhpGetThunkStubsBlockAddress(IntPtr thunkDataAddress); + + [RuntimeImport(Redhawk.BaseName, "RhpGetThunkBlockSize")] + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern int RhpGetThunkBlockSize(); + + [LibraryImport(RuntimeLibrary, EntryPoint = "RhAllocateThunksMapping")] + [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })] + internal static partial IntPtr RhAllocateThunksMapping(); + // // calls to runtime for type equality checks // @@ -467,30 +499,6 @@ internal static unsafe int RhCompatibleReentrantWaitAny(bool alertable, int time [RuntimeImport(RuntimeLibrary, "RhpResolveInterfaceMethod")] internal static extern IntPtr RhpResolveInterfaceMethod(object pObject, IntPtr pCell); - [MethodImplAttribute(MethodImplOptions.InternalCall)] - [RuntimeImport(RuntimeLibrary, "RhCreateThunksHeap")] - internal static extern object RhCreateThunksHeap(IntPtr commonStubAddress); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - [RuntimeImport(RuntimeLibrary, "RhAllocateThunk")] - internal static extern IntPtr RhAllocateThunk(object thunksHeap); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - [RuntimeImport(RuntimeLibrary, "RhFreeThunk")] - internal static extern void RhFreeThunk(object thunksHeap, IntPtr thunkAddress); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - [RuntimeImport(RuntimeLibrary, "RhSetThunkData")] - internal static extern void RhSetThunkData(object thunksHeap, IntPtr thunkAddress, IntPtr context, IntPtr target); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - [RuntimeImport(RuntimeLibrary, "RhTryGetThunkData")] - internal static extern bool RhTryGetThunkData(object thunksHeap, IntPtr thunkAddress, out IntPtr context, out IntPtr target); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - [RuntimeImport(RuntimeLibrary, "RhGetThunkSize")] - internal static extern int RhGetThunkSize(); - [MethodImplAttribute(MethodImplOptions.InternalCall)] [RuntimeImport(RuntimeLibrary, "RhResolveDispatchOnType")] internal static extern unsafe IntPtr RhResolveDispatchOnType(EETypePtr instanceType, EETypePtr interfaceType, ushort slot, EETypePtr* pGenericContext); diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ThunkPool.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/ThunkPool.cs similarity index 93% rename from src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ThunkPool.cs rename to src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/ThunkPool.cs index 36b9f060c0d4dc..0ef42bc0bebcbb 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ThunkPool.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/ThunkPool.cs @@ -41,10 +41,10 @@ namespace System.Runtime internal static class Constants { public static readonly int ThunkDataSize = 2 * IntPtr.Size; - public static readonly int ThunkCodeSize = InternalCalls.RhpGetThunkSize(); - public static readonly int NumThunksPerBlock = InternalCalls.RhpGetNumThunksPerBlock(); - public static readonly int NumThunkBlocksPerMapping = InternalCalls.RhpGetNumThunkBlocksPerMapping(); - public static readonly uint ThunkBlockSize = (uint)InternalCalls.RhpGetThunkBlockSize(); + public static readonly int ThunkCodeSize = RuntimeImports.RhpGetThunkSize(); + public static readonly int NumThunksPerBlock = RuntimeImports.RhpGetNumThunksPerBlock(); + public static readonly int NumThunkBlocksPerMapping = RuntimeImports.RhpGetNumThunkBlocksPerMapping(); + public static readonly uint ThunkBlockSize = (uint)RuntimeImports.RhpGetThunkBlockSize(); public static readonly nuint ThunkBlockSizeMask = ThunkBlockSize - 1; } @@ -94,7 +94,7 @@ private unsafe ThunksHeap(IntPtr commonStubAddress) if (thunkStubsBlock != IntPtr.Zero) { - IntPtr thunkDataBlock = InternalCalls.RhpGetThunkDataBlockAddress(thunkStubsBlock); + IntPtr thunkDataBlock = RuntimeImports.RhpGetThunkDataBlockAddress(thunkStubsBlock); // Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned) Debug.Assert(((nuint)(nint)thunkDataBlock % Constants.ThunkBlockSize) == 0); @@ -150,7 +150,7 @@ private unsafe bool ExpandHeap() if (thunkStubsBlock != IntPtr.Zero) { - IntPtr thunkDataBlock = InternalCalls.RhpGetThunkDataBlockAddress(thunkStubsBlock); + IntPtr thunkDataBlock = RuntimeImports.RhpGetThunkDataBlockAddress(thunkStubsBlock); // Address of the first thunk data cell should be at the beginning of the thunks data block (page-aligned) Debug.Assert(((nuint)(nint)thunkDataBlock % Constants.ThunkBlockSize) == 0); @@ -214,7 +214,7 @@ public unsafe IntPtr AllocateThunk() Debug.Assert((thunkIndex % Constants.ThunkDataSize) == 0); thunkIndex /= Constants.ThunkDataSize; - IntPtr thunkAddress = InternalCalls.RhpGetThunkStubsBlockAddress(nextAvailableThunkPtr) + thunkIndex * Constants.ThunkCodeSize; + IntPtr thunkAddress = RuntimeImports.RhpGetThunkStubsBlockAddress(nextAvailableThunkPtr) + thunkIndex * Constants.ThunkCodeSize; return SetThumbBit(thunkAddress); } @@ -278,7 +278,7 @@ private static IntPtr TryGetThunkDataAddress(IntPtr thunkAddress) int thunkIndex = (int)((thunkAddressValue - currentThunksBlockAddress) / (nuint)Constants.ThunkCodeSize); // Compute the address of the data block that corresponds to the current thunk - IntPtr thunkDataBlockAddress = InternalCalls.RhpGetThunkDataBlockAddress((IntPtr)((nint)thunkAddressValue)); + IntPtr thunkDataBlockAddress = RuntimeImports.RhpGetThunkDataBlockAddress((IntPtr)((nint)thunkAddressValue)); return thunkDataBlockAddress + thunkIndex * Constants.ThunkDataSize; } @@ -353,7 +353,7 @@ public static unsafe IntPtr GetNewThunksBlock() } else { - nextThunksBlock = InternalCalls.RhAllocateThunksMapping(); + nextThunksBlock = RuntimeImports.RhAllocateThunksMapping(); if (nextThunksBlock == IntPtr.Zero) { @@ -370,7 +370,7 @@ public static unsafe IntPtr GetNewThunksBlock() // Each mapping consists of multiple blocks of thunk stubs/data pairs. Keep track of those // so that we do not create a new mapping until all blocks in the sections we just mapped are consumed IntPtr currentThunksBlock = nextThunksBlock; - int thunkBlockSize = InternalCalls.RhpGetThunkBlockSize(); + int thunkBlockSize = RuntimeImports.RhpGetThunkBlockSize(); for (int i = 0; i < Constants.NumThunkBlocksPerMapping; i++) { s_currentlyMappedThunkBlocks[i] = currentThunksBlock; @@ -383,7 +383,7 @@ public static unsafe IntPtr GetNewThunksBlock() // Setup the thunks in the new block as a linked list of thunks. // Use the first data field of the thunk to build the linked list. - IntPtr dataAddress = InternalCalls.RhpGetThunkDataBlockAddress(nextThunksBlock); + IntPtr dataAddress = RuntimeImports.RhpGetThunkDataBlockAddress(nextThunksBlock); for (int i = 0; i < Constants.NumThunksPerBlock; i++) { diff --git a/src/coreclr/nativeaot/Test.CoreLib/src/System/Threading/Monitor.cs b/src/coreclr/nativeaot/Test.CoreLib/src/System/Threading/Monitor.cs deleted file mode 100644 index d631d3c9348386..00000000000000 --- a/src/coreclr/nativeaot/Test.CoreLib/src/System/Threading/Monitor.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// Trivial implementation of Monitor lock for singlethreaded environment -using System.Runtime; - -namespace System.Threading -{ - public static partial class Monitor - { - private static readonly IntPtr s_thread = InternalCalls.RhCurrentNativeThreadId(); - - public static void Enter(object obj) - { - if (s_thread != InternalCalls.RhCurrentNativeThreadId()) - throw new InvalidOperationException(); - } - - public static void Enter(object obj, ref bool lockTaken) - { - if (lockTaken) - throw new InvalidOperationException(); - - Enter(obj); - lockTaken = true; - } - - public static void Exit(object obj) - { - if (s_thread != InternalCalls.RhCurrentNativeThreadId()) - throw new InvalidOperationException(); - } - } -} diff --git a/src/coreclr/nativeaot/Test.CoreLib/src/Test.CoreLib.csproj b/src/coreclr/nativeaot/Test.CoreLib/src/Test.CoreLib.csproj index 6ccb98e36f8979..338e4c162d5571 100644 --- a/src/coreclr/nativeaot/Test.CoreLib/src/Test.CoreLib.csproj +++ b/src/coreclr/nativeaot/Test.CoreLib/src/Test.CoreLib.csproj @@ -59,9 +59,6 @@ Runtime.Base\src\System\Runtime\StackFrameIterator.cs - - Runtime.Base\src\System\Runtime\ThunkPool.cs - Runtime.Base\src\System\Runtime\TypeCast.cs @@ -233,7 +230,6 @@ -