This is the async
-ready equivalent of Semaphore, similar to Stephen Toub's AsyncSempahore. Alternatively, you can use the SemaphoreSlim class, which is async
-ready on modern platforms.
An AsyncSemaphore
keeps a count, which is the number of open "slots" it has available to satisfy waiters. Any thread may increase the number of slots available by calling Release
.
The task returned from WaitAsync
will enter the Completed
state when the AsyncSemaphore
has given it a slot. That same task will enter the Canceled
state if the CancellationToken
is signaled before the wait is satisfied; in that case, the AsyncSemaphore
does not lose a slot.
You can call WaitAsync
with an already-cancelled CancellationToken
to attempt to acquire a slot from the AsyncSemaphore
immediately without actually entering the wait queue.