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

Add a has_changed function to watch::Receiver #4342

Merged
merged 2 commits into from
Dec 31, 2021

Conversation

elichai
Copy link
Contributor

@elichai elichai commented Dec 24, 2021

Motivation

I'm using sync::watch to tell a bunch of workers to do some work, and once in a while they check to see what's the latest job, and if there's no new job they should keep doing what they're already doing, and currently the only way to do that is via rex.changed().now_or_never().is_some() which has some Waker overhead (see asm in https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=8fdf2f1bf2b6b16827b106dc13ef5895)

Solution

Add a has_changed method to check if the value had changed.

Alternatives

  1. This method doesn't change the version that the receiver currently holds, this is the only function in the API that observes the version without changing it, should it maybe also change it? (that comes at the cost of being a &mut self function).
  2. Implement a try_changed function that doesn't block if the value hasn't changed.

@github-actions github-actions bot added the R-loom Run loom tests on this PR label Dec 24, 2021
tokio/src/sync/watch.rs Outdated Show resolved Hide resolved
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Dec 30, 2021
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.

@Darksonn Darksonn merged commit 12dd063 into tokio-rs:master Dec 31, 2021
@elichai elichai deleted the watch-hash-changed branch January 2, 2022 08:41
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 Run loom tests on this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants