Skip to content

Commit d38beee

Browse files
committed
moved cooldown
1 parent 33ce64f commit d38beee

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ private bool WaitAsWaiter(int timeoutMs, bool allowFastWake)
202202

203203
if (!allowFastWake)
204204
{
205+
// The caller wants that the thread spends some time waiting as a matter of rate limiting
206+
// thus we will require a 4 usec cooldown before reintroducing the thread.
207+
// The sleep/wake transition typically takes care of the wait, but the blocker has fast
208+
// wake paths and the underlying OS API may have fast/trivial wake paths as well,
209+
// thus fast wakeups can happen and are hard to avoid completely.
210+
// So, if a fast wake happened when parking was desired, we hold up the thread a bit
211+
// before releasing.
212+
long cooldown = Stopwatch.Frequency * 4 / 1000000;
205213
while (Stopwatch.GetTimestamp() - blockingStart < cooldown)
206214
{
207215
Thread.SpinWait(1);

0 commit comments

Comments
 (0)