-
Notifications
You must be signed in to change notification settings - Fork 12.1k
/
block_on_future.rs
229 lines (197 loc) · 5.23 KB
/
block_on_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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! Tests for the block_on_future function
#![feature(block_on_future)]
use std::future::Future;
use std::task::{Context, Poll, Waker};
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::thread::{block_on_future, JoinHandle, spawn};
struct WakeFromRemoteThreadFuture {
was_polled: bool,
wake_by_ref: bool,
join_handle: Option<JoinHandle<()>>,
}
impl WakeFromRemoteThreadFuture {
fn new(wake_by_ref: bool) -> Self {
WakeFromRemoteThreadFuture {
was_polled: false,
wake_by_ref,
join_handle: None,
}
}
}
impl Future for WakeFromRemoteThreadFuture {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
if !self.was_polled {
self.was_polled = true;
let waker = cx.waker().clone();
let wake_by_ref = self.wake_by_ref;
self.join_handle = Some(spawn(move || {
if wake_by_ref {
waker.wake();
} else {
waker.wake_by_ref();
}
}));
Poll::Pending
} else {
if let Some(handle) = self.join_handle.take() {
handle.join().unwrap();
}
Poll::Ready(())
}
}
}
struct Yield {
iterations: usize,
}
impl Yield {
fn new(iterations: usize) -> Self {
Yield {
iterations
}
}
}
impl Future for Yield {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.iterations == 0 {
Poll::Ready(())
} else {
self.iterations -= 1;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
struct NeverReady {
}
impl NeverReady {
fn new() -> Self {
NeverReady {}
}
}
impl Future for NeverReady {
type Output = ();
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Pending
}
}
struct WakerStore {
waker: Option<Waker>,
}
struct StoreWakerFuture {
store: Arc<Mutex<WakerStore>>,
}
impl StoreWakerFuture {
fn new(store: Arc<Mutex<WakerStore>>) -> Self {
StoreWakerFuture {
store
}
}
}
impl Future for StoreWakerFuture {
type Output = ();
fn poll(mut self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
(*self.store.lock().unwrap()).waker = Some(cx.waker().clone());
Poll::Ready(())
}
}
struct WakeFromPreviouslyStoredWakerFuture {
store: Arc<Mutex<WakerStore>>,
was_polled: bool,
join_handle: Option<JoinHandle<()>>,
}
impl WakeFromPreviouslyStoredWakerFuture {
fn new(store: Arc<Mutex<WakerStore>>) -> Self {
WakeFromPreviouslyStoredWakerFuture {
store,
was_polled: false,
join_handle: None,
}
}
}
impl Future for WakeFromPreviouslyStoredWakerFuture {
type Output = ();
fn poll(mut self: core::pin::Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
if !self.was_polled {
self.was_polled = true;
// Don't take the waker from Context but from the side channel
let waker = self.store.lock().unwrap().waker.clone().take().unwrap();
self.join_handle = Some(spawn(move || {
waker.wake();
}));
Poll::Pending
} else {
if let Some(handle) = self.join_handle.take() {
handle.join().unwrap();
}
Poll::Ready(())
}
}
}
#[test]
fn wake_from_local_thread() {
block_on_future(async {
Yield::new(10).await;
});
}
#[test]
fn wake_from_foreign_thread() {
block_on_future(async {
WakeFromRemoteThreadFuture::new(false).await;
});
}
#[test]
fn wake_by_ref_from_foreign_thread() {
block_on_future(async {
WakeFromRemoteThreadFuture::new(true).await;
});
}
#[test]
fn wake_from_multiple_threads() {
block_on_future(async {
WakeFromRemoteThreadFuture::new(false).await;
WakeFromRemoteThreadFuture::new(true).await;
});
}
#[test]
fn wake_local_remote_local() {
block_on_future(async {
Yield::new(10).await;
WakeFromRemoteThreadFuture::new(false).await;
Yield::new(20).await;
WakeFromRemoteThreadFuture::new(true).await;
});
}
#[test]
fn returns_result_from_task() {
let result = block_on_future(async {
let x = 42i32;
Yield::new(10).await;
x
});
assert_eq!(42, result);
}
#[test]
#[should_panic]
fn panics_if_waker_was_not_cloned_and_task_is_not_ready() {
block_on_future(async {
NeverReady::new().await;
});
}
#[test]
fn does_not_panic_if_waker_is_cloned_and_used_a_lot_later() {
let store = Arc::new(Mutex::new(WakerStore {
waker: None,
}));
block_on_future(async {
StoreWakerFuture::new(store.clone()).await;
Yield::new(10).await;
// Multiple wakes from an outdated waker - because it can
// have been cloned multiple times.
WakeFromPreviouslyStoredWakerFuture::new(store.clone()).await;
WakeFromPreviouslyStoredWakerFuture::new(store.clone()).await;
WakeFromPreviouslyStoredWakerFuture::new(store).await;
});
}