From 6018ad01235e670e4420186f797c8eef45f4495e Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Thu, 6 May 2021 16:39:12 -0700 Subject: [PATCH] Fix for hill climbing not working sometimes (#52397) Updated the time intervals used in the hill climbing check to be unsigned to prevent wrap-around. This also prevents the check from failing when the current time is negative since the prior and next times are initialized to zero, and matches the previous implementation. Fixes https://github.com/dotnet/runtime/issues/51935 --- .../src/System/Threading/PortableThreadPool.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.cs index 303a268..b18e963 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.cs @@ -327,10 +327,13 @@ namespace System.Threading private bool ShouldAdjustMaxWorkersActive(int currentTimeMs) { - // We need to subtract by prior time because Environment.TickCount can wrap around, making a comparison of absolute times unreliable. + // We need to subtract by prior time because Environment.TickCount can wrap around, making a comparison of absolute + // times unreliable. Intervals are unsigned to avoid wrapping around on the subtract after enough time elapses, and + // this also prevents the initial elapsed interval from being negative due to the prior and next times being + // initialized to zero. int priorTime = Volatile.Read(ref _separated.priorCompletedWorkRequestsTime); - int requiredInterval = _separated.nextCompletedWorkRequestsTime - priorTime; - int elapsedInterval = currentTimeMs - priorTime; + uint requiredInterval = (uint)(_separated.nextCompletedWorkRequestsTime - priorTime); + uint elapsedInterval = (uint)(currentTimeMs - priorTime); if (elapsedInterval >= requiredInterval) { // Avoid trying to adjust the thread count goal if there are already more threads than the thread count goal. -- 2.7.4