1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-10 16:11:13 -05:00

Clean up Isolate::event_loop with utility function.

This commit is contained in:
Ryan Dahl 2018-10-09 13:24:08 -04:00
parent 8ada28716a
commit d26792d926

View file

@ -171,31 +171,11 @@ impl Isolate {
pub fn event_loop(&mut self) {
// Main thread event loop.
while !self.is_idle() {
// Ideally, mpsc::Receiver would have a receive method that takes a optional
// timeout. But it doesn't so we need all this duplicate code.
match self.timeout_due {
Some(due) => {
// Subtracting two Instants causes a panic if the resulting duration
// would become negative. Avoid this.
let now = Instant::now();
let timeout = if due > now {
due - now
} else {
Duration::new(0, 0)
};
// TODO: use recv_deadline() instead of recv_timeout() when this
// feature becomes stable/available.
match self.rx.recv_timeout(timeout) {
Ok((req_id, buf)) => self.complete_op(req_id, buf),
Err(mpsc::RecvTimeoutError::Timeout) => self.timeout(),
Err(e) => panic!("mpsc::Receiver::recv_timeout() failed: {:?}", e),
}
}
None => match self.rx.recv() {
Ok((req_id, buf)) => self.complete_op(req_id, buf),
Err(e) => panic!("mpsc::Receiver::recv() failed: {:?}", e),
},
};
match recv_deadline(&self.rx, self.timeout_due) {
Ok((req_id, buf)) => self.complete_op(req_id, buf),
Err(mpsc::RecvTimeoutError::Timeout) => self.timeout(),
Err(e) => panic!("recv_deadline() failed: {:?}", e),
}
}
}
@ -285,6 +265,28 @@ extern "C" fn pre_dispatch(
}
}
fn recv_deadline<T>(
rx: &mpsc::Receiver<T>,
maybe_due: Option<Instant>,
) -> Result<T, mpsc::RecvTimeoutError> {
match maybe_due {
None => rx.recv().map_err(|e| e.into()),
Some(due) => {
// Subtracting two Instants causes a panic if the resulting duration
// would become negative. Avoid this.
let now = Instant::now();
let timeout = if due > now {
due - now
} else {
Duration::new(0, 0)
};
// TODO: use recv_deadline() instead of recv_timeout() when this
// feature becomes stable/available.
rx.recv_timeout(timeout)
}
}
}
#[cfg(test)]
mod tests {
use super::*;