Start adding a global event loop

This commit starts to add support for a global event loop by adding a
`Handle::default` method and implementing it. Currently the support is quite
rudimentary and doesn't support features such as shutdown, overriding the return
value of `Handle::default`, etc. Those will come as future commits.
This commit is contained in:
Alex Crichton
2017-12-11 17:26:39 -06:00
committed by Carl Lerche
parent 23a0e990d2
commit 32f2750c2d
5 changed files with 193 additions and 9 deletions
+43
View File
@@ -0,0 +1,43 @@
use std::io;
use std::thread;
use reactor::{Reactor, Handle};
pub struct HelperThread {
thread: Option<thread::JoinHandle<()>>,
reactor: Handle,
}
impl HelperThread {
pub fn new() -> io::Result<HelperThread> {
let reactor = Reactor::new()?;
let reactor_handle = reactor.handle().clone();
let thread = thread::Builder::new().spawn(move || run(reactor))?;
Ok(HelperThread {
thread: Some(thread),
reactor: reactor_handle,
})
}
pub fn handle(&self) -> &Handle {
&self.reactor
}
pub fn forget(mut self) {
drop(self.thread.take());
}
}
impl Drop for HelperThread {
fn drop(&mut self) {
// TODO: kill the reactor thread and wait for it to exit, needs
// `Handle::wakeup` to be implemented in a future PR
}
}
fn run(mut reactor: Reactor) {
loop {
reactor.turn(None);
}
}