-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Refactor sync::batch_semaphore::Semaphore::poll_acquire
#7046
base: master
Are you sure you want to change the base?
Conversation
30ebf9e
to
4c24022
Compare
This commit tries to make the logic in `poll_acquire` flow more naturally, while also using more descriptive names and improving commentary (including the addition of a missing "SAFETY:" annotation). There should be no functional changes.
4c24022
to
74df9e7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. Overall looks reasonable, but I have one request below:
tokio/src/sync/batch_semaphore.rs
Outdated
return Poll::Ready(Err(AcquireError::closed())); | ||
} | ||
|
||
acquired >>= Self::PERMIT_SHIFT; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it to be confusing when the meaning of this is changed like this. Previously, it's the number of permits shifted, and after it's the actual number of permits. Can we use a different variable name prior to this that makes it clear that it's not just the number of permits?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, but gosh is it hard to find a good name. I renamed to acquired_shifted
before the shift, but now I feel I should append _shifted
to a bunch of other variables, for consistency. But then it just looks awkward...
f72570b
to
b4e45b0
Compare
Motivation
I found the implementation of the low-level semaphore a bit hard to read compared to other code (possibly because it has been untouched for some years). It could potentially benefit from small rewrites, better documentation, and the like.
Solution
Well, I thought it would be faster to attempt a PR for an initial change, rather than opening an issue. But I have no expectations that it will be merged. :)
I reworked the
poll_acquire
method, these are the major changes:This probably needs a thorough review to make sure that I haven't messed up anything (or more likely, figure out what I have). If this kind of refactoring is accepted in core modules, I'm also happy to follow up with similar ones.