1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00

timers: remove the old timer implementation

Fixes #878.
This commit is contained in:
Bert Belder 2018-10-02 23:21:56 -07:00
parent 99e2c42d16
commit 4eeda9ea27
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
3 changed files with 1 additions and 115 deletions

View file

@ -13,7 +13,6 @@ use msg;
use flatbuffers::FlatBufferBuilder;
use futures;
use futures::future::poll_fn;
use futures::sync::oneshot;
use futures::Poll;
use hyper;
use hyper::rt::{Future, Stream};
@ -30,7 +29,6 @@ use std::sync::Arc;
use std::time::UNIX_EPOCH;
use std::time::{Duration, Instant};
use tokio;
use tokio::timer::Delay;
use tokio_io;
use tokio_threadpool;
@ -71,8 +69,6 @@ pub fn msg_from_js(
msg::Any::CodeCache => handle_code_cache,
msg::Any::Environ => handle_env,
msg::Any::FetchReq => handle_fetch_req,
msg::Any::TimerStart => handle_timer_start,
msg::Any::TimerClear => handle_timer_clear,
msg::Any::MakeTempDir => handle_make_temp_dir,
msg::Any::Mkdir => handle_mkdir,
msg::Any::Open => handle_open,
@ -446,30 +442,6 @@ fn handle_fetch_req(
Box::new(future)
}
fn set_timeout<F>(
cb: F,
delay: u32,
) -> (
impl Future<Item = (), Error = ()>,
futures::sync::oneshot::Sender<()>,
)
where
F: FnOnce() -> (),
{
let (cancel_tx, cancel_rx) = oneshot::channel::<()>();
let when = Instant::now() + Duration::from_millis(delay.into());
let delay_task = Delay::new(when)
.map_err(|e| panic!("timer failed; err={:?}", e))
.and_then(|_| {
cb();
Ok(())
}).select(cancel_rx)
.map(|_| ())
.map_err(|_| ());
(delay_task, cancel_tx)
}
// This is just type conversion. Implement From trait?
// See https://github.com/tokio-rs/tokio/blob/ffd73a64e7ec497622b7f939e38017afe7124dc4/tokio-fs/src/lib.rs#L76-L85
fn convert_blocking<F>(f: F) -> Poll<Buf, DenoError>
@ -865,72 +837,6 @@ fn handle_write_file(
})
}
fn remove_timer(state: Arc<IsolateState>, timer_id: u32) {
let mut timers = state.timers.lock().unwrap();
timers.remove(&timer_id);
}
// Prototype: https://github.com/ry/isolate/blob/golang/timers.go#L25-L39
fn handle_timer_start(
state: Arc<IsolateState>,
base: &msg::Base,
data: &'static mut [u8],
) -> Box<Op> {
assert_eq!(data.len(), 0);
debug!("handle_timer_start");
let msg = base.msg_as_timer_start().unwrap();
let cmd_id = base.cmd_id();
let timer_id = msg.id();
let delay = msg.delay();
let config2 = state.clone();
let future = {
let (delay_task, cancel_delay) = set_timeout(
move || {
remove_timer(config2, timer_id);
},
delay,
);
let mut timers = state.timers.lock().unwrap();
timers.insert(timer_id, cancel_delay);
delay_task
};
let r = Box::new(future.then(move |result| {
let builder = &mut FlatBufferBuilder::new();
let msg = msg::TimerReady::create(
builder,
&msg::TimerReadyArgs {
id: timer_id,
canceled: result.is_err(),
..Default::default()
},
);
Ok(serialize_response(
cmd_id,
builder,
msg::BaseArgs {
msg: Some(msg.as_union_value()),
msg_type: msg::Any::TimerReady,
..Default::default()
},
))
}));
r
}
// Prototype: https://github.com/ry/isolate/blob/golang/timers.go#L40-L43
fn handle_timer_clear(
state: Arc<IsolateState>,
base: &msg::Base,
data: &'static mut [u8],
) -> Box<Op> {
assert_eq!(data.len(), 0);
let msg = base.msg_as_timer_clear().unwrap();
debug!("handle_timer_clear");
remove_timer(state, msg.id());
ok_future(empty_buf())
}
fn handle_rename(
state: Arc<IsolateState>,
base: &msg::Base,

View file

@ -9,11 +9,9 @@ use errors::DenoError;
use flags;
use libdeno;
use futures;
use futures::Future;
use libc::c_void;
use std;
use std::collections::HashMap;
use std::ffi::CStr;
use std::ffi::CString;
use std::sync::mpsc;
@ -54,7 +52,6 @@ pub struct Isolate {
// needs to be accessed outside the main V8 thread should be inside IsolateState.
pub struct IsolateState {
pub dir: deno_dir::DenoDir,
pub timers: Mutex<HashMap<u32, futures::sync::oneshot::Sender<()>>>,
pub argv: Vec<String>,
pub flags: flags::DenoFlags,
tx: Mutex<Option<mpsc::Sender<(i32, Buf)>>>,
@ -92,7 +89,6 @@ impl Isolate {
timeout_due: None,
state: Arc::new(IsolateState {
dir: deno_dir::DenoDir::new(flags.reload, None).unwrap(),
timers: Mutex::new(HashMap::new()),
argv: argv_rest,
flags,
tx: Mutex::new(Some(tx)),
@ -280,6 +276,7 @@ extern "C" fn pre_dispatch(
#[cfg(test)]
mod tests {
use super::*;
use futures;
#[test]
fn test_c_to_rust() {

View file

@ -6,9 +6,6 @@ union Any {
CodeCache,
SetTimeout,
Exit,
TimerStart,
TimerReady,
TimerClear,
Environ,
EnvironRes,
FetchReq,
@ -135,20 +132,6 @@ table Exit {
code: int;
}
table TimerStart {
id: uint;
delay: uint;
}
table TimerReady {
id: uint;
canceled: bool;
}
table TimerClear {
id: uint;
}
table Environ {}
table SetEnv {