2021-11-29 18:45:24 +00:00
|
|
|
// Copyright (C) 2018-2020 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
// Copyright (C) 2019-2021 François Laignel <fengalin@free.fr>
|
|
|
|
//
|
2021-12-14 18:40:27 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2021-11-29 18:45:24 +00:00
|
|
|
|
|
|
|
use futures::prelude::*;
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::Poll;
|
|
|
|
|
|
|
|
use super::context::Context;
|
|
|
|
use super::TaskId;
|
2021-12-14 18:40:27 +00:00
|
|
|
use super::{Handle, Scheduler};
|
2021-11-29 18:45:24 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct JoinError(TaskId);
|
|
|
|
|
|
|
|
impl fmt::Display for JoinError {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-12-14 18:40:27 +00:00
|
|
|
write!(fmt, "{:?} was cancelled", self.0)
|
2021-11-29 18:45:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for JoinError {}
|
|
|
|
|
|
|
|
pub struct JoinHandle<T> {
|
2021-12-14 18:40:27 +00:00
|
|
|
task: Option<async_task::Task<T>>,
|
2021-11-29 18:45:24 +00:00
|
|
|
task_id: TaskId,
|
2021-12-14 18:40:27 +00:00
|
|
|
scheduler: Handle,
|
2021-11-29 18:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T: Send> Send for JoinHandle<T> {}
|
|
|
|
unsafe impl<T: Send> Sync for JoinHandle<T> {}
|
|
|
|
|
|
|
|
impl<T> JoinHandle<T> {
|
2021-12-14 18:40:27 +00:00
|
|
|
pub(super) fn new(task_id: TaskId, task: async_task::Task<T>, scheduler: &Handle) -> Self {
|
2021-11-29 18:45:24 +00:00
|
|
|
JoinHandle {
|
2021-12-14 18:40:27 +00:00
|
|
|
task: Some(task),
|
2021-11-29 18:45:24 +00:00
|
|
|
task_id,
|
2021-12-14 18:40:27 +00:00
|
|
|
scheduler: scheduler.clone(),
|
2021-11-29 18:45:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_current(&self) -> bool {
|
|
|
|
if let Some((cur_scheduler, task_id)) = Scheduler::current().zip(TaskId::current()) {
|
2021-12-14 18:40:27 +00:00
|
|
|
cur_scheduler == self.scheduler && task_id == self.task_id
|
2021-11-29 18:45:24 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:40:27 +00:00
|
|
|
pub fn context(&self) -> Context {
|
|
|
|
Context::from(self.scheduler.clone())
|
2021-11-29 18:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn task_id(&self) -> TaskId {
|
|
|
|
self.task_id
|
|
|
|
}
|
2021-12-14 18:40:27 +00:00
|
|
|
|
|
|
|
pub fn cancel(mut self) {
|
|
|
|
let _ = self.task.take().map(|task| task.cancel());
|
|
|
|
}
|
2021-11-29 18:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Future for JoinHandle<T> {
|
|
|
|
type Output = Result<T, JoinError>;
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
|
|
|
|
if self.as_ref().is_current() {
|
|
|
|
panic!("Trying to join task {:?} from itself", self.as_ref());
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:40:27 +00:00
|
|
|
if let Some(task) = self.as_mut().task.as_mut() {
|
|
|
|
// Unfortunately, we can't detect whether the task has panicked
|
|
|
|
// because the `async_task::Task` `Future` implementation
|
|
|
|
// `expect`s and we can't `panic::catch_unwind` here because of `&mut cx`.
|
|
|
|
// One solution for this would be to use our own `async_task` impl.
|
|
|
|
task.poll_unpin(cx).map(Ok)
|
|
|
|
} else {
|
|
|
|
Poll::Ready(Err(JoinError(self.task_id)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Drop for JoinHandle<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(task) = self.task.take() {
|
|
|
|
task.detach();
|
|
|
|
}
|
2021-11-29 18:45:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for JoinHandle<T> {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt.debug_struct("JoinHandle")
|
2021-12-14 18:40:27 +00:00
|
|
|
.field("context", &self.scheduler.context_name())
|
2021-11-29 18:45:24 +00:00
|
|
|
.field("task_id", &self.task_id)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|