forked from mirror/async-std
fix spawning
This commit is contained in:
parent
fc9ee0dfdd
commit
e082634b5e
5 changed files with 37 additions and 18 deletions
|
@ -56,7 +56,7 @@ alloc = [
|
|||
|
||||
[dependencies]
|
||||
async-attributes = { version = "1.1.1", optional = true }
|
||||
async-task = { version = "1.3.1", optional = true }
|
||||
async-task = { version = "3.0.0", optional = true }
|
||||
broadcaster = { version = "1.0.0", optional = true }
|
||||
crossbeam-channel = { version = "0.4.2", optional = true }
|
||||
crossbeam-deque = { version = "0.7.3", optional = true }
|
||||
|
|
|
@ -42,9 +42,9 @@ where
|
|||
|
||||
let wrapped_future = async move {
|
||||
// Drop task-locals on exit.
|
||||
// defer! {
|
||||
// Task::get_current(|t| unsafe { t.drop_locals() });
|
||||
// }
|
||||
defer! {
|
||||
Task::get_current(|t| unsafe { t.drop_locals() });
|
||||
}
|
||||
|
||||
// Log completion on exit.
|
||||
defer! {
|
||||
|
|
|
@ -42,9 +42,9 @@ impl Builder {
|
|||
|
||||
let wrapped_future = async move {
|
||||
// Drop task-locals on exit.
|
||||
// defer! {
|
||||
// Task::get_current(|t| unsafe { t.drop_locals() });
|
||||
// }
|
||||
defer! {
|
||||
Task::get_current(|t| unsafe { t.drop_locals() });
|
||||
}
|
||||
|
||||
// Log completion on exit.
|
||||
defer! {
|
||||
|
@ -57,7 +57,9 @@ impl Builder {
|
|||
|
||||
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
|
||||
|
||||
let task = smol::Task::spawn(wrapped_future);
|
||||
Ok(JoinHandle::new(task))
|
||||
// FIXME: figure out how to set the current task.
|
||||
|
||||
let smol_task = smol::Task::spawn(wrapped_future).detach();
|
||||
Ok(JoinHandle::new(smol_task, task))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,12 +12,18 @@ use crate::task::{Context, Poll, Task};
|
|||
///
|
||||
/// [spawned]: fn.spawn.html
|
||||
#[derive(Debug)]
|
||||
pub struct JoinHandle<T>(smol::Task<T>);
|
||||
pub struct JoinHandle<T> {
|
||||
handle: Option<async_task::JoinHandle<T, ()>>,
|
||||
task: Task,
|
||||
}
|
||||
|
||||
impl<T> JoinHandle<T> {
|
||||
/// Creates a new `JoinHandle`.
|
||||
pub(crate) fn new(inner: smol::Task<T>) -> JoinHandle<T> {
|
||||
JoinHandle(inner)
|
||||
pub(crate) fn new(inner: async_task::JoinHandle<T, ()>, task: Task) -> JoinHandle<T> {
|
||||
JoinHandle {
|
||||
handle: Some(inner),
|
||||
task,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a handle to the underlying task.
|
||||
|
@ -36,7 +42,14 @@ impl<T> JoinHandle<T> {
|
|||
/// #
|
||||
/// # })
|
||||
pub fn task(&self) -> &Task {
|
||||
todo!()
|
||||
&self.task
|
||||
}
|
||||
|
||||
/// Cancel this task.
|
||||
pub async fn cancel(mut self) -> Option<T> {
|
||||
let handle = self.handle.take().unwrap();
|
||||
handle.cancel();
|
||||
handle.await
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,7 +57,11 @@ impl<T> Future for JoinHandle<T> {
|
|||
type Output = T;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
dbg!("poll joinhandle");
|
||||
Pin::new(&mut self.0).poll(cx)
|
||||
match Pin::new(&mut self.handle.as_mut().unwrap()).poll(cx) {
|
||||
Poll::Pending => Poll::Pending,
|
||||
Poll::Ready(output) => {
|
||||
Poll::Ready(output.expect("cannot await the result of a panicked task"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::task::JoinHandle;
|
||||
use crate::task::{JoinHandle, Task};
|
||||
|
||||
/// Spawns a blocking task.
|
||||
///
|
||||
|
@ -37,6 +37,6 @@ where
|
|||
{
|
||||
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
|
||||
|
||||
let handle = smol::Task::blocking(async move { f() });
|
||||
JoinHandle::new(handle)
|
||||
let handle = smol::Task::blocking(async move { f() }).detach();
|
||||
JoinHandle::new(handle, Task::new(None))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue