Content-Length: 345970 | pFad | http://redirect.github.com/tokio-rs/tokio/pull/7100

5D sync: Added `WeakSender` to `sync::broadcast::channel` by tglane · Pull Request #7100 · tokio-rs/tokio · GitHub
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: Added WeakSender to sync::broadcast::channel #7100

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

Conversation

tglane
Copy link
Contributor

@tglane tglane commented Jan 14, 2025

Motivation

Closing issue #7003. Add a type WeakSender to the sync::broadcast::channel similar to sync::maps::channel.

Solution

The new WeakSender type just stores an Arc<Shared<T>> just like the normal Sender but active WeakSenders will not prevent the channel from being closed if all Senders are dropped.

Closes #7003.

@github-actions github-actions bot added the R-loom-sync Run loom sync tests on this PR label Jan 14, 2025
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Jan 27, 2025
tokio/src/sync/broadcast.rs Show resolved Hide resolved
Comment on lines +1365 to +1367
pub fn is_closed(&self) -> bool {
// Channel is closed when there are no strong senders left active
self.shared.num_tx.load(Acquire) == 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Man, the orderings in this file are a mess. Mixing SeqCst and non-SeqCst orderings on the same atomic is not a good idea. The correct orderings for counters is:

  • Increments happen with relaxed.
  • Decrements happen with acqrel.
  • Checks for zero happen with acquire.

We shouldn't need SeqCst for num_tx ever.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm sorry for that. There orderings and atomic operations are new for me so I copied it from sync/mpsc and tried to understand why the specific orderings where used there.
Thank you for some explanation on what should be used instead! I will change the code accordingly.

Copy link
Contributor Author

@tglane tglane Jan 27, 2025

Choose a reason for hiding this comment

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

Re-reading your comment I noticed that you said that we don't need SeqCst for num_tx ever. That was not part of my PR but should I adjust it now here in the PR too?

Edit.: I adjusted the orderings for num_tx too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, it's not your fault. This problem is pre-existing.

tokio/tests/sync_broadcast.rs Show resolved Hide resolved
tokio/src/sync/broadcast.rs Outdated Show resolved Hide resolved
* Changed memory orderings of `num_weak_tx`: Increments happen with relaxed, decrements happen with acqrel and checks for zero happen with acquire.
* Add asserts for `sync::broadcast::WeakSender` to
  `tests/async_send_sync.rs`
//redirect.github.com/ Tries to convert a `WeakSender` into a [`Sender`]. This will return `Some`
//redirect.github.com/ if there are other `Sender` instances alive and the channel wasn't
//redirect.github.com/ previously dropped, otherwise `None` is returned.
pub fn upgrade(&self) -> Option<Sender<T>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be #[must_use] too.

Comment on lines +1079 to +1081
//redirect.github.com/ Tries to convert a `WeakSender` into a [`Sender`]. This will return `Some`
//redirect.github.com/ if there are other `Sender` instances alive and the channel wasn't
//redirect.github.com/ previously dropped, otherwise `None` is returned.
Copy link
Contributor

Choose a reason for hiding this comment

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

Markdown docs generally start with one really short summary, followed by more text.

Suggested change
//redirect.github.com/ Tries to convert a `WeakSender` into a [`Sender`]. This will return `Some`
//redirect.github.com/ if there are other `Sender` instances alive and the channel wasn't
//redirect.github.com/ previously dropped, otherwise `None` is returned.
//redirect.github.com/ Tries to convert a `WeakSender` into a [`Sender`].
//redirect.github.com/
//redirect.github.com/ This will return `Some`
//redirect.github.com/ if there are other `Sender` instances alive and the channel wasn't
//redirect.github.com/ previously dropped, otherwise `None` is returned.

+ reflow to line length

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.

Add WeakSender for tokio::sync::broadcast
2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://redirect.github.com/tokio-rs/tokio/pull/7100

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy