mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-28 00:00:11 +02:00
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:
committed by
Carl Lerche
parent
23a0e990d2
commit
32f2750c2d
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user