-
Notifications
You must be signed in to change notification settings - Fork 714
/
event_loop.rs
366 lines (296 loc) · 10.6 KB
/
event_loop.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
use std::default::Default;
use std::time::duration::Duration;
use std::uint;
use error::{MioResult, MioError};
use handler::Handler;
use io::IoHandle;
use net::{Socket, SockAddr};
use notify::Notify;
use os;
use poll::{Poll, IoEvent};
use timer::{Timer, Timeout, TimerResult};
use token::Token;
/// A lightweight event loop.
///
/// TODO:
/// - Enforce private tokens
#[deriving(Clone, Show)]
pub struct EventLoopConfig {
pub io_poll_timeout_ms: uint,
// == Notifications ==
pub notify_capacity: uint,
pub messages_per_tick: uint,
// == Timer ==
pub timer_tick_ms: u64,
pub timer_wheel_size: uint,
pub timer_capacity: uint,
}
impl Default for EventLoopConfig {
fn default() -> EventLoopConfig {
EventLoopConfig {
io_poll_timeout_ms: 1_000,
notify_capacity: 1_024,
messages_per_tick: 64,
timer_tick_ms: 100,
timer_wheel_size: 1_024,
timer_capacity: 65_536,
}
}
}
pub struct EventLoop<T, M: Send> {
run: bool,
poll: Poll,
timer: Timer<T>,
notify: Notify<M>,
config: EventLoopConfig,
}
// Token used to represent notifications
const NOTIFY: Token = Token(uint::MAX);
impl<T, M: Send> EventLoop<T, M> {
/// Initializes a new event loop. The event loop will not be running yet.
pub fn new() -> MioResult<EventLoop<T, M>> {
EventLoop::configured(Default::default())
}
pub fn configured(config: EventLoopConfig) -> MioResult<EventLoop<T, M>> {
// Create the IO poller
let mut poll = try!(Poll::new());
// Create the timer
let mut timer = Timer::new(
config.timer_tick_ms,
config.timer_wheel_size,
config.timer_capacity);
// Create cross thread notification queue
let notify = try!(Notify::with_capacity(config.notify_capacity));
// Register the notification wakeup FD with the IO poller
try!(poll.register(¬ify, NOTIFY));
// Set the timer's starting time reference point
timer.setup();
Ok(EventLoop {
run: true,
poll: poll,
timer: timer,
notify: notify,
config: config,
})
}
/// Returns a sender that allows sending messages to the event loop in a
/// thread-safe way, waking up the event loop if needed.
pub fn channel(&self) -> EventLoopSender<M> {
EventLoopSender::new(self.notify.clone())
}
/// After the requested time interval, the handler's `timeout` function
/// will be called with the supplied token.
pub fn timeout(&mut self, token: T, delay: Duration) -> TimerResult<Timeout> {
self.timer.timeout(token, delay)
}
/// If the supplied timeout has not been triggered, cancel it such that it
/// will not be triggered in the future.
pub fn clear_timeout(&mut self, timeout: Timeout) -> bool {
self.timer.clear(timeout)
}
/// Tells the event loop to exit after it is done handling all events in the
/// current iteration.
pub fn shutdown(&mut self) {
self.run = false;
}
/// Registers an IO handle with the event loop.
pub fn register<H: IoHandle>(&mut self, io: &H, token: Token) -> MioResult<()> {
self.poll.register(io, token)
}
/// Connects the socket to the specified address. When the operation
/// completes, the handler will be notified with the supplied token.
///
/// The goal of this method is to ensure that the event loop will always
/// notify about the connection, even if the connection happens
/// immediately. Otherwise, every consumer of the event loop would have
/// to worry about possibly-immediate connection.
pub fn connect<S: Socket>(&mut self, io: &S, addr: &SockAddr, token: Token) -> MioResult<()> {
debug!("socket connect; addr={}", addr);
// Attempt establishing the context. This may not complete immediately.
if try!(os::connect(io.desc(), addr)) {
// On some OSs, connecting to localhost succeeds immediately. In
// this case, queue the writable callback for execution during the
// next event loop tick.
debug!("socket connected immediately; addr={}", addr);
}
// Register interest with socket on the event loop
try!(self.register(io, token));
Ok(())
}
/// Keep spinning the event loop indefinitely, and notify the handler whenever
/// any of the registered handles are ready.
pub fn run<H: Handler<T, M>>(&mut self, mut handler: H) -> EventLoopResult<H> {
self.run = true;
while self.run {
// Execute ticks as long as the event loop is running
match self.tick(&mut handler) {
Err(e) => return Err(EventLoopError::new(handler, e)),
_ => {}
}
}
Ok(handler)
}
/// Spin the event loop once, with a timeout of one second, and notify the
/// handler if any of the registered handles become ready during that
/// time.
pub fn run_once<H: Handler<T, M>>(&mut self, mut handler: H) -> EventLoopResult<H> {
// Execute a single tick
match self.tick(&mut handler) {
Err(e) => return Err(EventLoopError::new(handler, e)),
_ => {}
}
Ok(handler)
}
// Executes a single run of the event loop loop
fn tick<H: Handler<T, M>>(&mut self, handler: &mut H) -> MioResult<()> {
let mut messages;
let mut pending;
debug!("event loop tick");
// Check the notify channel for any pending messages. If there are any,
// avoid blocking when polling for IO events. Messages will be
// processed after IO events.
messages = self.notify.check(self.config.messages_per_tick, true);
pending = messages > 0;
// Check the registered IO handles for any new events. Each poll
// is for one second, so a shutdown request can last as long as
// one second before it takes effect.
let events = try!(self.io_poll(pending));
if !pending {
// Indicate that the sleep period is over, also grab any additional
// messages
let remaining = self.config.messages_per_tick - messages;
messages += self.notify.check(remaining, false);
}
self.io_process(handler, events);
self.notify(handler, messages);
self.timer_process(handler);
Ok(())
}
#[inline]
fn io_poll(&mut self, immediate: bool) -> MioResult<uint> {
if immediate {
self.poll.poll(0)
} else {
let mut sleep = self.timer.next_tick_in_ms() as uint;
if sleep > self.config.io_poll_timeout_ms {
sleep = self.config.io_poll_timeout_ms;
}
self.poll.poll(sleep)
}
}
// Process IO events that have been previously polled
fn io_process<H: Handler<T, M>>(&mut self, handler: &mut H, cnt: uint) {
let mut i = 0u;
// Iterate over the notifications. Each event provides the token
// it was registered with (which usually represents, at least, the
// handle that the event is about) as well as information about
// what kind of event occurred (readable, writable, signal, etc.)
while i < cnt {
let evt = self.poll.event(i);
debug!("event={}", evt);
match evt.token() {
NOTIFY => self.notify.cleanup(),
_ => self.io_event(handler, evt)
}
i += 1;
}
}
fn io_event<H: Handler<T, M>>(&mut self, handler: &mut H, evt: IoEvent) {
let tok = evt.token();
if evt.is_readable() {
handler.readable(self, tok, evt.read_hint());
}
if evt.is_writable() {
handler.writable(self, tok);
}
if evt.is_error() {
println!(" + ERROR");
}
}
fn notify<H: Handler<T, M>>(&mut self, handler: &mut H, mut cnt: uint) {
while cnt > 0 {
let msg = self.notify.poll()
.expect("[BUG] at this point there should always be a message");
handler.notify(self, msg);
cnt -= 1;
}
}
fn timer_process<H: Handler<T, M>>(&mut self, handler: &mut H) {
let now = self.timer.now();
loop {
match self.timer.tick_to(now) {
Some(t) => handler.timeout(self, t),
_ => return
}
}
}
}
#[deriving(Clone)]
pub struct EventLoopSender<M: Send> {
notify: Notify<M>
}
impl<M: Send> EventLoopSender<M> {
fn new(notify: Notify<M>) -> EventLoopSender<M> {
EventLoopSender { notify: notify }
}
pub fn send(&self, msg: M) -> Result<(), M> {
self.notify.notify(msg)
}
}
pub type EventLoopResult<H> = Result<H, EventLoopError<H>>;
pub struct EventLoopError<H> {
pub handler: H,
pub error: MioError
}
impl<H> EventLoopError<H> {
fn new(handler: H, error: MioError) -> EventLoopError<H> {
EventLoopError {
handler: handler,
error: error
}
}
}
#[cfg(test)]
mod tests {
use std::str;
use std::sync::Arc;
use std::sync::atomic::{AtomicInt, SeqCst};
use super::EventLoop;
use io::{IoWriter, IoReader};
use {io, buf, Buf, Handler, Token, ReadHint};
type TestEventLoop = EventLoop<uint, ()>;
struct Funtimes {
rcount: Arc<AtomicInt>,
wcount: Arc<AtomicInt>
}
impl Funtimes {
fn new(rcount: Arc<AtomicInt>, wcount: Arc<AtomicInt>) -> Funtimes {
Funtimes {
rcount: rcount,
wcount: wcount
}
}
}
impl Handler<uint, ()> for Funtimes {
fn readable(&mut self, _event_loop: &mut TestEventLoop, token: Token, _hint: ReadHint) {
(*self.rcount).fetch_add(1, SeqCst);
assert_eq!(token, Token(10));
}
}
#[test]
fn test_readable() {
let mut event_loop = EventLoop::new().ok().expect("Couldn't make event loop");
let (mut reader, mut writer) = io::pipe().unwrap();
let rcount = Arc::new(AtomicInt::new(0));
let wcount = Arc::new(AtomicInt::new(0));
let handler = Funtimes::new(rcount.clone(), wcount.clone());
writer.write(&mut buf::wrap("hello".as_bytes())).unwrap();
event_loop.register(&reader, Token(10)).unwrap();
let _ = event_loop.run_once(handler);
let mut b = buf::ByteBuf::new(16);
assert_eq!((*rcount).load(SeqCst), 1);
reader.read(&mut b).unwrap();
b.flip();
assert_eq!(str::from_utf8(b.bytes()).unwrap(), "hello");
}
}