Skip to content

Commit

Permalink
ConcurrentQueueSegment allows spinning threads to sleep. (#44265)
Browse files Browse the repository at this point in the history
* Allow threads to sleep when ConcurrentQueue has many enqueuers/dequeuers.

* Update src/libraries/System.Private.CoreLib/src/System/Collections/Concurrent/ConcurrentQueueSegment.cs

Co-authored-by: Stephen Toub <stoub@microsoft.com>

* Apply suggestions from code review

Co-authored-by: Stephen Toub <stoub@microsoft.com>

Co-authored-by: AMD DAYTONA EPYC <amd@amd-DAYTONA-X0.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
  • Loading branch information
3 people committed Nov 5, 2020
1 parent ae47aa2 commit ce4772d
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ internal sealed class ConcurrentQueueSegment<T>
internal ConcurrentQueueSegment<T>? _nextSegment; // SOS's ThreadPool command depends on this name
#pragma warning restore 0649

/// <summary>Threshold to spin before allowing threads to sleep when there is contention.</summary>
private const int Sleep1Threshold = 8;

/// <summary>Creates the segment.</summary>
/// <param name="boundedLength">
/// The maximum number of elements the segment can contain. Must be a power of 2.
Expand Down Expand Up @@ -183,7 +186,7 @@ public bool TryDequeue([MaybeNullWhen(false)] out T item)
}

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

Expand Down Expand Up @@ -244,7 +247,7 @@ public bool TryPeek([MaybeNullWhen(false)] out T result, bool resultUsed)
}

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

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

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

Expand Down

0 comments on commit ce4772d

Please sign in to comment.