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

Code before:
        {
            ThreadPoolWorkQueue workQueue = ThreadPool.s_workQueue;
            ThreadPoolWorkQueueThreadLocals tl = workQueue.GetOrCreateThreadLocals();

            if (s_assignableWorkItemQueueCount > 0)
            {
                workQueue.AssignWorkItemQueue(tl);
            }

            // Before dequeuing the first work item, acknowledge that the thread request has been satisfied
            workQueue._separated._hasOutstandingThreadRequest = 0;

            // The state change must happen before sweeping queues for items.
            Interlocked.MemoryBarrier();

            object? workItem = DequeueWithPriorityAlternation(workQueue, tl, out bool missedSteal);
            if (workItem == null)
            {
                if (s_assignableWorkItemQueueCount > 0)
                {
                    workQueue.UnassignWorkItemQueue(tl);
                }

                // Missing a steal means there may be an item that we were unable to get.
                // Effectively, we failed to fulfill our promise to check the queues after
                // clearing "Scheduled" flag.
                // We need to make sure someone will do another pass.
                if (missedSteal)
                {
                    workQueue.EnsureThreadRequested();
                }

                // Tell the VM we're returning normally, not because Hill Climbing asked us to return.
                return true;
            }

            // The workitems that are currently in the queues could have asked only for one worker.
            // We are going to process a workitem, which may take unknown time or even block.
            // In a worst case the current workitem will indirectly depend on progress of other
            // items and that would lead to a deadlock if no one else checks the queue.
            // We must ensure at least one more worker is coming if the queue is not empty.
            workQueue.EnsureThreadRequested();

            //
            // After this point, this method is no longer responsible for ensuring thread requests except for missed steals
            //

            // Has the desire for logging changed since the last time we entered?
            workQueue.RefreshLoggingEnabled();

            object? threadLocalCompletionCountObject = tl.threadLocalCompletionCountObject;

Review comment (bug):
The comment refers to clearing the "Scheduled" flag, but the new implementation uses `_hasOutstandingThreadRequest` instead (cleared on line 918). The comment should be updated to match the current implementation:

```csharp
// Missing a steal means there may be an item that we were unable to get.
// Effectively, we failed to fulfill our promise to check the queues after
// clearing the outstanding thread request flag.
// We need to make sure someone will do another pass.
```
```suggestion
                // clearing the outstanding thread request flag.
```

Code after:
            bool dispatchNormalPriorityWorkFirst = workQueue._dispatchNormalPriorityWorkFirst;
            if (dispatchNormalPriorityWorkFirst && !tl.workStealingQueue.CanSteal)
            {
                workQueue._dispatchNormalPriorityWorkFirst = !dispatchNormalPriorityWorkFirst;
                WorkQueue queue =
                    s_assignableWorkItemQueueCount > 0 ? tl.assignedGlobalWorkItemQueue : workQueue.workItems;
                if (!queue.TryDequeue(out workItem) && s_assignableWorkItemQueueCount > 0)
                {
                    workQueue.workItems.TryDequeue(out workItem);
                }
            }

            missedSteal = false;
            workItem ??= workQueue.Dequeue(tl, ref missedSteal);

            return workItem;
        }

        /// <summary>
        /// Dispatches work items to this thread.
        /// </summary>
        /// <returns>
        /// <c>true</c> if this thread did as much work as was available or its quantum expired.
        /// <c>false</c> if this thread stopped working early.
        /// </returns>
        internal static bool Dispatch()
        {
            ThreadPoolWorkQueue workQueue = ThreadPool.s_workQueue;
            ThreadPoolWorkQueueThreadLocals tl = workQueue.GetOrCreateThreadLocals();

            if (s_assignableWorkItemQueueCount > 0)
            {
                workQueue.AssignWorkItemQueue(tl);
            }

            // Before dequeuing the first work item, acknowledge that the thread request has been satisfied
            workQueue._separated._hasOutstandingThreadRequest = 0;

            // The state change must happen before sweeping queues for items.
            Interlocked.MemoryBarrier();

            object? workItem = DequeueWithPriorityAlternation(workQueue, tl, out bool missedSteal);
            if (workItem == null)
            {
                if (s_assignableWorkItemQueueCount > 0)
                {
                    workQueue.UnassignWorkItemQueue(tl);
                }

                // Missing a steal means there may be an item that we were unable to get.
                // Effectively, we failed to fulfill our promise to check the queues after