Skip to content

Commit 7d76a13

Browse files
committed
spinning tweaks
1 parent 9784e14 commit 7d76a13

4 files changed

Lines changed: 8 additions & 4 deletions

File tree

src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ public bool Wait(int timeoutMs, short activeThreadCount)
111111
return WaitSlow(timeoutMs, activeThreadCount);
112112
}
113113

114-
private bool WaitSlow(int timeoutMs, short activeThreadCount)
114+
private bool WaitSlow(int timeoutMs, short tpThreadCount)
115115
{
116116
// Now spin briefly with exponential backoff.
117117
// We estimate availability of CPU resources and limit spin count accordingly.
118118
// See comments on DefaultSemaphoreSpinCountLimit for more details.
119119
// Count current thread as active for the duration of spinning.
120-
int active = activeThreadCount + 1;
120+
int active = tpThreadCount - _separated._counts.WaiterCount;
121121
int available = _procCount - active;
122122
int spinStep = _maxSpinCount * 2 / _procCount;
123123
// With activeThreadCount arbitrarily large and _procCount arbitrarily small

src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.Blocking.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private enum PendingBlockingAdjustment : byte
260260
private static class BlockingConfig
261261
{
262262
public static readonly bool IsCooperativeBlockingEnabled =
263-
AppContextConfigHelper.GetBooleanConfig("System.Threading.ThreadPool.Blocking.CooperativeBlocking", true);
263+
AppContextConfigHelper.GetBooleanConfig("System.Threading.ThreadPool.Blocking.CooperativeBlocking", "DOTNET_ThreadPool_CooperativeBlocking", true);
264264
public static readonly bool IgnoreMemoryUsage =
265265
AppContextConfigHelper.GetBooleanConfig("System.Threading.ThreadPool.Blocking.IgnoreMemoryUsage", false);
266266

src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WorkerThread.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private static void WorkerThreadStart()
105105

106106
while (true)
107107
{
108-
while (semaphore.Wait(timeoutMs, threadPoolInstance._separated.counts.NumProcessingWork))
108+
while (semaphore.Wait(timeoutMs, threadPoolInstance._separated.counts.NumExistingThreads))
109109
{
110110
WorkerDoWork(threadPoolInstance);
111111
}

src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,11 @@ internal static bool Dispatch()
857857
// Missing a steal means there may be an item that we were unable to get.
858858
// Effectively, we failed to fulfill our promise to check the queues for work.
859859
// We need to make sure someone will do another pass.
860+
// We do not need to do that right away though as that might result in a bunch
861+
// of threads inviting each other while mostly seeing missed steals.
860862
if (missedSteal)
861863
{
864+
Thread.Sleep(1);
862865
ThreadPool.EnsureWorkerRequested();
863866
}
864867

@@ -918,6 +921,7 @@ internal static bool Dispatch()
918921
//
919922
if (missedSteal)
920923
{
924+
Thread.Sleep(1);
921925
ThreadPool.EnsureWorkerRequested();
922926
}
923927

0 commit comments

Comments
 (0)