Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maitake: add an async semaphore #299

Closed
hawkw opened this issue Aug 26, 2022 · 0 comments · Fixed by #301
Closed

maitake: add an async semaphore #299

hawkw opened this issue Aug 26, 2022 · 0 comments · Fixed by #301
Assignees
Labels
crate/maitake Related to the `maitake` crate kind/enhancement New feature or request

Comments

@hawkw
Copy link
Owner

hawkw commented Aug 26, 2022

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.

@hawkw hawkw added kind/enhancement New feature or request crate/maitake Related to the `maitake` crate labels Aug 26, 2022
@hawkw hawkw self-assigned this Aug 26, 2022
@hawkw hawkw closed this as completed in 8a46328 Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate/maitake Related to the `maitake` crate kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant