mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-28 12:10:37 +00:00

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.
44 lines
929 B
Rust
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);
|
|
}
|
|
}
|