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

Refactor sync::batch_semaphore::Semaphore::poll_acquire #7046

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cip999
Copy link
Contributor

@cip999 cip999 commented Dec 18, 2024

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:

  • Restructured the control flow so that there is less logic inside the loop (specifically, only kept what really matters for the CAS subtraction).
  • Renamed a bunch of variables.
  • More commentary, and tried to clarify some details that weren't obvious to me.

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.

@github-actions github-actions bot added the R-loom-sync Run loom sync tests on this PR label Dec 18, 2024
@hawkw hawkw self-requested a review December 18, 2024 23:27
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.
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Jan 6, 2025
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.

Thanks for the PR. Overall looks reasonable, but I have one request below:

return Poll::Ready(Err(AcquireError::closed()));
}

acquired >>= Self::PERMIT_SHIFT;
Copy link
Contributor

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?

Copy link
Contributor Author

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...

@cip999 cip999 requested a review from Darksonn January 10, 2025 16:45
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 M-sync Module: tokio/sync R-loom-sync Run loom sync tests on this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants