Skip to content

Commit

Permalink
Moved ThanksPool to corlib
Browse files Browse the repository at this point in the history
  • Loading branch information
VSadov committed Aug 26, 2023
1 parent cbaeb2c commit d437066
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,30 +257,6 @@ internal static int RhEndNoGCRegion()
[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();
Expand Down Expand Up @@ -314,10 +290,6 @@ internal static int RhEndNoGCRegion()
[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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,38 +819,38 @@ 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;
}

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;
}

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<object?[], object?> invoker)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
<Compile Include="System\Runtime\ExceptionIDs.cs" />
<Compile Include="System\Runtime\GCSettings.NativeAot.cs" />
<Compile Include="System\Runtime\TypeLoaderExports.cs" />
<Compile Include="System\Runtime\ThunkPool.cs" />
<Compile Include="System\Runtime\InitializeFinalizerThread.cs" />
<Compile Include="System\Runtime\InteropServices\ComEventsHelper.NativeAot.cs" Condition="'$(FeatureCominterop)' == 'true'" />
<Compile Include="System\Runtime\InteropServices\ComWrappers.NativeAot.cs" Condition="'$(FeatureComWrappers)' == 'true'" />
Expand Down Expand Up @@ -538,9 +539,6 @@
<Compile Include="$(RuntimeBasePath)System\Runtime\StackFrameIterator.cs">
<Link>Runtime.Base\src\System\Runtime\StackFrameIterator.cs</Link>
</Compile>
<Compile Include="$(RuntimeBasePath)System\Runtime\ThunkPool.cs">
<Link>Runtime.Base\src\System\Runtime\ThunkPool.cs</Link>
</Compile>
<Compile Include="$(RuntimeBasePath)System\Runtime\TypeCast.cs">
<Link>Runtime.Base\src\System\Runtime\TypeCast.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -353,7 +353,7 @@ public static unsafe IntPtr GetNewThunksBlock()
}
else
{
nextThunksBlock = InternalCalls.RhAllocateThunksMapping();
nextThunksBlock = RuntimeImports.RhAllocateThunksMapping();

if (nextThunksBlock == IntPtr.Zero)
{
Expand All @@ -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;
Expand All @@ -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++)
{
Expand Down
34 changes: 0 additions & 34 deletions src/coreclr/nativeaot/Test.CoreLib/src/System/Threading/Monitor.cs

This file was deleted.

4 changes: 0 additions & 4 deletions src/coreclr/nativeaot/Test.CoreLib/src/Test.CoreLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@
<Compile Include="..\..\Runtime.Base\src\System\Runtime\StackFrameIterator.cs">
<Link>Runtime.Base\src\System\Runtime\StackFrameIterator.cs</Link>
</Compile>
<Compile Include="..\..\Runtime.Base\src\System\Runtime\ThunkPool.cs">
<Link>Runtime.Base\src\System\Runtime\ThunkPool.cs</Link>
</Compile>
<Compile Include="..\..\Runtime.Base\src\System\Runtime\TypeCast.cs">
<Link>Runtime.Base\src\System\Runtime\TypeCast.cs</Link>
</Compile>
Expand Down Expand Up @@ -233,7 +230,6 @@
<Compile Include="System\Runtime\RuntimeHelpers.cs" />
<Compile Include="System\Runtime\InitializeFinalizerThread.cs" />
<Compile Include="System\Threading\Interlocked.cs" />
<Compile Include="System\Threading\Monitor.cs" />
<Compile Include="System\Array.cs" />
<Compile Include="System\RuntimeExceptionHelpers.cs" />
<Compile Include="System\Object.cs" />
Expand Down

0 comments on commit d437066

Please sign in to comment.