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

feat(maitake): add wait::Semaphore #301

Merged
merged 14 commits into from
Aug 30, 2022
Merged

feat(maitake): add wait::Semaphore #301

merged 14 commits into from
Aug 30, 2022

Conversation

hawkw
Copy link
Owner

@hawkw hawkw commented Aug 29, 2022

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.

This branch adds an asynchronous semaphore implementation in
maitake::wait. The implementation is based on the implementation I
wrote for Tokio in tokio-rs/tokio#2325, with some code simplified a bit
as it was not necessary to maintain Tokio's required API surface.

Closes #299

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
Copy link
Owner Author

hawkw commented Aug 29, 2022

This is a draft primarily because I'd like to add docs.

@hawkw hawkw changed the title [WIP] feat(maitake): add wait::Semaphore feat(maitake): add wait::Semaphore Aug 29, 2022
@hawkw hawkw marked this pull request as ready for review August 29, 2022 17:15
@hawkw hawkw requested a review from jamesmunns August 29, 2022 17:15
@hawkw
Copy link
Owner Author

hawkw commented Aug 29, 2022

@jamesmunns any opinions as to whether this belongs in maitake::wait or maitake::sync? Or, should we just combine those modules?

@hawkw hawkw merged commit 8a46328 into main Aug 30, 2022
@hawkw hawkw deleted the eliza/semaphore branch August 30, 2022 16:39
hawkw added a commit that referenced this pull request Aug 30, 2022
currently, `maitake` has a `wait` module, which contains `WaitCell`,
`WaitQueue`, `WaitMap`, and (as of #301) `Semaphore`. in addition, it
has a separate `sync` module, which currently contains `Mutex` and
nothing else (and may grow a `RwLock` eventually/soon). this feels a bit
unfortunate --- the `wait` types are also synchronization primitives.

this branch moves all of `maitake::wait` into `maitake::sync`. in
addition,i fixed up the docs a little bit.

closes #302
hawkw added a commit that referenced this pull request Sep 1, 2022
This branch adds an async read-write lock implementation in
`maitake::sync`. This lock is implemented using the `Semaphore` type
added in #301. The rwlock is modeled using a `Semaphore` with
`Semaphore::MAX_PERMITS` permits in it; a reader must acquire a single
permit to gain read access, while a writer must acquire all the permits.
hawkw added a commit that referenced this pull request Sep 1, 2022
This branch adds an async read-write lock implementation in
`maitake::sync`. This lock is implemented using the `Semaphore` type
added in #301. The rwlock is modeled using a `Semaphore` with
`Semaphore::MAX_PERMITS` permits in it; a reader must acquire a single
permit to gain read access, while a writer must acquire all the permits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

maitake: add an async semaphore
2 participants