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

Commit

Permalink
In a rare case when local queue has multiple segments, do a local Deq…
Browse files Browse the repository at this point in the history
…ueue to ensure that continuously nonempty global queue does not delay retirement of old segments.

This is not a very common scenario, generally happening only at start up, but mitigation is also simple.
  • Loading branch information
VSadov committed Sep 4, 2019
1 parent 24c326f commit c214ac5
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/System.Private.CoreLib/shared/System/Threading/ThreadPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,18 @@ internal bool TryRemove(object callback)

public object? DequeueAny(ref bool missedSteal, LocalQueue localQueue)
{
// We come here after Pop failed.
// We look at the global queue and then do a sweep through all local queues for any work remaining.
//
// However, in a rare case when local queue has multiple segments, do a local Dequeue first
// to ensure that continuously nonempty global queue does not delay retirement of old segments.
if (localQueue._enqSegment != localQueue._deqSegment)
{
object? locallyDequeued = localQueue.Dequeue(ref missedSteal);
if (locallyDequeued != null)
return locallyDequeued;
}

object? callback = _globalQueue.Dequeue(localQueue);
if (callback == null)
{
Expand Down

0 comments on commit c214ac5

Please sign in to comment.