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):
Lines 721-723 are executed unconditionally even when no work items are transferred (i.e., when the while loop at line 716 doesn't execute). This means `_mayHaveHighPriorityWorkItems` could be set to true when no high-priority items exist, and a thread could be requested unnecessarily.

Consider guarding these operations:
```csharp
bool hasItems = false;
while (tl.workStealingQueue.LocalPop() is object workItem)
{
    queue.highPriorityWorkItems.Enqueue(workItem);
    hasItems = true;
}

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

Code after:

            // 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;
            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();