-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
215 lines (179 loc) · 6.75 KB
/
lib.rs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use futures::future::{abortable, AbortHandle, Aborted};
use pin_project::pin_project;
use std::future::Future;
use std::marker::PhantomData;
use std::mem::transmute;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use tokio::sync::mpsc;
enum Void {}
#[derive(Clone)]
pub struct Scope<'env> {
handle: tokio::runtime::Handle,
chan: mpsc::Sender<Void>,
abort_handles: Arc<Mutex<Vec<AbortHandle>>>,
_marker: PhantomData<&'env mut &'env ()>,
}
#[pin_project]
pub struct ScopedJoinHandle<'scope, R> {
#[pin]
handle: tokio::task::JoinHandle<Result<R, Aborted>>,
_marker: PhantomData<&'scope ()>,
}
impl<'env, R> Future for ScopedJoinHandle<'env, R> {
type Output = Result<R, Box<dyn std::error::Error + Send + 'static>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
match self.project().handle.poll(cx) {
Poll::Ready(Ok(Ok(x))) => Poll::Ready(Ok(x)),
Poll::Ready(Ok(Err(e))) => Poll::Ready(Err(Box::new(e))),
Poll::Ready(Err(e)) => Poll::Ready(Err(Box::new(e))),
Poll::Pending => Poll::Pending,
}
}
}
impl<'env> Scope<'env> {
pub fn spawn<'scope, T, R>(&'scope self, task: T) -> ScopedJoinHandle<'scope, R>
where
T: Future<Output = R> + Send + 'env,
R: Send + 'static, // TODO: weaken to 'env
{
let chan = self.chan.clone();
let (abortable, abort_handle) = abortable(task);
self.abort_handles.lock().unwrap().push(abort_handle);
let task_env: Pin<Box<dyn Future<Output = Result<R, Aborted>> + Send + 'env>> =
Box::pin(async move {
// the cloned channel gets dropped at the end of the task
let _chan = chan;
abortable.await
});
// SAFETY: scoped API ensures the spawned tasks will not outlive the parent scope
let task_static: Pin<Box<dyn Future<Output = Result<R, Aborted>> + Send + 'static>> =
unsafe { transmute(task_env) };
let handle = self.handle.spawn(task_static);
ScopedJoinHandle {
handle,
_marker: PhantomData,
}
}
}
#[derive(Default)]
struct AbortOnDrop {
abort_handles: Arc<Mutex<Vec<AbortHandle>>>,
}
impl Drop for AbortOnDrop {
fn drop(&mut self) {
self.abort_handles
.lock()
.unwrap()
.drain(..)
.for_each(|h| h.abort());
// TODO: we must wait for spawned tasks to abort synchronously here
}
}
// TODO: if `F` takes a reference to the scope, `scope.spawn` will generate a cryptic error
#[doc(hidden)]
pub async unsafe fn scope_impl<'env, F, T, R>(handle: tokio::runtime::Handle, f: F) -> R
where
F: FnOnce(Scope<'env>) -> T,
T: Future<Output = R> + Send,
R: Send,
{
let mut _abort_on_drop = AbortOnDrop::default();
// we won't send data through this channel, so reserve the minimal buffer (buffer size must be
// greater than 0).
let (tx, mut rx) = mpsc::channel(1);
let scope = Scope::<'env> {
handle,
chan: tx,
abort_handles: _abort_on_drop.abort_handles.clone(),
_marker: PhantomData,
};
// TODO: verify that `AbortOnDrop` correctly handles drop and panic.
let result = f(scope).await;
// yield the control until all spawned tasks finish(drop).
assert!(rx.recv().await.is_none());
// no need to abort tasks anymore
_abort_on_drop.abort_handles.lock().unwrap().clear();
result
}
#[macro_export]
macro_rules! scope {
($handle:expr, $func:expr) => {{
unsafe { crate::scope_impl($handle, $func) }.await
}};
}
#[cfg(test)]
mod test {
use super::*;
use std::time::Duration;
use tokio::time::delay_for;
#[test]
fn test_scoped() {
let mut rt = tokio::runtime::Runtime::new().unwrap();
let handle = rt.handle().clone();
rt.block_on(async {
{
let local = String::from("hello world");
let local = &local;
scope!(handle, |scope| {
// TODO: without this `move`, we get a compilation error. why?
async move {
// this spawned subtask will continue running after the scoped task
// finished, but `scope!` will wait until this task completes.
scope.spawn(async move {
delay_for(Duration::from_millis(500)).await;
println!("spanwed task is done: {}", local);
});
// since spawned tasks can outlive the scoped task, they cannot have
// references to the scoped task's stack
/*
let evil = String::from("may dangle");
scope.spawn(async {
delay_for(Duration::from_millis(200)).await;
println!("spanwed task cannot access evil: {}", evil);
});
*/
let handle = scope.spawn(async {
println!("another spawned task");
});
handle.await.unwrap(); // you can await the returned handle
delay_for(Duration::from_millis(100)).await;
println!("scoped task is done: {}", local);
}
});
delay_for(Duration::from_millis(110)).await;
println!("local can be used here: {}", local);
}
println!("local is freed");
delay_for(Duration::from_millis(600)).await;
});
}
#[test]
#[should_panic]
fn test_panic() {
let mut rt = tokio::runtime::Runtime::new().unwrap();
let handle = rt.handle().clone();
rt.block_on(async {
{
let local = String::from("hello world");
let local = &local;
scope!(handle, |scope| {
async move {
scope.spawn(async move {
println!("spanwed task started: {}", local);
delay_for(Duration::from_millis(500)).await;
println!("spanwed task is done: {}", local);
});
delay_for(Duration::from_millis(100)).await;
panic!("scoped task panicked: {}", local);
}
});
delay_for(Duration::from_millis(110)).await;
println!("local can be used here: {}", local);
}
println!("local is freed");
delay_for(Duration::from_millis(600)).await;
});
}
}