Repository: dotnet/runtime
File: src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs (C#)

Code before:

            // If the change below is seen by another thread, ensure that the enqueued work item will also be visible
            Volatile.Write(ref _mayHaveHighPriorityWorkItems, true);

            EnsureThreadRequested();
        }

        internal static void TransferAllLocalWorkItemsToHighPriorityGlobalQueue()
        {
            // If there's no local queue, there's nothing to transfer.
            if (ThreadPoolWorkQueueThreadLocals.threadLocals is not ThreadPoolWorkQueueThreadLocals tl)
            {
                return;
            }

            // Pop each work item off the local queue and push it onto the global. This is a
            // bounded loop as no other thread is allowed to push into this thread's queue.
            ThreadPoolWorkQueue queue = ThreadPool.s_workQueue;
            while (tl.workStealingQueue.LocalPop() is object workItem)
            {
                queue.highPriorityWorkItems.Enqueue(workItem);
            }

            Volatile.Write(ref queue._mayHaveHighPriorityWorkItems, true);

            queue.EnsureThreadRequested();
        }

        internal static bool LocalFindAndPop(object callback)
        {
            ThreadPoolWorkQueueThreadLocals? tl = ThreadPoolWorkQueueThreadLocals.threadLocals;
            return tl != null && tl.workStealingQueue.LocalFindAndPop(callback);
        }

        public object? Dequeue(ThreadPoolWorkQueueThreadLocals tl, ref bool missedSteal)
        {
            // Check for local work items
            object? workItem = tl.workStealingQueue.LocalPop();
            if (workItem != null)
            {
                return workItem;
            }

            // Check for high-priority work items
            if (tl.isProcessingHighPriorityWorkItems)
            {
                if (highPriorityWorkItems.TryDequeue(out workItem))
                {
                    return workItem;
                }


Review comment (suggestion):
The code unconditionally writes `true` to `_mayHaveHighPriorityWorkItems` and requests a thread even when no work items were transferred (i.e., the while loop at line 725 didn't execute). This could lead to unnecessary thread requests.

Consider only setting the flag and requesting a thread if at least one work item was actually transferred:
```csharp
bool addedItems = false;
while (tl.workStealingQueue.LocalPop() is object workItem)
{
    queue.highPriorityWorkItems.Enqueue(workItem);
    addedItems = true;
}

if (addedItems)
{
    Volatile.Write(ref queue._mayHaveHighPriorityWorkItems, true);
    queue.EnsureThreadRequested();
}
```

Code after:
            // If there's no local queue, there's nothing to transfer.
            if (ThreadPoolWorkQueueThreadLocals.threadLocals is not ThreadPoolWorkQueueThreadLocals tl)
            {
                return;
            }

            // Pop each work item off the local queue and push it onto the global. This is a
            // bounded loop as no other thread is allowed to push into this thread's queue.
            ThreadPoolWorkQueue queue = ThreadPool.s_workQueue;
            bool addedHighPriorityWorkItem = false;
            bool ensureThreadRequest = false;
            while (tl.workStealingQueue.LocalPop() is object workItem)
            {
                // If there's an unexpected exception here that happens to get handled, the lost work item, or missing thread
                // request, etc., may lead to other issues. A fail-fast or try-finally here could reduce the effect of such
                // uncommon issues to various degrees, but it's also uncommon to check for unexpected exceptions.
                try
                {
                    queue.highPriorityWorkItems.Enqueue(workItem);
                    addedHighPriorityWorkItem = true;
                }
                catch (OutOfMemoryException)
                {
                    // This is not expected to throw under normal circumstances
                    tl.workStealingQueue.LocalPush(workItem);

                    // A work item had been removed temporarily and other threads may have missed stealing it, so ensure that
                    // there will be a thread request
                    ensureThreadRequest = true;
                    break;
                }
            }

            if (addedHighPriorityWorkItem)
            {
                Volatile.Write(ref queue._mayHaveHighPriorityWorkItems, true);
                ensureThreadRequest = true;
            }

            if (ensureThreadRequest)
            {
                queue.EnsureThreadRequested();
            }
        }

        internal static bool LocalFindAndPop(object callback)
        {
            ThreadPoolWorkQueueThreadLocals? tl = ThreadPoolWorkQueueThreadLocals.threadLocals;
            return tl != null && tl.workStealingQueue.LocalFindAndPop(callback);
        }