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

sync: add_permits does not panic with usize::MAX >> 3 #3188

Merged
merged 3 commits into from Dec 2, 2020

Conversation

blasrodri
Copy link
Contributor

@blasrodri blasrodri commented Nov 28, 2020

Fixed incorrect shift in the assignment of permits on batch_semaphore.rs

Fixes: #3095

Fixed incorrect shift in the assignment of permits on batch_semaphore.rs

Fixes: tokio-rs#3095
@Darksonn Darksonn added A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-sync Module: tokio/sync labels Nov 28, 2020
@Darksonn
Copy link
Contributor

Can someone who knows the implementation of Semaphore confirm whether this line is also correct?

let prev = self.permits.fetch_add(rem << Self::PERMIT_SHIFT, Release);

It's just outside the area I can select with the diff tool.

@Darksonn Darksonn requested a review from hawkw November 28, 2020 21:36
@blasrodri
Copy link
Contributor Author

Can someone who knows the implementation of Semaphore confirm whether this line is also correct?

let prev = self.permits.fetch_add(rem << Self::PERMIT_SHIFT, Release);

It's just outside the area I can select with the diff tool.

My guess is that that line is correct.
self.permits are always shifted while stored in the struct. But when manipulated, the shift is reverted. That's why, when they are stored again (in that line you mentioned), the shift occurs.

Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

This looks correct to me!

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

This also seems correct to me now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Max permits for Semaphore::add_permits does not match documentation
3 participants