1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use futures_core::{Future, Async, Poll};
use futures_core::never::Never;
use futures_core::task::{self, Context};
use futures_channel::oneshot::{channel, Sender, Receiver};
use futures_util::FutureExt;
use std::thread;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::AtomicBool;
#[derive(Debug)]
pub struct Spawn<F>(Option<F>);
pub fn spawn<F>(f: F) -> Spawn<F>
where F: Future<Item = (), Error = Never> + 'static + Send
{
Spawn(Some(f))
}
impl<F: Future<Item = (), Error = Never> + Send + 'static> Future for Spawn<F> {
type Item = ();
type Error = Never;
fn poll(&mut self, cx: &mut Context) -> Poll<(), Never> {
cx.spawn(self.0.take().unwrap());
Ok(Async::Ready(()))
}
}
#[derive(Debug)]
pub struct SpawnWithHandle<F>(Option<F>);
pub fn spawn_with_handle<F>(f: F) -> SpawnWithHandle<F>
where F: Future + 'static + Send, F::Item: Send, F::Error: Send
{
SpawnWithHandle(Some(f))
}
impl<F> Future for SpawnWithHandle<F>
where F: Future<Item = (), Error = Never> + Send + 'static,
F::Item: Send,
F::Error: Send,
{
type Item = JoinHandle<F::Item, F::Error>;
type Error = Never;
fn poll(&mut self, cx: &mut Context) -> Poll<Self::Item, Never> {
let (tx, rx) = channel();
let keep_running_flag = Arc::new(AtomicBool::new(false));
let sender = MySender {
fut: AssertUnwindSafe(self.0.take().unwrap()).catch_unwind(),
tx: Some(tx),
keep_running_flag: keep_running_flag.clone(),
};
cx.spawn(sender);
Ok(Async::Ready(JoinHandle {
inner: rx ,
keep_running_flag: keep_running_flag.clone()
}))
}
}
struct MySender<F, T> {
fut: F,
tx: Option<Sender<T>>,
keep_running_flag: Arc<AtomicBool>,
}
#[must_use]
#[derive(Debug)]
pub struct JoinHandle<T, E> {
inner: Receiver<thread::Result<Result<T, E>>>,
keep_running_flag: Arc<AtomicBool>,
}
impl<T, E> JoinHandle<T, E> {
pub fn forget(self) {
self.keep_running_flag.store(true, Ordering::SeqCst);
}
}
impl<T: Send + 'static, E: Send + 'static> Future for JoinHandle<T, E> {
type Item = T;
type Error = E;
fn poll(&mut self, cx: &mut task::Context) -> Poll<T, E> {
match self.inner.poll(cx).expect("cannot poll JoinHandle twice") {
Async::Ready(Ok(Ok(e))) => Ok(e.into()),
Async::Ready(Ok(Err(e))) => Err(e),
Async::Ready(Err(e)) => panic::resume_unwind(e),
Async::Pending => Ok(Async::Pending),
}
}
}
impl<F: Future> Future for MySender<F, Result<F::Item, F::Error>> {
type Item = ();
type Error = Never;
fn poll(&mut self, cx: &mut task::Context) -> Poll<(), Never> {
if let Ok(Async::Ready(_)) = self.tx.as_mut().unwrap().poll_cancel(cx) {
if !self.keep_running_flag.load(Ordering::SeqCst) {
return Ok(().into())
}
}
let res = match self.fut.poll(cx) {
Ok(Async::Ready(e)) => Ok(e),
Ok(Async::Pending) => return Ok(Async::Pending),
Err(e) => Err(e),
};
drop(self.tx.take().unwrap().send(res));
Ok(Async::Ready(()))
}
}