Module batch_semaphore

Source
Expand description

ยงImplementation Details.

The semaphore is implemented using an intrusive linked list of waiters. An atomic counter tracks the number of available permits. If the semaphore does not contain the required number of permits, the task attempting to acquire permits places its waker at the end of a queue. When new permits are made available (such as by releasing an initial acquisition), they are assigned to the task at the front of the queue, waking that task if its requested number of permits is met.

Because waiters are enqueued at the back of the linked list and dequeued from the front, the semaphore is fair. Tasks trying to acquire large numbers of permits at a time will always be woken eventually, even if many other tasks are acquiring smaller numbers of permits. This means that in a use-case like tokioโ€™s read-write lock, writers will not be starved by readers.

Structsยง

Acquire ๐Ÿ”’
AcquireError
Error returned from the Semaphore::acquire function.
Semaphore ๐Ÿ”’
An asynchronous counting semaphore which permits waiting on multiple permits at once.
Waiter ๐Ÿ”’
An entry in the wait queue.
Waitlist ๐Ÿ”’

Enumsยง

TryAcquireError
Error returned from the Semaphore::try_acquire function.