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