7

Implement `Sync` for `mpsc::Sender` by ibraheemdev · Pull Request #111087 · rust...

 1 year ago
source link: https://github.com/rust-lang/rust/pull/111087
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Conversation

Contributor

mpsc::Sender is currently !Sync because the previous implementation contained an optimization where the channel started out as single-producer and was dynamically upgraded on the first clone, which relied on a unique reference to the sender. This optimization is one of the main reasons the old implementation was so complex and was removed in #93563. mpsc::Sender can now soundly implement Sync.

Note for any potential confusion, this chance does not add MPMC behavior. This only affects the already Send + Clone sender, not receiver.

It's technically possible to rely on the !Sync behavior in the same way as a PhantomData<*mut T>, but that seems very unlikely in practice. Either way, this change is insta-stable and needs an FCP.

@rustbot label +T-libs-api -T-libs

goffrie, taiki-e, and tjallingt reacted with thumbs up emoji

Collaborator

r? @joshtriplett

(rustbot has picked a reviewer for you, use r? to override)

rustbot

added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

labels

May 2, 2023

Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

rustbot

added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label

May 2, 2023

Contributor

Author

@rustbot label -T-libs

rustbot

removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label

May 2, 2023

This comment has been minimized.

Member

This PR makes mpsc::Sender implement Sync.

@rfcbot fcp merge

rfcbot

commented

Jun 8, 2023

edited by m-ou-se

Team member @BurntSushi has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

rfcbot

added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it.

labels

Jun 8, 2023

Member

Are we certain that the original reason for a !Sync impl was because of an internal optimization? Can we go back and find where the original API was sketched out and see if there were any other reasons for the !Sync impl?

@rfcbot concern why-not-sync

Member

Right now there are some failing tests. We should check to make sure those themselves don't raise any other concerns.

@rfcbot concern failing-tests

Contributor

Are we certain that the original reason for a !Sync impl was because of an internal optimization? Can we go back and find where the original API was sketched out and see if there were any other reasons for the !Sync impl?

I dug up #11111 - which made senders (and other channel types) !Freeze (because Sync hadn't been invented yet). The only comment was:

#[no_freeze] // technically this implementation is shareable, but it shouldn't
             // be required to be shareable in an arc
pub struct SharedChan<T> {

I doubt that the decision was considered in-depth - my guess is that it was done just to keep options open for future implementation changes. But maybe @alexcrichton remembers ;)

Contributor

Ah, I found #42397 as well which made SyncSender Sync; there's some interesting discussion in there.

Member

@goffrie Thanks for digging up #42397. That gives me a lot of confidence that these types weren't Sync primarily due to implementation details. So I can at least resolve that concern.

@rfcbot resolve why-not-sync

Member

the previous implementation contained an optimization where the channel started out as single-producer and was dynamically upgraded on the first clone, which relied on a unique reference to the sender. This optimization is one of the main reasons the old implementation was so complex and was removed in #75194.

This isn't the right PR. Is it #93563?

Contributor

Author

@dtolnay yeah that's the one, not sure how #75194 ended up in there.

Contributor

Author

I guess the big question is why implement Sync if it's already Send and Clone, and the only real reason is ergonomics. For example, it makes patterns with thread::scope possible without extra cloning clutter:

let (tx, rx) = std::sync::mpsc::channel();
 
 // std::thread::scope(|s| {
//     let tx1 = tx.clone();
//     s.spawn(move || {
//         tx1.send(1);
//     });
//
//     let tx2 = tx.clone();
//     s.spawn(move || {
//         tx2.send(2);
//     });
// })

std::thread::scope(|s| {
    s.spawn(|| {
        tx.send(1);
    });
    
    s.spawn(|| {
        tx.send(2);
    });
})

This comment has been minimized.

Collaborator

The job x86_64-gnu-llvm-14 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

Member

@rfcbot resolve failing-tests

rfcbot

added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised.

and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.

labels

Jun 13, 2023

bellThis is now entering its final comment period, as per the review above. bell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

None yet

8 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK