-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Pinned Channels #2811
Merged
Merged
Pinned Channels #2811
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5bf1ae8
Switch to pinned channels.
futursolo fb2603e
Fix ServerRenderer so it's not blocked until the result is resolved.
futursolo 892b759
Fix tests.
futursolo 29a46bd
Remove unused SendError.
futursolo 20fc4a3
Merge branch 'master' into pinned-channels
futursolo f84f9d8
Merge branch 'master' into pinned-channels
futursolo a6c65c0
Merge branch 'master' into pinned-channels
futursolo ec96101
Merge branch 'master' into pinned-channels
futursolo 83331d1
Revert channel-based BufWriter.
futursolo e92a2fa
Removed unused fmt module.
futursolo 3fcce34
Update docs.
futursolo fb5b501
Add safety comments, adjust borrowing designs.
futursolo 4a64d9b
Adjust oneshot as well.
futursolo f93c7f7
Merge branch 'master' into pinned-channels
futursolo 24bbea2
Fix workflow temporarily.
futursolo a8ee1c0
Cell or UnsafeCell?
futursolo 6309a9a
Update safety notice.
futursolo 0328976
Should be sync.
futursolo 177c6cb
Actually should be both.
futursolo 1e73fb4
Update packages/yew/src/platform/pinned/mpsc.rs
futursolo 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
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
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
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,6 @@ | ||
//! Task synchronisation primitives for pinned tasks. | ||
//! | ||
//! This module provides task synchronisation for `!Send` futures. | ||
|
||
pub mod mpsc; | ||
pub mod oneshot; |
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,347 @@ | ||
//! A multi-producer single-receiver channel. | ||
|
||
use std::collections::VecDeque; | ||
use std::marker::PhantomData; | ||
use std::rc::Rc; | ||
use std::task::{Poll, Waker}; | ||
|
||
use futures::sink::Sink; | ||
use futures::stream::{FusedStream, Stream}; | ||
use thiserror::Error; | ||
|
||
/// Error returned by [`try_next`](UnboundedReceiver::try_next). | ||
#[derive(Error, Debug)] | ||
#[error("queue is empty")] | ||
pub struct TryRecvError { | ||
_marker: PhantomData<()>, | ||
} | ||
|
||
/// Error returned by [`send_now`](UnboundedSender::send_now). | ||
#[derive(Error, Debug)] | ||
#[error("failed to send")] | ||
pub struct SendError<T> { | ||
/// The send value. | ||
pub inner: T, | ||
} | ||
|
||
/// Error returned by [`UnboundedSender`] when used as a [`Sink`](futures::sink::Sink). | ||
#[derive(Error, Debug)] | ||
#[error("failed to send")] | ||
pub struct TrySendError { | ||
_marker: PhantomData<()>, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Inner<T> { | ||
rx_waker: Option<Waker>, | ||
closed: bool, | ||
sender_ctr: usize, | ||
items: VecDeque<T>, | ||
} | ||
|
||
impl<T> Inner<T> { | ||
/// Creates a unchecked mutable reference from an immutable reference. | ||
/// | ||
/// SAFETY: You can only use this when: | ||
/// | ||
/// 1. The mutable reference is released at the end of a function call. | ||
/// 2. No parent function has acquired the mutable reference. | ||
/// 3. The caller is not an async function / the mutable reference is released before an await | ||
/// statement. | ||
#[inline] | ||
unsafe fn get_mut_unchecked(&self) -> *mut Self { | ||
self as *const Self as *mut Self | ||
} | ||
|
||
fn close(&mut self) { | ||
self.closed = true; | ||
|
||
if let Some(m) = self.rx_waker.take() { | ||
m.wake(); | ||
} | ||
} | ||
} | ||
|
||
/// The receiver of an unbounded mpsc channel. | ||
#[derive(Debug)] | ||
pub struct UnboundedReceiver<T> { | ||
inner: Rc<Inner<T>>, | ||
} | ||
|
||
impl<T> UnboundedReceiver<T> { | ||
/// Try to read the next value from the channel. | ||
/// | ||
/// This function will return: | ||
/// - `Ok(Some(T))` if a value is ready. | ||
/// - `Ok(None)` if the channel has become closed. | ||
/// - `Err(TryRecvError)` if the channel is not closed and the channel is empty. | ||
pub fn try_next(&self) -> std::result::Result<Option<T>, TryRecvError> { | ||
let inner = unsafe { &mut *self.inner.get_mut_unchecked() }; | ||
futursolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
match (inner.items.pop_front(), inner.closed) { | ||
(Some(m), _) => Ok(Some(m)), | ||
(None, false) => Ok(None), | ||
(None, true) => Err(TryRecvError { | ||
_marker: PhantomData, | ||
}), | ||
} | ||
} | ||
} | ||
|
||
impl<T> Stream for UnboundedReceiver<T> { | ||
type Item = T; | ||
|
||
fn poll_next( | ||
self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<Option<Self::Item>> { | ||
let inner = unsafe { &mut *self.inner.get_mut_unchecked() }; | ||
futursolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
match (inner.items.pop_front(), inner.closed) { | ||
(Some(m), _) => Poll::Ready(Some(m)), | ||
(None, false) => { | ||
inner.rx_waker = Some(cx.waker().clone()); | ||
Poll::Pending | ||
} | ||
(None, true) => Poll::Ready(None), | ||
} | ||
} | ||
} | ||
|
||
impl<T> FusedStream for UnboundedReceiver<T> { | ||
fn is_terminated(&self) -> bool { | ||
let inner = unsafe { &mut *self.inner.get_mut_unchecked() }; | ||
futursolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
inner.items.is_empty() && inner.closed | ||
} | ||
} | ||
|
||
impl<T> Drop for UnboundedReceiver<T> { | ||
fn drop(&mut self) { | ||
let inner = unsafe { &mut *self.inner.get_mut_unchecked() }; | ||
futursolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
inner.close(); | ||
} | ||
} | ||
|
||
/// The sender of an unbounded mpsc channel. | ||
#[derive(Debug)] | ||
pub struct UnboundedSender<T> { | ||
inner: Rc<Inner<T>>, | ||
} | ||
|
||
impl<T> UnboundedSender<T> { | ||
/// Sends a value to the unbounded receiver. | ||
pub fn send_now(&self, item: T) -> Result<(), SendError<T>> { | ||
WorldSEnder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let inner = unsafe { &mut *self.inner.get_mut_unchecked() }; | ||
futursolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if inner.closed { | ||
return Err(SendError { inner: item }); | ||
} | ||
|
||
inner.items.push_back(item); | ||
|
||
if let Some(m) = inner.rx_waker.take() { | ||
m.wake(); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Closes the channel. | ||
futursolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn close_now(&self) { | ||
WorldSEnder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let inner = unsafe { &mut *self.inner.get_mut_unchecked() }; | ||
|
||
inner.close(); | ||
} | ||
} | ||
|
||
impl<T> Clone for UnboundedSender<T> { | ||
fn clone(&self) -> Self { | ||
{ | ||
let inner = unsafe { &mut *self.inner.get_mut_unchecked() }; | ||
inner.sender_ctr += 1; | ||
} | ||
|
||
Self { | ||
inner: self.inner.clone(), | ||
} | ||
futursolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
impl<T> Drop for UnboundedSender<T> { | ||
fn drop(&mut self) { | ||
let sender_ctr = { | ||
let inner = unsafe { &mut *self.inner.get_mut_unchecked() }; | ||
futursolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
inner.sender_ctr -= 1; | ||
|
||
inner.sender_ctr | ||
}; | ||
|
||
if sender_ctr == 0 { | ||
self.close_now(); | ||
} | ||
} | ||
} | ||
|
||
impl<T> Sink<T> for &'_ UnboundedSender<T> { | ||
type Error = TrySendError; | ||
|
||
fn start_send(self: std::pin::Pin<&mut Self>, item: T) -> Result<(), Self::Error> { | ||
self.send_now(item).map_err(|_| TrySendError { | ||
_marker: PhantomData, | ||
}) | ||
} | ||
|
||
fn poll_ready( | ||
self: std::pin::Pin<&mut Self>, | ||
_cx: &mut std::task::Context<'_>, | ||
) -> Poll<Result<(), Self::Error>> { | ||
let inner = unsafe { &mut *self.inner.get_mut_unchecked() }; | ||
futursolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
match inner.closed { | ||
false => Poll::Ready(Ok(())), | ||
true => Poll::Ready(Err(TrySendError { | ||
_marker: PhantomData, | ||
})), | ||
} | ||
} | ||
|
||
fn poll_flush( | ||
self: std::pin::Pin<&mut Self>, | ||
_cx: &mut std::task::Context<'_>, | ||
) -> Poll<Result<(), Self::Error>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
|
||
fn poll_close( | ||
self: std::pin::Pin<&mut Self>, | ||
_cx: &mut std::task::Context<'_>, | ||
) -> Poll<Result<(), Self::Error>> { | ||
self.close_now(); | ||
|
||
Poll::Ready(Ok(())) | ||
} | ||
} | ||
|
||
/// Creates an unbounded channel. | ||
/// | ||
/// # Note | ||
/// | ||
/// This channel has an infinite buffer and can run out of memory if the channel is not actively | ||
/// drained. | ||
pub fn unbounded<T>() -> (UnboundedSender<T>, UnboundedReceiver<T>) { | ||
let inner = Rc::new(Inner { | ||
rx_waker: None, | ||
closed: false, | ||
|
||
sender_ctr: 1, | ||
items: VecDeque::new(), | ||
}); | ||
|
||
( | ||
UnboundedSender { | ||
inner: inner.clone(), | ||
}, | ||
UnboundedReceiver { inner }, | ||
) | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
#[cfg(feature = "tokio")] | ||
Comment on lines
+300
to
+301
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we run these tests on both targets? |
||
#[cfg(test)] | ||
mod tests { | ||
use std::time::Duration; | ||
|
||
use futures::sink::SinkExt; | ||
use futures::stream::StreamExt; | ||
use tokio::task::LocalSet; | ||
use tokio::test; | ||
|
||
use super::*; | ||
use crate::platform::spawn_local; | ||
use crate::platform::time::sleep; | ||
|
||
#[test] | ||
async fn mpsc_works() { | ||
let local_set = LocalSet::new(); | ||
|
||
local_set | ||
.run_until(async { | ||
let (tx, mut rx) = unbounded::<usize>(); | ||
|
||
spawn_local(async move { | ||
for i in 0..10 { | ||
(&tx).send(i).await.expect("failed to send."); | ||
sleep(Duration::from_millis(1)).await; | ||
} | ||
}); | ||
|
||
for i in 0..10 { | ||
let received = rx.next().await.expect("failed to receive"); | ||
|
||
assert_eq!(i, received); | ||
} | ||
|
||
assert_eq!(rx.next().await, None); | ||
}) | ||
.await; | ||
} | ||
|
||
#[test] | ||
async fn mpsc_drops_receiver() { | ||
let (tx, rx) = unbounded::<usize>(); | ||
drop(rx); | ||
|
||
(&tx).send(0).await.expect_err("should fail to send."); | ||
} | ||
|
||
#[test] | ||
async fn mpsc_multi_sender() { | ||
let local_set = LocalSet::new(); | ||
|
||
local_set | ||
.run_until(async { | ||
let (tx, mut rx) = unbounded::<usize>(); | ||
|
||
spawn_local(async move { | ||
let tx2 = tx.clone(); | ||
|
||
for i in 0..10 { | ||
if i % 2 == 0 { | ||
(&tx).send(i).await.expect("failed to send."); | ||
} else { | ||
(&tx2).send(i).await.expect("failed to send."); | ||
} | ||
|
||
sleep(Duration::from_millis(1)).await; | ||
} | ||
|
||
drop(tx2); | ||
|
||
for i in 10..20 { | ||
(&tx).send(i).await.expect("failed to send."); | ||
|
||
sleep(Duration::from_millis(1)).await; | ||
} | ||
}); | ||
|
||
for i in 0..20 { | ||
let received = rx.next().await.expect("failed to receive"); | ||
|
||
assert_eq!(i, received); | ||
} | ||
|
||
assert_eq!(rx.next().await, None); | ||
}) | ||
.await; | ||
} | ||
|
||
#[test] | ||
async fn mpsc_drops_sender() { | ||
let (tx, mut rx) = unbounded::<usize>(); | ||
drop(tx); | ||
|
||
assert_eq!(rx.next().await, None); | ||
} | ||
} |
Oops, something went wrong.
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.
Why have these markers?
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.
This is to mark that there is a hidden information in this struct.
Some other struct have this as we need to expand and add error reason in the future. This is here to keep consistency.