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
use std::cell::RefCell;
use std::io;
use std::rc::Rc;
use std::time::Duration;

use mio::{Events, Poll};

use list::SourceList;
use sources::{EventSource, Idle, Source};

/// An handle to an event loop
///
/// This handle allows you to insert new sources and idles in this event loop,
/// it can be cloned, and it is possible to insert new sources from within a source
/// callback.
#[derive(Clone)]
pub struct LoopHandle {
    poll: Rc<Poll>,
    list: Rc<RefCell<SourceList>>,
    idles: Rc<RefCell<Vec<Rc<RefCell<Option<Box<FnMut()>>>>>>>,
}

impl LoopHandle {
    /// Insert an new event source in the loop
    ///
    /// The provided callback will be called during the dispatching cycles whenever the
    /// associated source generates events, see `EventLoop::dispatch(..)` for details.
    pub fn insert_source<E: EventSource, F: FnMut(E::Event) + 'static>(
        &self,
        source: E,
        callback: F,
    ) -> io::Result<Source<E>> {
        let dispatcher = source.make_dispatcher(callback);

        let token = self.list.borrow_mut().add_source(dispatcher);

        let interest = source.interest();
        let opt = source.pollopts();

        self.poll.register(&source, token, interest, opt)?;

        Ok(Source {
            source,
            poll: self.poll.clone(),
            list: self.list.clone(),
            token,
        })
    }

    /// Insert an idle callback
    ///
    /// This callback will be called during a dispatching cycle when the event loop has
    /// finished processing all pending events from the sources and becomes idle.
    pub fn insert_idle<F: FnMut() + 'static>(&self, callback: F) -> Idle {
        let callback = Rc::new(RefCell::new(Some(Box::new(callback) as Box<FnMut()>)));
        self.idles.borrow_mut().push(callback.clone());
        Idle { callback }
    }
}

/// An event loop
///
/// This loop can host several event sources, that can be dynamically added or removed.
pub struct EventLoop {
    handle: LoopHandle,
    events_buffer: Events,
}

impl EventLoop {
    /// Create a new event loop
    ///
    /// It is backed by an `mio` provided machinnery, and will fail if the `mio`
    /// initialization fails.
    pub fn new() -> io::Result<EventLoop> {
        Ok(EventLoop {
            handle: LoopHandle {
                poll: Rc::new(Poll::new()?),
                list: Rc::new(RefCell::new(SourceList::new())),
                idles: Rc::new(RefCell::new(Vec::new())),
            },
            events_buffer: Events::with_capacity(32),
        })
    }

    /// Retrieve a loop handle
    pub fn handle(&self) -> LoopHandle {
        self.handle.clone()
    }

    fn dispatch_events(&mut self, timeout: Option<Duration>) -> io::Result<()> {
        self.events_buffer.clear();
        self.handle.poll.poll(&mut self.events_buffer, timeout)?;

        loop {
            if self.events_buffer.is_empty() {
                break;
            }

            for event in &self.events_buffer {
                if let Some(dispatcher) = self.handle.list.borrow().get_dispatcher(event.token()) {
                    dispatcher.borrow_mut().ready(event.readiness());
                }
            }

            // process remaining events if any
            self.events_buffer.clear();
            self.handle
                .poll
                .poll(&mut self.events_buffer, Some(Duration::from_millis(0)))?;
        }

        Ok(())
    }

    fn dispatch_idles(&mut self) {
        let idles = ::std::mem::replace(&mut *self.handle.idles.borrow_mut(), Vec::new());
        for idle in idles {
            if let Some(ref mut callback) = *idle.borrow_mut() {
                callback();
            }
        }
    }

    /// Dispatch pending events to their callbacks
    ///
    /// Some source have events available, their callbacks will be immediatly called.
    /// Otherwise this will wait until an event is receive or the provided `timeout`
    /// is reached. If `timeout` is `None`, it will wait without a duration limit.
    ///
    /// Once pending events have been processed or the timeout is reached, all pending
    /// idle callbacks will be fired before this method returns.
    pub fn dispatch(&mut self, timeout: Option<Duration>) -> io::Result<()> {
        self.dispatch_events(timeout)?;

        self.dispatch_idles();

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::cell::Cell;
    use std::rc::Rc;
    use std::time::Duration;

    use super::EventLoop;

    #[test]
    fn dispatch_idle() {
        let mut event_loop = EventLoop::new().unwrap();

        let dispatched = Rc::new(Cell::new(false));

        let impl_dispatched = dispatched.clone();
        event_loop
            .handle()
            .insert_idle(move || impl_dispatched.set(true));

        event_loop.dispatch(Some(Duration::from_millis(0))).unwrap();

        assert!(dispatched.get());
    }

    #[test]
    fn cancel_idle() {
        let mut event_loop = EventLoop::new().unwrap();

        let dispatched = Rc::new(Cell::new(false));

        let impl_dispatched = dispatched.clone();
        let idle = event_loop
            .handle()
            .insert_idle(move || impl_dispatched.set(true));

        idle.cancel();

        event_loop.dispatch(Some(Duration::from_millis(0))).unwrap();

        assert!(!dispatched.get());
    }
}