Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Merge pull request #16295 from stephentoub/cs_random
Browse files Browse the repository at this point in the history
Delay Random allocation in ConcurrentStack
  • Loading branch information
stephentoub authored Feb 19, 2017
2 parents 6d3e8dd + c0a0c55 commit c97abfa
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ private int TryPopCore(int count, out Node poppedHead)
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 = _head;
Expand Down Expand Up @@ -694,7 +694,18 @@ private int TryPopCore(int count, out Node poppedHead)
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;
}
}
}
#pragma warning restore 0420
Expand Down

0 comments on commit c97abfa

Please sign in to comment.