-
-
Notifications
You must be signed in to change notification settings - Fork 3k
util: add AtomicBool for lock-free is_cancelled check #7874
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
Open
HueCodes
wants to merge
4
commits into
tokio-rs:master
Choose a base branch
from
HueCodes:atomic-is-cancelled
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7827cfc
util: add AtomicBool for lock-free is_cancelled check
HueCodes cc3ce58
util: remove duplicated is_cancelled field from Inner
HueCodes 392b196
util: use mutex lock in WaitForCancellationFuture poll
HueCodes 9ab2acb
util: wrap Rust identifiers in backticks in doc comments
HueCodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hmm. Acquire is not actually strong enough here. When
WaitForCancellationFuture::poll()returns pending in parallel with being woken up, we currently rely on the following logic:WaitForCancellationFuture, we create aNotifiedfuture.WaitForCancellationFuture, we readfalsefromis_cancelled.CancellationToken::cancel(), we writetruetois_cancelled.CancellationToken::cancel(), we callnotify_waiters().And here it is really important that
notify_waiters()will wake up theNotifiedfuture created in step 1. Otherwise theWaitForCancellationFuturemight sleep forever. With a mutex, that's fine, but with the current atomics, it's not actually satisfied!That's because what we need is that step 2 happens-before step 3. However, for that to be true, operation 2 must be Release, and operation 3 must be Acquire. Unfortunately, it's reversed! Operation 2 is Acquire, and operation 3 is Release.
Unfortunately, a
load()can't be Release and astore()can't beAcquire, so we have to upgrade them to read-modify-write operations for this to work:AtomicBool::swap(true, AcqRel)to writetrue.AtomicBool::compare_exchange(false, false, AcqRel, Acquire)to read. (fetch_and(true, AcqRel)would also work)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.
Or we can just take the mutex in
WaitForCancellationFuture::poll(), and leave theAtomicBooloptimization for users ofis_cancelled()only.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 again for the review and explaining the memory order issue. I am learning a ton.
I've gone with your simpler suggestion and added a separate
is_cancelled_with_lock() function that takes the mutex for proper synchronization in
WaitForCancellationFuture::poll(), while keeping the lock-free AtomicBool path for direct
is_cancelled() calls.
I've also just pushed a fix for the cargo-spellcheck CI failures
Let me know if you need anything else.
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.
IMO using both AtomicBool and Mutex for the same purpose but for different use cases is not a good idea. It makes the reasoning harder - should I use the atomics based solution or the mutex one for use case 3 (a use case from the future) ?!
If it could be implemented fully with atomics (
swap+compare_exchange) it would be better!A good start would be to add an integration (or bench) test that reproduces the problem with Acquire+Release and then fixing it by using swap+compare_exchange.
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.
What do you mean by a use-case from the future? As #7775 indicates, there are users today that call
is_cancelled()instead of.awaiting a future, and such users would get the benefit today.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.
This is clear!
What bothers me is that some use cases could use
is_cancelled()(lock-free, for both external and internal users) and other use cases (internal user) likeWaitForCancellationFuture::poll()can't and they need to depend on the Mutex.With a "use case from the future" I mean the second - a new Tokio internal functionality that needs to know whether a token is cancelled or not. What should the developer use -
is_cancelled()oris_cancelled_with_lock()?! I guess the answer isit depends.But if by using swap+compare_exchange there won't be a need of
is_cancelled_with_lock()then the decision will be simple.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.
if there are any changes I should make let me know!