-
Notifications
You must be signed in to change notification settings - Fork 484
/
future.rs
139 lines (124 loc) · 3.33 KB
/
future.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
#![allow(missing_debug_implementations)]
#![allow(missing_docs)]
use crate::job::JobResult;
use crate::unwind;
use crate::ThreadPool;
use crate::{spawn, spawn_fifo};
use crate::{Scope, ScopeFifo};
use std::future::Future;
use std::mem;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};
pub struct AsyncSpawn<T> {
state: Arc<Mutex<State<T>>>,
}
struct AsyncSpawnJob<T> {
state: Arc<Mutex<State<T>>>,
}
struct State<T> {
result: JobResult<T>,
waker: Option<Waker>,
}
fn new<T>() -> (AsyncSpawn<T>, AsyncSpawnJob<T>) {
let state = Arc::new(Mutex::new(State {
result: JobResult::None,
waker: None,
}));
(
AsyncSpawn {
state: state.clone(),
},
AsyncSpawnJob { state },
)
}
impl<T> Future for AsyncSpawn<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut guard = self.state.lock().expect("rayon future lock");
match mem::replace(&mut guard.result, JobResult::None) {
JobResult::None => {
guard.waker = Some(cx.waker().clone());
Poll::Pending
}
JobResult::Ok(x) => Poll::Ready(x),
JobResult::Panic(p) => {
drop(guard); // don't poison the lock
unwind::resume_unwinding(p);
}
}
}
}
impl<T> AsyncSpawnJob<T> {
fn execute(self, func: impl FnOnce() -> T) {
let result = unwind::halt_unwinding(func);
let mut guard = self.state.lock().expect("rayon future lock");
guard.result = match result {
Ok(x) => JobResult::Ok(x),
Err(p) => JobResult::Panic(p),
};
if let Some(waker) = guard.waker.take() {
waker.wake();
}
}
}
pub fn async_spawn<F, T>(func: F) -> AsyncSpawn<T>
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
let (future, job) = new();
spawn(move || job.execute(func));
future
}
pub fn async_spawn_fifo<F, T>(func: F) -> AsyncSpawn<T>
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
let (future, job) = new();
spawn_fifo(move || job.execute(func));
future
}
impl ThreadPool {
pub fn async_spawn<F, T>(&self, func: F) -> AsyncSpawn<T>
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
let (future, job) = new();
self.spawn(move || job.execute(func));
future
}
pub fn async_spawn_fifo<F, T>(&self, func: F) -> AsyncSpawn<T>
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
let (future, job) = new();
self.spawn_fifo(move || job.execute(func));
future
}
}
impl<'scope> Scope<'scope> {
pub fn async_spawn<F, T>(&self, func: F) -> AsyncSpawn<T>
where
F: FnOnce(&Self) -> T + Send + 'scope,
T: Send + 'scope,
{
let (future, job) = new();
self.spawn(|scope| job.execute(move || func(scope)));
future
}
}
impl<'scope> ScopeFifo<'scope> {
pub fn async_spawn_fifo<F, T>(&self, func: F) -> AsyncSpawn<T>
where
F: FnOnce(&Self) -> T + Send + 'scope,
T: Send + 'scope,
{
let (future, job) = new();
self.spawn_fifo(|scope| job.execute(move || func(scope)));
future
}
}