From e111f12bdff9dd217f61b93ab8a3ece50f641f22 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Wed, 12 Jul 2023 11:34:43 -0700 Subject: [PATCH 1/5] add trivial monitor lock to Test.CorLib --- .../src/System/Threading/Monitor.cs | 34 +++++++++++++++++++ .../Test.CoreLib/src/Test.CoreLib.csproj | 1 + 2 files changed, 35 insertions(+) create mode 100644 src/coreclr/nativeaot/Test.CoreLib/src/System/Threading/Monitor.cs diff --git a/src/coreclr/nativeaot/Test.CoreLib/src/System/Threading/Monitor.cs b/src/coreclr/nativeaot/Test.CoreLib/src/System/Threading/Monitor.cs new file mode 100644 index 0000000000000..d631d3c934838 --- /dev/null +++ b/src/coreclr/nativeaot/Test.CoreLib/src/System/Threading/Monitor.cs @@ -0,0 +1,34 @@ +// 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 f4c83b553a366..6ccb98e36f897 100644 --- a/src/coreclr/nativeaot/Test.CoreLib/src/Test.CoreLib.csproj +++ b/src/coreclr/nativeaot/Test.CoreLib/src/Test.CoreLib.csproj @@ -233,6 +233,7 @@ + From 6527835edfa670fe40f3c623188c7b05393ec01f Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Wed, 12 Jul 2023 11:42:19 -0700 Subject: [PATCH 2/5] Use regular lock in ThunksHeap --- .../src/System/Runtime/InternalCalls.cs | 4 ++ .../src/System/Runtime/ThunkPool.cs | 53 +++++++++---------- 2 files changed, 29 insertions(+), 28 deletions(-) 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 3c9d6c86ffc32..e43406e904d0d 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs @@ -285,6 +285,10 @@ internal static extern unsafe IntPtr RhpCallPropagateExceptionCallback( [MethodImpl(MethodImplOptions.InternalCall)] internal static extern Exception RhpGetThreadAbortException(); + [RuntimeImport(Redhawk.BaseName, "RhCurrentNativeThreadId")] + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern unsafe IntPtr RhCurrentNativeThreadId(); + //------------------------------------------------------------------------------------------------------------ // PInvoke-based internal calls // diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ThunkPool.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ThunkPool.cs index d8f9d7c31cb59..36b9f060c0d4d 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ThunkPool.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ThunkPool.cs @@ -86,11 +86,11 @@ private unsafe ThunksHeap(IntPtr commonStubAddress) _allocatedBlocks = new AllocatedBlock(); - InternalCalls.RhpAcquireThunkPoolLock(); - - IntPtr thunkStubsBlock = ThunkBlocks.GetNewThunksBlock(); - - InternalCalls.RhpReleaseThunkPoolLock(); + IntPtr thunkStubsBlock; + lock (this) + { + thunkStubsBlock = ThunkBlocks.GetNewThunksBlock(); + } if (thunkStubsBlock != IntPtr.Zero) { @@ -183,28 +183,26 @@ public unsafe IntPtr AllocateThunk() Debug.Assert(_nextAvailableThunkPtr != IntPtr.Zero); - InternalCalls.RhpAcquireThunkPoolLock(); - - IntPtr nextAvailableThunkPtr = _nextAvailableThunkPtr; - IntPtr nextNextAvailableThunkPtr = *((IntPtr*)(nextAvailableThunkPtr)); - - if (nextNextAvailableThunkPtr == IntPtr.Zero) + IntPtr nextAvailableThunkPtr; + lock (this) { - if (!ExpandHeap()) + nextAvailableThunkPtr = _nextAvailableThunkPtr; + IntPtr nextNextAvailableThunkPtr = *((IntPtr*)(nextAvailableThunkPtr)); + + if (nextNextAvailableThunkPtr == IntPtr.Zero) { - InternalCalls.RhpReleaseThunkPoolLock(); - return IntPtr.Zero; + if (!ExpandHeap()) + { + return IntPtr.Zero; + } + + nextAvailableThunkPtr = _nextAvailableThunkPtr; + nextNextAvailableThunkPtr = *((IntPtr*)(nextAvailableThunkPtr)); + Debug.Assert(nextNextAvailableThunkPtr != IntPtr.Zero); } - nextAvailableThunkPtr = _nextAvailableThunkPtr; - nextNextAvailableThunkPtr = *((IntPtr*)(nextAvailableThunkPtr)); - Debug.Assert(nextNextAvailableThunkPtr != IntPtr.Zero); + _nextAvailableThunkPtr = nextNextAvailableThunkPtr; } - - _nextAvailableThunkPtr = nextNextAvailableThunkPtr; - - InternalCalls.RhpReleaseThunkPoolLock(); - Debug.Assert(nextAvailableThunkPtr != IntPtr.Zero); #if DEBUG @@ -238,12 +236,11 @@ public unsafe void FreeThunk(IntPtr thunkAddress) *((IntPtr*)(dataAddress + IntPtr.Size)) = new IntPtr(-1); #endif - InternalCalls.RhpAcquireThunkPoolLock(); - - *((IntPtr*)(dataAddress)) = _nextAvailableThunkPtr; - _nextAvailableThunkPtr = dataAddress; - - InternalCalls.RhpReleaseThunkPoolLock(); + lock (this) + { + *((IntPtr*)(dataAddress)) = _nextAvailableThunkPtr; + _nextAvailableThunkPtr = dataAddress; + } } private bool IsThunkInHeap(IntPtr thunkAddress) From cbaeb2c3fc051f8bcea182f6ca50c037baa685a9 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Wed, 12 Jul 2023 11:53:20 -0700 Subject: [PATCH 3/5] remove CrstThunkPool --- .../Runtime.Base/src/System/Runtime/InternalCalls.cs | 8 -------- src/coreclr/nativeaot/Runtime/Crst.h | 1 - src/coreclr/nativeaot/Runtime/MiscHelpers.cpp | 12 ------------ src/coreclr/nativeaot/Runtime/startup.cpp | 5 ----- 4 files changed, 26 deletions(-) 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 e43406e904d0d..05494c84d0a1b 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InternalCalls.cs @@ -314,14 +314,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 void RhpAcquireThunkPoolLock(); - - [DllImport(Redhawk.BaseName)] - [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })] - internal static extern void RhpReleaseThunkPoolLock(); - [DllImport(Redhawk.BaseName)] [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })] internal static extern IntPtr RhAllocateThunksMapping(); diff --git a/src/coreclr/nativeaot/Runtime/Crst.h b/src/coreclr/nativeaot/Runtime/Crst.h index 31e43008da150..143205c34df26 100644 --- a/src/coreclr/nativeaot/Runtime/Crst.h +++ b/src/coreclr/nativeaot/Runtime/Crst.h @@ -21,7 +21,6 @@ enum CrstType CrstObjectiveCMarshalCallouts, CrstGcStressControl, CrstThreadStore, - CrstThunkPool, CrstYieldProcessorNormalized, CrstEventPipe, CrstEventPipeConfig, diff --git a/src/coreclr/nativeaot/Runtime/MiscHelpers.cpp b/src/coreclr/nativeaot/Runtime/MiscHelpers.cpp index 6df37cf23b9d3..1fb99cd776229 100644 --- a/src/coreclr/nativeaot/Runtime/MiscHelpers.cpp +++ b/src/coreclr/nativeaot/Runtime/MiscHelpers.cpp @@ -340,18 +340,6 @@ COOP_PINVOKE_HELPER(uint8_t *, RhGetCodeTarget, (uint8_t * pCodeOrg)) return pCodeOrg; } -extern CrstStatic g_ThunkPoolLock; - -EXTERN_C NATIVEAOT_API void __cdecl RhpAcquireThunkPoolLock() -{ - g_ThunkPoolLock.Enter(); -} - -EXTERN_C NATIVEAOT_API void __cdecl RhpReleaseThunkPoolLock() -{ - g_ThunkPoolLock.Leave(); -} - EXTERN_C NATIVEAOT_API uint64_t __cdecl RhpGetTickCount64() { return PalGetTickCount64(); diff --git a/src/coreclr/nativeaot/Runtime/startup.cpp b/src/coreclr/nativeaot/Runtime/startup.cpp index 32cbab53cb830..9a6fd2f9c22bb 100644 --- a/src/coreclr/nativeaot/Runtime/startup.cpp +++ b/src/coreclr/nativeaot/Runtime/startup.cpp @@ -49,8 +49,6 @@ static bool DetectCPUFeatures(); extern RhConfig * g_pRhConfig; -CrstStatic g_ThunkPoolLock; - #if defined(HOST_X86) || defined(HOST_AMD64) || defined(HOST_ARM64) // This field is inspected from the generated code to determine what intrinsics are available. EXTERN_C int g_cpuFeatures; @@ -170,9 +168,6 @@ static bool InitDLL(HANDLE hPalInstance) return false; #endif - if (!g_ThunkPoolLock.InitNoThrow(CrstType::CrstThunkPool)) - return false; - return true; } 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 4/5] 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 05494c84d0a1b..01475cf9addef 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 359c091936358..1fa421ec3e245 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 3aaf11fd0ef33..43b23737f503e 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 5b97cc9d8464c..a6f249eed8dd9 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 6c6d682cda5d6..236ef9ebfe379 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 36b9f060c0d4d..0ef42bc0bebcb 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 d631d3c934838..0000000000000 --- 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 6ccb98e36f897..338e4c162d557 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 @@ - From 39dddcea87ba1326b2b4d08b5f0624c46516b6c8 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Sat, 26 Aug 2023 08:55:04 -0700 Subject: [PATCH 5/5] Remove unused CrstObjectiveCMarshalCallouts --- src/coreclr/nativeaot/Runtime/Crst.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/coreclr/nativeaot/Runtime/Crst.h b/src/coreclr/nativeaot/Runtime/Crst.h index 143205c34df26..b4112a174b274 100644 --- a/src/coreclr/nativeaot/Runtime/Crst.h +++ b/src/coreclr/nativeaot/Runtime/Crst.h @@ -18,7 +18,6 @@ enum CrstType CrstInterfaceDispatchGlobalLists, CrstStressLog, CrstRestrictedCallouts, - CrstObjectiveCMarshalCallouts, CrstGcStressControl, CrstThreadStore, CrstYieldProcessorNormalized,