Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConcurrentQueueSegment allows spinning threads to sleep. #44265

Merged
merged 4 commits into from
Nov 5, 2020
Merged
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 @@ -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