diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/ThunkPool.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/ThunkPool.cs index 429513459ce250..29767cdcf9d00d 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/ThunkPool.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/ThunkPool.cs @@ -36,6 +36,7 @@ using System.Diagnostics; using System.Numerics; +using System.Threading; namespace System.Runtime { @@ -293,9 +294,12 @@ internal static class ThunkBlocks { private static IntPtr[] s_currentlyMappedThunkBlocks = new IntPtr[Constants.NumThunkBlocksPerMapping]; private static int s_currentlyMappedThunkBlocksIndex = Constants.NumThunkBlocksPerMapping; + private static Lock s_lock = new Lock(useTrivialWaits: true); public static unsafe IntPtr GetNewThunksBlock() { + using Lock.Scope scope = s_lock.EnterScope(); + IntPtr nextThunksBlock; // Check the most recently mapped thunks block. Each mapping consists of multiple