Delay Random allocation in ConcurrentStack
authorStephen Toub <stoub@microsoft.com>
Sat, 18 Feb 2017 23:32:20 +0000 (15:32 -0800)
committerStephen Toub <stoub@microsoft.com>
Sat, 18 Feb 2017 23:32:20 +0000 (15:32 -0800)
Commit migrated from https://github.com/dotnet/coreclr/commit/f81185ff4cc3c06224f49e696257c2e4f52e4c35

src/coreclr/src/mscorlib/src/System/Collections/Concurrent/ConcurrentStack.cs

index 10a5201..3cc100e 100644 (file)
@@ -328,7 +328,7 @@ namespace System.Collections.Concurrent
             Node head;
             Node next;
             int backoff = 1;
-            Random r = new Random(Environment.TickCount & Int32.MaxValue); // avoid the case where TickCount could return Int32.MinValue
+            Random r = null;
             while (true)
             {
                 head = m_head;
@@ -359,7 +359,18 @@ namespace System.Collections.Concurrent
                     spin.SpinOnce();
                 }
 
-                backoff = spin.NextSpinWillYield ? r.Next(1, BACKOFF_MAX_YIELDS) : backoff * 2;
+                if (spin.NextSpinWillYield)
+                {
+                    if (r == null)
+                    {
+                        r = new Random();
+                    }
+                    backoff = r.Next(1, BACKOFF_MAX_YIELDS);
+                }
+                else
+                {
+                    backoff *= 2;
+                }
             }
         }