Skip to content
This repository was archived by the owner on Oct 30, 2019. It is now read-only.

Replace JoinHandle with RemoteHandle #80

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 5 additions & 27 deletions benches/baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate test;

mod baseline {
use futures::executor;
use futures::future::RemoteHandle;
use futures::prelude::*;
use std::pin::Pin;
use std::task::{Context, Poll};
Expand Down Expand Up @@ -100,7 +101,7 @@ mod baseline {
}
}))
.map(|_: Result<(), ()>| ()),
)
).await
})
})
.collect::<Vec<_>>();
Expand All @@ -113,38 +114,15 @@ mod baseline {
}

/// Spawn function for juliex to get back a handle
pub fn spawn<F, T>(fut: F) -> JoinHandle<T>
pub fn spawn<F, T>(fut: F) -> RemoteHandle<T>
Copy link
Collaborator

Choose a reason for hiding this comment

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

The model we've been using so far is Tasks being async versions of Threads. In so the naming of JoinHandle would be more accurate, as it has a direct counterpart in std::thread::JoinHandle.

where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
let (tx, rx) = futures::channel::oneshot::channel();

let fut = async move {
let t = fut.await;
let _ = tx.send(t);
};
let (fut, handle) = fut.remote_handle();

juliex::spawn(fut);
JoinHandle { rx }
}

/// Handle returned from Juliex.
// We should patch Juliex to support this natively, and be more efficient on channel use.
#[derive(Debug)]
pub struct JoinHandle<T> {
pub(crate) rx: futures::channel::oneshot::Receiver<T>,
}

impl<T> Future for JoinHandle<T> {
type Output = T;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.rx.poll_unpin(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok(t)) => Poll::Ready(t),
Poll::Ready(Err(_)) => panic!(), // TODO: Is this OK? Print a better error message?
}
}
handle
}
}
2 changes: 1 addition & 1 deletion benches/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ macro_rules! benchmark_suite {
}
}))
.map(|_: Result<(), ()>| ()),
)
).await
})
})
.collect::<Vec<_>>();
Expand Down
38 changes: 5 additions & 33 deletions src/task.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//! Types and Functions for working with asynchronous tasks.

use std::pin::Pin;

use futures::future::FutureObj;
use futures::future::{FutureObj, RemoteHandle};
use futures::prelude::*;
use futures::task::{Context, Poll, Spawn, SpawnError};
use futures::task::{Spawn, SpawnError};

/// A [`Spawn`] handle to runtime's thread pool for spawning futures.
///
Expand Down Expand Up @@ -63,42 +61,16 @@ impl<'a> Spawn for &'a Spawner {
/// assert_eq!(handle.await, 42);
/// }
/// ```
pub fn spawn<F, T>(fut: F) -> JoinHandle<T>
pub fn spawn<F, T>(fut: F) -> RemoteHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
let (tx, rx) = futures::channel::oneshot::channel();

let fut = async move {
let t = fut.await;
let _ = tx.send(t);
};
let (fut, handle) = fut.remote_handle();

runtime_raw::current_runtime()
.spawn_boxed(fut.boxed())
.expect("cannot spawn a future");

JoinHandle { rx }
}

/// A handle that awaits the result of a [`spawn`]ed future.
///
/// [`spawn`]: fn.spawn.html
#[must_use = "futures do nothing unless you `.await` or poll them"]
#[derive(Debug)]
pub struct JoinHandle<T> {
pub(crate) rx: futures::channel::oneshot::Receiver<T>,
}

impl<T> Future for JoinHandle<T> {
type Output = T;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.rx.poll_unpin(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok(t)) => Poll::Ready(t),
Poll::Ready(Err(_)) => panic!(), // TODO: Is this OK? Print a better error message?
}
}
handle
}