Skip to content

Conversation

@hawkw
Copy link
Member

@hawkw hawkw commented Apr 7, 2021

Motivation

The tokio::sync::Semaphore type provides a
Semaphore::available_permits method which returns the current
number of permits available on the semaphore.
tokio_util::sync::PollSemaphore does not expose such a method. It
is possible to use PollSemaphore::into_inner or
PollSemaphore::clone_inner to unwrap the inner semaphore, but this may
require cloning/dropping the semaphore's Arc which shouldn't be
necessary to access the permits.

Solution

This commit adds PollSemaphore::available_permits. It also adds
PollSemaphore::add_permits while we're here.

Closes #3682

## Motivation

The `tokio::sync::Semaphore` type provides a
[`Semaphore::available_permits` method][1] which returns the current
number of permits available on the semaphore.
`tokio_util::sync::PollSemaphore` [does not expose such a method][2]. It
is possible to use `PollSemaphore::into_inner` or
`PollSemaphore::clone_inner` to unwrap the inner semaphore, but this may
require cloning/dropping the semaphore's `Arc` which shouldn't be
necessary to access the permits.

## Solution

This commit adds `PollSemaphore::available_permits`. It also adds
`PollSemaphore::add_permits` while we're here.

[1]: https://docs.rs/tokio/1.4.0/tokio/sync/struct.Semaphore.html#method.available_permits
[2]: https://docs.rs/tokio-util/0.6.5/tokio_util/sync/struct.PollSemaphore.html#implementations
@hawkw hawkw requested a review from Darksonn April 7, 2021 17:16
@Darksonn
Copy link
Contributor

Darksonn commented Apr 7, 2021

Do you want to add AsRef<Semaphore> too?

@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate M-sync Module: tokio/sync labels Apr 7, 2021
@hawkw
Copy link
Member Author

hawkw commented Apr 7, 2021

Do you want to add AsRef<Semaphore> too?

Can do, I wasn't sure whether or not we wanted to expose all the API surface of the Semaphore type or not?

@hawkw
Copy link
Member Author

hawkw commented Apr 7, 2021

FreeBSD build failure looks unrelated?

@Darksonn
Copy link
Contributor

Darksonn commented Apr 7, 2021

I am fine with exposing access to the Semaphore in this case.

@hawkw hawkw merged commit bf8c77b into master Apr 7, 2021
@hawkw hawkw deleted the eliza/poll-semaphore-permit-ops branch April 7, 2021 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio-util Area: The tokio-util crate M-sync Module: tokio/sync

Projects

None yet

Development

Successfully merging this pull request may close these issues.

util: PollSemaphore should expose available_permits

3 participants