Remove idiv from ThreadPoolWorkQueue:Dequeue loop (#6781)
authorBen Adams <thundercat@illyriad.co.uk>
Thu, 25 Aug 2016 05:08:53 +0000 (06:08 +0100)
committerJan Kotas <jkotas@microsoft.com>
Thu, 25 Aug 2016 05:08:53 +0000 (22:08 -0700)
src/mscorlib/src/System/Threading/ThreadPool.cs

index 2ee7b76..09fe93c 100644 (file)
@@ -685,11 +685,13 @@ namespace System.Threading
             if (null == callback)
             {
                 WorkStealingQueue[] otherQueues = allThreadQueues.Current;
-                int i = tl.random.Next(otherQueues.Length);
                 int c = otherQueues.Length;
+                int maxIndex = c - 1;
+                int i = tl.random.Next(c);
                 while (c > 0)
                 {
-                    WorkStealingQueue otherQueue = Volatile.Read(ref otherQueues[i % otherQueues.Length]);
+                    i = (i < maxIndex) ? i + 1 : 0;
+                    WorkStealingQueue otherQueue = Volatile.Read(ref otherQueues[i]);
                     if (otherQueue != null &&
                         otherQueue != wsq &&
                         otherQueue.TrySteal(out callback, ref missedSteal))
@@ -697,7 +699,6 @@ namespace System.Threading
                         Contract.Assert(null != callback);
                         break;
                     }
-                    i++;
                     c--;
                 }
             }