hawkw/mycelium

maitake: add an async semaphore

hawkw opened this issue · 0 comments

hawkw commented

It would be nice to have an async semaphore implementation for maitake.

A semaphore is a useful synchronization type for things like rate-limiting async tasks. It could also be used as a lower-level primitive to implement things like read-write locks1 and channels2.

We can probably do something very similar to the implementation I wrote for Tokio in tokio-rs/tokio#2325, although it should be possible to simplify some code that was only necessary to maintain Tokio's API surface.

Footnotes

  1. a rwlock can be modeled by a semaphore with n permits (where n is the maximum number of concurrent readers); each reader must acquire a single permit, while a writer must acquire n permits).

  2. a bounded MPSC channel of capacity n can be implemented using a semaphore with n permits, where each producer must acquire a single permit to write, and every time a message is consumed, the reader releases a permit to the writers.