Skip to content

Commit

Permalink
Remove some unnecessary spinning (dotnet/coreclr#21437)
Browse files Browse the repository at this point in the history
Most of the use of SpinWait in CoreLib involves waiting for some short-lived operation to complete on another thread, in which case the spinning thread should backoff as it's unable to make forward progress until the other operation completes.  In a few cases, however, SpinWait is being used just around CompareExchange operations, such that at least one thread running this code path is guaranteed to make forward progress, and the backoff in the spinning doesn't actually help (in theory it could help to reduce contention if lots of threads were all trying to CompareExchange concurrently, but in such cases you'd actually want more randomized backoff, as otherwise it's likely all the threads would re-attempt at around the same time and similarly re-encounter contention).

Signed-off-by: dotnet-bot <[email protected]>
  • Loading branch information
stephentoub authored and dotnet-bot committed Dec 10, 2018
1 parent 54aa0d7 commit c8fce13
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ internal static int RoundUpToPowerOf2(int i)
_frozenForEnqueues = true;

// Increase the tail by FreezeOffset, spinning until we're successful in doing so.
var spinner = new SpinWait();
int tail = _headAndTail.Tail;
while (true)
{
int tail = Volatile.Read(ref _headAndTail.Tail);
if (Interlocked.CompareExchange(ref _headAndTail.Tail, tail + FreezeOffset, tail) == tail)
int oldTail = Interlocked.CompareExchange(ref _headAndTail.Tail, tail + FreezeOffset, tail);
if (oldTail == tail)
{
break;
}
spinner.SpinOnce();
tail = oldTail;
}
}
}
Expand Down

0 comments on commit c8fce13

Please sign in to comment.