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: Implement Clone for watch::Sender #6388

Merged
merged 5 commits into from Mar 12, 2024

Conversation

mox692
Copy link
Member

@mox692 mox692 commented Mar 10, 2024

Motivation

This PR attempt to resolve #4809.

Currently, watch channel has only one sender but this enable it to be mpmc by adding a clone of the sender.

Solution

As with the reciever, the ref_count_tx field is added to the shared structure to record the number of senders; this counter is incremented by the sender's clone method and decremented by the drop method.

@github-actions github-actions bot added the R-loom-sync Run loom sync tests on this PR label Mar 10, 2024
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Mar 10, 2024
tokio/src/sync/watch.rs Outdated Show resolved Hide resolved
@mox692 mox692 marked this pull request as draft March 10, 2024 16:47
@mox692 mox692 marked this pull request as ready for review March 11, 2024 12:08
@@ -146,6 +146,16 @@ pub struct Sender<T> {
shared: Arc<Shared<T>>,
}

impl<T> Clone for Sender<T> {
fn clone(&self) -> Self {
self.shared.ref_count_tx.fetch_add(1, AcqRel);
Copy link
Contributor

Choose a reason for hiding this comment

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

When it comes to incrementing refcounts (but not decrementing), relaxed is generally enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

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 a3d2548 into tokio-rs:master Mar 12, 2024
75 checks passed
@mox692 mox692 deleted the add_sender_clone branch March 12, 2024 11:12
@mox692 mox692 mentioned this pull request Apr 22, 2024
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.

Implement Clone for watch::Sender to make it multi-producer
2 participants