Simplify Dequeue method
authorStephen Toub <stoub@microsoft.com>
Wed, 1 Feb 2017 15:49:27 +0000 (10:49 -0500)
committerStephen Toub <stoub@microsoft.com>
Wed, 1 Feb 2017 15:49:27 +0000 (10:49 -0500)
- Consolidate branches
- Pass resulting work item as a return value rather than an out
- Avoid an unnecessary write to the bool passed by-ref

Commit migrated from https://github.com/dotnet/coreclr/commit/ff1caae983fd65b825c44a331e39a37ffb5b1710

src/coreclr/src/mscorlib/src/System/Threading/ThreadPool.cs

index fb47d82..0645ce5 100644 (file)
@@ -476,31 +476,18 @@ namespace System.Threading
         internal bool LocalFindAndPop(IThreadPoolWorkItem callback)
         {
             ThreadPoolWorkQueueThreadLocals tl = ThreadPoolWorkQueueThreadLocals.threadLocals;
-            if (null == tl)
-                return false;
-
-            return tl.workStealingQueue.LocalFindAndPop(callback);
+            return tl != null && tl.workStealingQueue.LocalFindAndPop(callback);
         }
 
-        public void Dequeue(ThreadPoolWorkQueueThreadLocals tl, out IThreadPoolWorkItem callback, out bool missedSteal)
+        public IThreadPoolWorkItem Dequeue(ThreadPoolWorkQueueThreadLocals tl, ref bool missedSteal)
         {
-            callback = null;
-            missedSteal = false;
             WorkStealingQueue wsq = tl.workStealingQueue;
+            IThreadPoolWorkItem callback;
 
-            if (wsq.LocalPop(out callback))
-                Debug.Assert(null != callback);
-
-            if (null == callback)
-            {
-                if (workItems.TryDequeue(out callback))
-                {
-                    Debug.Assert(null != callback);
-                }
-            }
-
-            if (null == callback)
+            if (!wsq.LocalPop(out callback) && // first try the local queue
+                !workItems.TryDequeue(out callback)) // then try the global queue
             {
+                // finally try to steal from another thread's local queue
                 WorkStealingQueue[] otherQueues = allThreadQueues.Current;
                 int c = otherQueues.Length;
                 int maxIndex = c - 1;
@@ -520,6 +507,8 @@ namespace System.Threading
                     c--;
                 }
             }
+
+            return callback;
         }
 
         static internal bool Dispatch()
@@ -570,7 +559,7 @@ namespace System.Threading
                     finally
                     {
                         bool missedSteal = false;
-                        workQueue.Dequeue(tl, out workItem, out missedSteal);
+                        workItem = workQueue.Dequeue(tl, ref missedSteal);
 
                         if (workItem == null)
                         {