Skip to content

Commit

Permalink
Fix spinwaiting in LockFreeReaderHashtable (dotnet#55082)
Browse files Browse the repository at this point in the history
Task.Delay(1).Wait() is variant of sync-over-async anti-patern. It does not work well with threadpool used to run parallel AOT compilation. It can lead to too many threadpool threads getting created and progress slowing down to crawl, at least temporarily.
  • Loading branch information
jkotas authored Jul 2, 2021
1 parent 96ef64d commit e8572d3
Showing 1 changed file with 4 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,14 @@ private TValue WaitForSentinelInHashtableToDisappear(TValue[] hashtable, int tab
if (sentinel == null)
return null;

TValue value = Volatile.Read(ref hashtable[tableIndex]);
var sw = new SpinWait();
while (true)
{
for (int i = 0; (i < 10000) && value == sentinel; i++)
{
value = Volatile.Read(ref hashtable[tableIndex]);
}
TValue value = Volatile.Read(ref hashtable[tableIndex]);
if (value != sentinel)
break;

Task.Delay(1).Wait();
return value;
sw.SpinOnce();
}

return value;
}

/// <summary>
Expand Down

0 comments on commit e8572d3

Please sign in to comment.