Skip to content

Commit

Permalink
Add non-waited semaphore check (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 authored Apr 7, 2021
1 parent 975caac commit c8ad77b
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2152,14 +2152,22 @@ public ConcurrentQueueSemaphore(int initialCount)

public Task WaitAsync(CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
_queue.Enqueue(tcs);
_semaphore.WaitAsync().ContinueWith(
continuationAction: s_continuePop,
state: _queue,
cancellationToken: cancellationToken
);
return tcs.Task;
// try sync wait with 0 which will not block to see if we need to do an async wait
if (_semaphore.Wait(0, cancellationToken))
{
return Task.CompletedTask;
}
else
{
var tcs = new TaskCompletionSource<bool>();
_queue.Enqueue(tcs);
_semaphore.WaitAsync().ContinueWith(
continuationAction: s_continuePop,
state: _queue,
cancellationToken: cancellationToken
);
return tcs.Task;
}
}

public void Release()
Expand Down

0 comments on commit c8ad77b

Please sign in to comment.