Module tokio::sync::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

Enums