Skip to content

Commit ce4772d

Browse files
alexcovingtonAMD DAYTONA EPYCstephentoub
authored
ConcurrentQueueSegment allows spinning threads to sleep. (#44265)
* 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>
1 parent ae47aa2 commit ce4772d

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ internal sealed class ConcurrentQueueSegment<T>
3737
internal ConcurrentQueueSegment<T>? _nextSegment; // SOS's ThreadPool command depends on this name
3838
#pragma warning restore 0649
3939

40+
/// <summary>Threshold to spin before allowing threads to sleep when there is contention.</summary>
41+
private const int Sleep1Threshold = 8;
42+
4043
/// <summary>Creates the segment.</summary>
4144
/// <param name="boundedLength">
4245
/// The maximum number of elements the segment can contain. Must be a power of 2.
@@ -183,7 +186,7 @@ public bool TryDequeue([MaybeNullWhen(false)] out T item)
183186
}
184187

185188
// Lost a race. Spin a bit, then try again.
186-
spinner.SpinOnce(sleep1Threshold: -1);
189+
spinner.SpinOnce(Sleep1Threshold);
187190
}
188191
}
189192

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

246249
// Lost a race. Spin a bit, then try again.
247-
spinner.SpinOnce(sleep1Threshold: -1);
250+
spinner.SpinOnce(Sleep1Threshold);
248251
}
249252
}
250253

@@ -301,7 +304,7 @@ public bool TryEnqueue(T item)
301304
}
302305

303306
// Lost a race. Spin a bit, then try again.
304-
spinner.SpinOnce(sleep1Threshold: -1);
307+
spinner.SpinOnce(Sleep1Threshold);
305308
}
306309
}
307310

0 commit comments

Comments
 (0)