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

Expose Semaphore::MAX_PERMITS and document it #5144

Merged
merged 1 commit into from Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tokio-util/src/sync/poll_semaphore.rs
Expand Up @@ -95,7 +95,7 @@ impl PollSemaphore {

/// Adds `n` new permits to the semaphore.
///
/// The maximum number of permits is `usize::MAX >> 3`, and this function
/// The maximum number of permits is [`Semaphore::MAX_PERMITS`], and this function
/// will panic if the limit is exceeded.
///
/// This is equivalent to the [`Semaphore::add_permits`] method on the
Expand Down
9 changes: 8 additions & 1 deletion tokio/src/sync/semaphore.rs
Expand Up @@ -123,7 +123,14 @@ fn bounds() {
}

impl Semaphore {
/// The maximum number of permits which a semaphore can hold. It is `usize::MAX >>> 3`.
///
/// Exceeding this limit typically results in a panic.
pub const MAX_PERMITS: usize = super::batch_semaphore::Semaphore::MAX_PERMITS;

/// Creates a new semaphore with the initial number of permits.
///
/// Panics if `permits` exceeds [`Semaphore::MAX_PERMITS`].
#[track_caller]
pub fn new(permits: usize) -> Self {
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(all(tokio_unstable, feature = "tracing"))]
Expand Down Expand Up @@ -186,7 +193,7 @@ impl Semaphore {

/// Adds `n` new permits to the semaphore.
///
/// The maximum number of permits is `usize::MAX >> 3`, and this function will panic if the limit is exceeded.
/// The maximum number of permits is [`Semaphore::MAX_PERMITS`], and this function will panic if the limit is exceeded.
Comment on lines -189 to +196
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to keep the note that the value is usize::MAX >> 3 somewhere in the docs, though probably not here.

Copy link
Contributor Author

@vi vi Oct 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does't specific number visible in the docs (it looks like this: pub const MAX_PERMITS: usize = 2_305_843_009_213_693_951usize) clarify this?

Or do you mean the documentation should state that this number is fixed and is not expected to change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I mean that the docs should mention that 2_305_843_009_213_693_951usize is equal to usize::MAX >> 3.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added sentence about that.

Also searched for other usize::MAX >> 3 in the docs and replaced with a link to MAX_PERMITS.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing it would make sense to point out is that 2_305_843_009_213_693_951usize is only the correct number on 64-bit platforms.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usize::MAX >> 3 implies different value on different platforms.

pub fn add_permits(&self, n: usize) {
self.ll_sem.release(n);
}
Expand Down
31 changes: 27 additions & 4 deletions tokio/tests/sync_semaphore.rs
Expand Up @@ -114,14 +114,37 @@ async fn stress_test() {
#[test]
fn add_max_amount_permits() {
let s = tokio::sync::Semaphore::new(0);
s.add_permits(usize::MAX >> 3);
assert_eq!(s.available_permits(), usize::MAX >> 3);
s.add_permits(tokio::sync::Semaphore::MAX_PERMITS);
assert_eq!(s.available_permits(), tokio::sync::Semaphore::MAX_PERMITS);
}

#[cfg(not(tokio_wasm))] // wasm currently doesn't support unwinding
#[test]
#[should_panic]
fn add_more_than_max_amount_permits() {
fn add_more_than_max_amount_permits1() {
let s = tokio::sync::Semaphore::new(1);
s.add_permits(usize::MAX >> 3);
s.add_permits(tokio::sync::Semaphore::MAX_PERMITS);
}

#[cfg(not(tokio_wasm))] // wasm currently doesn't support unwinding
#[test]
#[should_panic]
fn add_more_than_max_amount_permits2() {
let s = Semaphore::new(Semaphore::MAX_PERMITS - 1);
s.add_permits(1);
s.add_permits(1);
}

#[cfg(not(tokio_wasm))] // wasm currently doesn't support unwinding
#[test]
#[should_panic]
fn panic_when_exceeds_maxpermits() {
let _ = Semaphore::new(Semaphore::MAX_PERMITS + 1);
}

#[test]
fn no_panic_at_maxpermits() {
let _ = Semaphore::new(Semaphore::MAX_PERMITS);
let s = Semaphore::new(Semaphore::MAX_PERMITS - 1);
s.add_permits(1);
}