use constants;
use mio;
use std::thread;
pub type Poll = mio::Poll;
pub type Events = mio::Events;
pub struct ThreadHandle {
pub handle: thread::JoinHandle<()>,
readiness: mio::SetReadiness,
}
pub trait Stoppable {
fn join(self) -> ();
fn shutdown(self) -> ();
}
impl Stoppable for ThreadHandle {
fn join(self) {
self.handle.join().expect("Failed to join child thread!");
}
fn shutdown(self) {
self.readiness
.set_readiness(mio::Ready::readable())
.expect("Failed to notify child thread of shutdown!");
self.join();
}
}
pub fn spawn<F>(f: F) -> ThreadHandle
where
F: Send + 'static + FnOnce(mio::Poll) -> (),
{
let poller = mio::Poll::new().unwrap();
let (registration, readiness) = mio::Registration::new2();
ThreadHandle {
readiness: readiness,
handle: thread::spawn(move || {
poller
.register(
®istration,
constants::SYSTEM,
mio::Ready::readable(),
mio::PollOpt::edge(),
)
.expect("Failed to register system pipe");
f(poller);
}),
}
}