Skip to content

Commit e1fe97a

Browse files
authored
When initializing Lock in NativeAOT, get the processor count directly instead of using Environment (#101514)
It's a bit safer to get the proc count directly since the Environment properties involve class construction. We used to get the proc count directly before, and there's still a reason to do so to get the correct value with the current initialization pattern due to in-thread reentrancy of the same class construction.
1 parent f78c367 commit e1fe97a

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Lock.NativeAot.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics;
55
using System.Diagnostics.Tracing;
6+
using System.Runtime;
67
using System.Runtime.CompilerServices;
78

89
namespace System.Threading
@@ -194,8 +195,12 @@ private static bool TryInitializeStatics()
194195
{
195196
if (oldStage == StaticsInitializationStage.NotStarted)
196197
{
197-
// If the stage is PartiallyComplete, these will have already been initialized
198-
s_isSingleProcessor = Environment.IsSingleProcessor;
198+
// If the stage is PartiallyComplete, these will have already been initialized.
199+
//
200+
// Not using Environment.ProcessorCount here as it involves class construction, and if that property is
201+
// already being constructed earlier in the stack on the same thread, it would return the default value
202+
// here. Initialize s_isSingleProcessor first, as it may be used by other initialization afterwards.
203+
s_isSingleProcessor = RuntimeImports.RhGetProcessCpuCount() == 1;
199204
s_maxSpinCount = DetermineMaxSpinCount();
200205
s_minSpinCount = DetermineMinSpinCount();
201206
}

0 commit comments

Comments
 (0)