From 8a569cfde41c0fdcf8c9013bdb30197c0fb19bd8 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 6 Jun 2019 01:36:16 +0100 Subject: [PATCH] Less work for OptimalMaxSpinWaitsPerSpinIteration fast-path (#21702) --- .../src/System/Threading/Thread.CoreCLR.cs | 24 ++++++++++++---------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs b/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs index 9b3118a..f849358 100644 --- a/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs +++ b/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs @@ -468,20 +468,22 @@ namespace System.Threading { get { - if (s_optimalMaxSpinWaitsPerSpinIteration != 0) - { - return s_optimalMaxSpinWaitsPerSpinIteration; - } - - // This is done lazily because the first call to the function below in the process triggers a measurement that - // takes a nontrivial amount of time if the measurement has not already been done in the backgorund. - // See Thread::InitializeYieldProcessorNormalized(), which describes and calculates this value. - s_optimalMaxSpinWaitsPerSpinIteration = GetOptimalMaxSpinWaitsPerSpinIterationInternal(); - Debug.Assert(s_optimalMaxSpinWaitsPerSpinIteration > 0); - return s_optimalMaxSpinWaitsPerSpinIteration; + int optimalMaxSpinWaitsPerSpinIteration = s_optimalMaxSpinWaitsPerSpinIteration; + return optimalMaxSpinWaitsPerSpinIteration != 0 ? optimalMaxSpinWaitsPerSpinIteration : CalculateOptimalMaxSpinWaitsPerSpinIteration(); } } + [MethodImpl(MethodImplOptions.NoInlining)] + private static int CalculateOptimalMaxSpinWaitsPerSpinIteration() + { + // This is done lazily because the first call to the function below in the process triggers a measurement that + // takes a nontrivial amount of time if the measurement has not already been done in the backgorund. + // See Thread::InitializeYieldProcessorNormalized(), which describes and calculates this value. + s_optimalMaxSpinWaitsPerSpinIteration = GetOptimalMaxSpinWaitsPerSpinIterationInternal(); + Debug.Assert(s_optimalMaxSpinWaitsPerSpinIteration > 0); + return s_optimalMaxSpinWaitsPerSpinIteration; + } + [MethodImpl(MethodImplOptions.InternalCall)] private static extern int GetCurrentProcessorNumber(); -- 2.7.4