Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Don't Sleep(1) in some spin-wait loops (dotnet/coreclr#21722)
Browse files Browse the repository at this point in the history
- 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.

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
  • Loading branch information
kouvel authored and jkotas committed Dec 31, 2018
1 parent 289f0dc commit 0bc5b34
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -311,7 +311,7 @@ public bool TryEnqueue(T item)
}

// Lost a race. Spin a bit, then try again.
spinner.SpinOnce();
spinner.SpinOnce(sleep1Threshold: -1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -720,7 +720,7 @@ private void UpdateStateAtomically(int newBits, int updateBitsMask)
return;
}

sw.SpinOnce();
sw.SpinOnce(sleep1Threshold: -1);
} while (true);
}

Expand Down
10 changes: 0 additions & 10 deletions src/System.Private.CoreLib/shared/System/Threading/SpinWait.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,6 @@ public struct SpinWait
/// </remarks>
internal static readonly int SpinCountforSpinBeforeWait = PlatformHelper.IsSingleProcessor ? 1 : 35;

/// <summary>
/// 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).
/// </summary>
/// <remarks>
/// Should be greater than <see cref="SpinCountforSpinBeforeWait"/> so that Sleep(1) would not be used by default.
/// </remarks>
internal const int Sleep1ThresholdForLongSpinBeforeWait = 40;

// The number of times we've spun already.
private int _count;

Expand Down

0 comments on commit 0bc5b34

Please sign in to comment.