mirror of
https://github.com/async-rs/async-std.git
synced 2025-03-05 01:29:43 +00:00
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]
|
[dependencies]
|
||||||
async-attributes = { version = "1.1.1", optional = true }
|
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 }
|
broadcaster = { version = "1.0.0", optional = true }
|
||||||
crossbeam-channel = { version = "0.4.2", optional = true }
|
crossbeam-channel = { version = "0.4.2", optional = true }
|
||||||
crossbeam-deque = { version = "0.7.3", optional = true }
|
crossbeam-deque = { version = "0.7.3", optional = true }
|
||||||
|
|
|
@ -42,9 +42,9 @@ where
|
||||||
|
|
||||||
let wrapped_future = async move {
|
let wrapped_future = async move {
|
||||||
// Drop task-locals on exit.
|
// Drop task-locals on exit.
|
||||||
// defer! {
|
defer! {
|
||||||
// Task::get_current(|t| unsafe { t.drop_locals() });
|
Task::get_current(|t| unsafe { t.drop_locals() });
|
||||||
// }
|
}
|
||||||
|
|
||||||
// Log completion on exit.
|
// Log completion on exit.
|
||||||
defer! {
|
defer! {
|
||||||
|
|
|
@ -42,9 +42,9 @@ impl Builder {
|
||||||
|
|
||||||
let wrapped_future = async move {
|
let wrapped_future = async move {
|
||||||
// Drop task-locals on exit.
|
// Drop task-locals on exit.
|
||||||
// defer! {
|
defer! {
|
||||||
// Task::get_current(|t| unsafe { t.drop_locals() });
|
Task::get_current(|t| unsafe { t.drop_locals() });
|
||||||
// }
|
}
|
||||||
|
|
||||||
// Log completion on exit.
|
// Log completion on exit.
|
||||||
defer! {
|
defer! {
|
||||||
|
@ -57,7 +57,9 @@ impl Builder {
|
||||||
|
|
||||||
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
|
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
|
||||||
|
|
||||||
let task = smol::Task::spawn(wrapped_future);
|
// FIXME: figure out how to set the current task.
|
||||||
Ok(JoinHandle::new(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
|
/// [spawned]: fn.spawn.html
|
||||||
#[derive(Debug)]
|
#[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> {
|
impl<T> JoinHandle<T> {
|
||||||
/// Creates a new `JoinHandle`.
|
/// Creates a new `JoinHandle`.
|
||||||
pub(crate) fn new(inner: smol::Task<T>) -> JoinHandle<T> {
|
pub(crate) fn new(inner: async_task::JoinHandle<T, ()>, task: Task) -> JoinHandle<T> {
|
||||||
JoinHandle(inner)
|
JoinHandle {
|
||||||
|
handle: Some(inner),
|
||||||
|
task,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a handle to the underlying task.
|
/// Returns a handle to the underlying task.
|
||||||
|
@ -36,7 +42,14 @@ impl<T> JoinHandle<T> {
|
||||||
/// #
|
/// #
|
||||||
/// # })
|
/// # })
|
||||||
pub fn task(&self) -> &Task {
|
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;
|
type Output = T;
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
dbg!("poll joinhandle");
|
match Pin::new(&mut self.handle.as_mut().unwrap()).poll(cx) {
|
||||||
Pin::new(&mut self.0).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.
|
/// Spawns a blocking task.
|
||||||
///
|
///
|
||||||
|
@ -37,6 +37,6 @@ where
|
||||||
{
|
{
|
||||||
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
|
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
|
||||||
|
|
||||||
let handle = smol::Task::blocking(async move { f() });
|
let handle = smol::Task::blocking(async move { f() }).detach();
|
||||||
JoinHandle::new(handle)
|
JoinHandle::new(handle, Task::new(None))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue