From 784658e191f6de416d12c3c5d0c0d5e8bcd22547 Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Fri, 28 Dec 2018 13:02:30 -0800 Subject: [PATCH] Don't Sleep(1) in some spin-wait loops - In spin-wait loops that are not expected to last too long, Sleep(1) significantly delays threads from completing the operation - From eliminating Sleep(1) in such spin-wait loops, there can be a tradeoff, better fairness vs worse throughput, because Sleep(1) removes threads from contention and in some cases fewer threads can make faster progress at the cost of delaying the Sleep(1) threads relatively significantly. Eliminating the Sleep(1) in such spin-wait loops seems to be a good tradeoff. --- .../Collections/Concurrent/ConcurrentQueueSegment.cs | 6 +++--- .../shared/System/Threading/ManualResetEventSlim.cs | 4 ++-- .../shared/System/Threading/SpinWait.cs | 10 ---------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueueSegment.cs b/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueueSegment.cs index 85224a537a5f..c724285c7ed8 100644 --- a/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueueSegment.cs +++ b/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueueSegment.cs @@ -193,7 +193,7 @@ public bool TryDequeue(out T item) } // Lost a race. Spin a bit, then try again. - spinner.SpinOnce(); + spinner.SpinOnce(sleep1Threshold: -1); } } @@ -254,7 +254,7 @@ public bool TryPeek(out T result, bool resultUsed) } // Lost a race. Spin a bit, then try again. - spinner.SpinOnce(); + spinner.SpinOnce(sleep1Threshold: -1); } } @@ -311,7 +311,7 @@ public bool TryEnqueue(T item) } // Lost a race. Spin a bit, then try again. - spinner.SpinOnce(); + spinner.SpinOnce(sleep1Threshold: -1); } } diff --git a/src/System.Private.CoreLib/shared/System/Threading/ManualResetEventSlim.cs b/src/System.Private.CoreLib/shared/System/Threading/ManualResetEventSlim.cs index dab6bd9a4f0a..dd3de8305883 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/ManualResetEventSlim.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/ManualResetEventSlim.cs @@ -551,7 +551,7 @@ public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken) var spinner = new SpinWait(); while (spinner.Count < spinCount) { - spinner.SpinOnce(SpinWait.Sleep1ThresholdForLongSpinBeforeWait); + spinner.SpinOnce(sleep1Threshold: -1); if (IsSet) { @@ -720,7 +720,7 @@ private void UpdateStateAtomically(int newBits, int updateBitsMask) return; } - sw.SpinOnce(); + sw.SpinOnce(sleep1Threshold: -1); } while (true); } diff --git a/src/System.Private.CoreLib/shared/System/Threading/SpinWait.cs b/src/System.Private.CoreLib/shared/System/Threading/SpinWait.cs index ad252535480a..01e71d1f0029 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/SpinWait.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/SpinWait.cs @@ -89,16 +89,6 @@ public struct SpinWait /// internal static readonly int SpinCountforSpinBeforeWait = PlatformHelper.IsSingleProcessor ? 1 : 35; - /// - /// Typically, Sleep(1) should not be issued for a spin-wait before a proper wait because it is usually more beneficial - /// to just issue the proper wait. For longer spin-waits (when the spin count is configurable), this value may be used as - /// a threshold for issuing Sleep(1). - /// - /// - /// Should be greater than so that Sleep(1) would not be used by default. - /// - internal const int Sleep1ThresholdForLongSpinBeforeWait = 40; - // The number of times we've spun already. private int _count;