tokio/src/reactor/global.rs
Alex Crichton 32f2750c2d 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.
2017-12-11 17:26:39 -06:00

44 lines
929 B
Rust

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);
}
}