-
Notifications
You must be signed in to change notification settings - Fork 23
/
lib.rs
272 lines (225 loc) Β· 8.12 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use log::debug;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll, Waker};
use futures_task::{ArcWake, FutureObj};
use libc::{fd_set, select, timeval, FD_ISSET, FD_SET, FD_ZERO};
use std::os::unix::io::RawFd;
use std::cell::{Cell, RefCell};
use std::collections::{BTreeMap, VecDeque};
use std::rc::Rc;
use std::sync::Arc;
mod async_tcp_listener;
mod async_tcp_stream;
pub use crate::async_tcp_listener::AsyncTcpListener;
pub use crate::async_tcp_stream::AsyncTcpStream;
// reactor lives in a thread local variable. Here's where all magic happens!
thread_local! {
static REACTOR: Rc<EventLoop> = Rc::new(EventLoop::new());
}
type TaskId = usize;
pub fn run<F: Future<Output = ()> + Send + 'static>(f: F) {
REACTOR.with(|reactor| reactor.run(f))
}
pub fn spawn<F: Future<Output = ()> + Send + 'static>(f: F) {
REACTOR.with(|reactor| reactor.do_spawn(f))
}
// Our waker Token. It stores the index of the future in the wait queue
// (see below)
#[derive(Debug)]
struct Token(usize);
impl ArcWake for Token {
fn wake_by_ref(arc_self: &Arc<Self>) {
debug!("waking {:?}", arc_self);
let Token(idx) = **arc_self;
// get access to the reactor by way of TLS and call wake
REACTOR.with(|reactor| {
let wakeup = Wakeup {
index: idx,
waker: futures_task::waker(arc_self.clone()),
};
reactor.wake(wakeup);
});
}
}
// Wakeup notification struct stores the index of the future in the wait queue
// and waker
struct Wakeup {
index: usize,
waker: Waker,
}
// Task is a boxed future with Output = ()
struct Task {
future: FutureObj<'static, ()>,
}
impl Task {
// returning Ready will lead to task being removed from wait queues and dropped
fn poll(&mut self, waker: Waker) -> Poll<()> {
let future = Pin::new(&mut self.future);
let mut ctx = Context::from_waker(&waker);
match future.poll(&mut ctx) {
Poll::Ready(_) => {
debug!("future done");
Poll::Ready(())
}
Poll::Pending => {
debug!("future not yet ready");
Poll::Pending
}
}
}
}
// The "real" event loop.
struct EventLoop {
read: RefCell<BTreeMap<RawFd, Waker>>,
write: RefCell<BTreeMap<RawFd, Waker>>,
counter: Cell<usize>,
wait_queue: RefCell<BTreeMap<TaskId, Task>>,
run_queue: RefCell<VecDeque<Wakeup>>,
}
impl EventLoop {
fn new() -> Self {
EventLoop {
read: RefCell::new(BTreeMap::new()),
write: RefCell::new(BTreeMap::new()),
counter: Cell::new(0),
wait_queue: RefCell::new(BTreeMap::new()),
run_queue: RefCell::new(VecDeque::new()),
}
}
// a future calls this to register its interest
// in socket's "ready to be read" events
fn add_read_interest(&self, fd: RawFd, waker: Waker) {
debug!("adding read interest for {}", fd);
if !self.read.borrow().contains_key(&fd) {
self.read.borrow_mut().insert(fd, waker);
}
}
fn remove_read_interest(&self, fd: RawFd) {
debug!("removing read interest for {}", fd);
self.read.borrow_mut().remove(&fd);
}
// see above
fn remove_write_interest(&self, fd: RawFd) {
debug!("removing write interest for {}", fd);
self.write.borrow_mut().remove(&fd);
}
fn add_write_interest(&self, fd: RawFd, waker: Waker) {
debug!("adding write interest for {}", fd);
if !self.write.borrow().contains_key(&fd) {
self.write.borrow_mut().insert(fd, waker);
}
}
// waker calls this to put the future on the run queue
fn wake(&self, wakeup: Wakeup) {
self.run_queue.borrow_mut().push_back(wakeup);
}
fn next_task(&self) -> (TaskId, Waker) {
let counter = self.counter.get();
let w = Arc::new(Token(counter));
self.counter.set(counter + 1);
(counter, futures_task::waker(w))
}
// create a task, poll it once and push it on wait queue
fn do_spawn<F: Future<Output = ()> + Send + 'static>(&self, f: F) {
let (id, waker) = self.next_task();
let f = Box::new(f);
let mut task = Task {
future: FutureObj::new(f),
};
// if the task is ready immediately, don't add it to wait_queue
if let Poll::Ready(_) = task.poll(waker) {
return;
}
self.wait_queue.borrow_mut().insert(id, task);
}
// the meat of the event loop
// we're using select(2) because it's simple and it's portable
pub fn run<F: Future<Output = ()> + Send + 'static>(&self, f: F) {
self.do_spawn(f);
loop {
debug!("select loop start");
// event loop iteration timeout. if no descriptor
// is ready we continue iterating
let mut tv: timeval = timeval {
tv_sec: 1,
tv_usec: 0,
};
// initialize fd_sets (file descriptor sets)
let mut read_fds: fd_set = unsafe { std::mem::zeroed() };
let mut write_fds: fd_set = unsafe { std::mem::zeroed() };
unsafe { FD_ZERO(&mut read_fds) };
unsafe { FD_ZERO(&mut write_fds) };
let mut nfds = 0;
// add read interests to read fd_sets
for fd in self.read.borrow().keys() {
debug!("added fd {} for read", fd);
unsafe { FD_SET(*fd, &mut read_fds as *mut fd_set) };
nfds = std::cmp::max(nfds, fd + 1);
}
// add write interests to write fd_sets
for fd in self.write.borrow().keys() {
debug!("added fd {} for write", fd);
unsafe { FD_SET(*fd, &mut write_fds as *mut fd_set) };
nfds = std::cmp::max(nfds, fd + 1);
}
// select will block until some event happens
// on the fds or timeout triggers
let rv = unsafe {
select(
nfds,
&mut read_fds,
&mut write_fds,
std::ptr::null_mut(),
&mut tv,
)
};
// don't care for errors
if rv == -1 {
panic!("select()");
} else if rv == 0 {
debug!("timeout");
} else {
debug!("data available on {} fds", rv);
}
// check which fd it was and put appropriate future on run queue
for (fd, waker) in self.read.borrow().iter() {
let is_set = unsafe { FD_ISSET(*fd, &mut read_fds as *mut fd_set) };
debug!("fd#{} set (read)", fd);
if is_set {
waker.wake_by_ref();
}
}
// same for write
for (fd, waker) in self.write.borrow().iter() {
let is_set = unsafe { FD_ISSET(*fd, &mut write_fds as *mut fd_set) };
debug!("fd#{} set (write)", fd);
if is_set {
waker.wake_by_ref();
}
}
// now pop wakeup notifications from the run queue and poll associated futures
loop {
let w = self.run_queue.borrow_mut().pop_front();
match w {
Some(w) => {
debug!("polling task#{}", w.index);
let task = self.wait_queue.borrow_mut().remove(&w.index);
if let Some(mut task) = task {
// if a task is not ready put it back
if let Poll::Pending = task.poll(w.waker) {
self.wait_queue.borrow_mut().insert(w.index, task);
}
// otherwise just drop it
}
}
None => break,
}
}
// stop the loop if no more tasks
if self.wait_queue.borrow().is_empty() {
return;
}
}
}
}