Skip to content

Commit 047a59c

Browse files
committed
use s_contentionCount as additional source of randomness
1 parent eec1c81 commit 047a59c

File tree

1 file changed

+5
-5
lines changed
  • src/libraries/System.Private.CoreLib/src/System/Threading

1 file changed

+5
-5
lines changed

src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,22 +345,22 @@ private void ExitImpl()
345345

346346
// we use this to de-synchronize threads if interlocked operations fail
347347
// we will pick a random number in exponentially expanding range and spin that many times
348-
private unsafe void CollisionBackoff(int collisions)
348+
private static unsafe void CollisionBackoff(int collisions)
349349
{
350350
Debug.Assert(collisions > 0);
351351

352-
// no need for much randomness here, we will just hash the stack address + _wakeWatchDog.
353-
uint rand = ((uint)&collisions + (uint)_wakeWatchDog) * 2654435769u;
352+
// no need for much randomness here, we will just hash the stack address + s_contentionCount.
353+
uint rand = ((uint)&collisions + (uint)s_contentionCount) * 2654435769u;
354354
uint spins = rand >> (byte)(32 - Math.Min(collisions, MaxExponentialBackoffBits));
355355
Thread.SpinWait((int)spins);
356356
}
357357

358358
// same idea as in CollisionBackoff, but with guaranteed minimum wait
359-
private unsafe void IterationBackoff(int iteration)
359+
private static unsafe void IterationBackoff(int iteration)
360360
{
361361
Debug.Assert(iteration > 0 && iteration < MaxExponentialBackoffBits);
362362

363-
uint rand = ((uint)&iteration + (uint)_wakeWatchDog) * 2654435769u;
363+
uint rand = ((uint)&iteration + (uint)s_contentionCount) * 2654435769u;
364364
// set the highmost bit to ensure minimum number of spins is exponentialy increasing
365365
// it basically gurantees that we spin at least 1, 2, 4, 8, 16, times, and so on
366366
rand |= (1u << 31);

0 commit comments

Comments
 (0)