Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Don't Sleep(1) in some spin-wait loops #21722

Merged
merged 1 commit into from
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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