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
mod deque;
mod local;
mod poll_fn;
mod shared;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::Poll;
use crate::poll_fn::poll_fn;
pub fn push(task: impl Future<Output = ()> + Send + 'static) {
shared::push(task);
}
pub fn push_local(task: impl Future<Output = ()> + 'static) {
local::push(task);
}
pub struct Poller<'a> {
local: local::Poller<'a>,
shared: shared::Poller<'a>,
_marker: PhantomData<*mut ()>,
}
pub fn poller<'a>() -> Poller<'a> {
Poller {
local: local::poller(),
shared: shared::poller(),
_marker: PhantomData,
}
}
impl<'a> Poller<'a> {
#[inline(always)]
pub fn poll_one(&self) -> bool {
if self.local.poll_one() {
return true;
}
if self.shared.poll_one() {
return true;
}
false
}
#[inline(always)]
pub async fn wait(&self) -> bool {
let mut local_wait = Some(self.local.wait());
let mut shared_wait = Some(self.shared.wait());
poll_fn(move |cx| {
assert!(
local_wait.is_none() && shared_wait.is_none(),
"calling poll when future is already done"
);
if local_wait.is_some() {
let f = local_wait.as_mut().unwrap();
match unsafe { Pin::new_unchecked(f) }.poll(cx) {
Poll::Ready(ok) => {
local_wait.take();
if ok {
return Poll::Ready(true);
}
}
Poll::Pending => {}
}
}
if shared_wait.is_some() {
let f = shared_wait.as_mut().unwrap();
match unsafe { Pin::new_unchecked(f) }.poll(cx) {
Poll::Ready(ok) => {
shared_wait.take();
if ok {
return Poll::Ready(true);
}
}
Poll::Pending => {}
}
}
if local_wait.is_some() || shared_wait.is_some() {
Poll::Pending
} else {
Poll::Ready(false)
}
})
.await
}
}