-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
std::sync::mpsc::SharedSender #1299
Closed
Closed
Changes from all commits
Commits
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
- Feature Name: shared_sender | ||
- Start Date: 2015-09-28 | ||
- RFC PR: (leave this empty) | ||
- Rust Issue: (leave this empty) | ||
|
||
# Summary | ||
|
||
Add a `SharedSender` to `std::sync::mpsc` that implements `Sync`. | ||
|
||
# Motivation | ||
|
||
The current `std::sync::mpsc::Sender` does not implement `Sync`. This is | ||
because the `Sender` starts as a `spsc` queue, and needs to upgrade to `mpsc` using `Clone::clone`. Accidentally putting the `Sender` into an `Arc` and cloning that would skip the upgrade, and make the `Sender` unsafe. So far, the design is just fine. | ||
|
||
However, at times, there is real desire for the `Sender` to implement `Sync`. If passing the `Sender` into something requiring `Sync`, the only options are both sub-optimal: a) put the `Sender` in a `Mutex`, or b) look on crates.io for another mpsc solution. | ||
|
||
Both "solutions" are not even truly required, since inside the `mpsc` module, there exists all the code necessary for a thread-safe mpsc, in the `Flavor::Shared` variant that is used when you clone a `Sender`. | ||
|
||
# Detailed design | ||
|
||
Add the following struct to the `mpsc` module: | ||
|
||
```rust | ||
pub struct SharedSender<T> { | ||
inner: Arc<UnsafeCell<shared::Packet<T>>> | ||
} | ||
|
||
unsafe impl<T: Send> Send for SharedSender<T> {} | ||
unsafe impl<T: Send> Sync for SharedSender<T> {} | ||
|
||
impl<T: Send> SharedSender { | ||
fn new(inner: Arc<UnsafeCell<shared::Packet<T>>>) -> SharedSender<T> { | ||
SharedSender { | ||
inner: inner | ||
} | ||
} | ||
pub fn send(&self, t: T) -> Result<(), SendError<T>> { | ||
unsafe { &mut *self.inner.get() }.send(t) | ||
} | ||
} | ||
|
||
impl<T: Send> Clone for SharedSender<T> { | ||
fn clone(&self) -> SharedSender<T> { | ||
let a = self.inner.clone(); | ||
unsafe { &mut *a }.clone_chan(); | ||
SharedSender::new(a) | ||
} | ||
} | ||
|
||
impl<T> Drop for SharedSender<T> { | ||
fn drop(&mut self) { | ||
unsafe { &mut *self.inner.get() }.drop_chan(); | ||
} | ||
} | ||
|
||
``` | ||
|
||
In order to create a `SharedSender`, the following method is proposed: | ||
|
||
```rust | ||
pub fn shared_channel<T: Send>() -> (SharedSender<T>, Receiver<T>) { | ||
let a = Arc::new(UnsafeCell::new(shared::Packet::new())); | ||
(SharedSender::new(a.clone())), Receiver::new(Flavor::Shared(a))) | ||
} | ||
``` | ||
|
||
# Drawbacks | ||
|
||
This adds more API surface area, and the specific details between `Sender` and `SharedSender` might be confusing. | ||
|
||
# Alternatives | ||
|
||
An alternative to the `shared_channel()` function could be adding a `shared()` upgrade method to `Sender` instead. Example: | ||
|
||
```rust | ||
impl<T: Send> Sender<T> { | ||
pub fn shared(self) -> SharedSender<T> { | ||
// upgrade to Flavor::Shared, take shared::Packet, create SharedSender | ||
} | ||
} | ||
``` | ||
|
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.
I would personally prefer this to a second channel constructor function, but discoverability might be hard?
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.
Er, I forgot to add description as to its downside: if you at the beginning you want a shared sender, this is just wasted operations going from oneshot -> shared, and bumping the internal counter by one since the
Sender
will be dropped and drop the counter.