2013-10-22 17:13:18 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-11-04 23:08:25 -06:00
|
|
|
use std::comm::{oneshot, stream, PortOne, ChanOne, SendDeferred};
|
2013-10-22 17:13:18 -05:00
|
|
|
use std::libc::c_int;
|
2013-11-01 11:36:21 -05:00
|
|
|
use std::rt::BlockedTask;
|
|
|
|
use std::rt::local::Local;
|
|
|
|
use std::rt::rtio::RtioTimer;
|
|
|
|
use std::rt::sched::{Scheduler, SchedHandle};
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
use uvll;
|
2013-11-05 13:29:45 -06:00
|
|
|
use super::{Loop, UvHandle};
|
2013-11-01 11:36:21 -05:00
|
|
|
use uvio::HomingIO;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-01 11:36:21 -05:00
|
|
|
pub struct TimerWatcher {
|
|
|
|
handle: *uvll::uv_timer_t,
|
|
|
|
home: SchedHandle,
|
|
|
|
action: Option<NextAction>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum NextAction {
|
|
|
|
WakeTask(BlockedTask),
|
|
|
|
SendOnce(ChanOne<()>),
|
|
|
|
SendMany(Chan<()>),
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
impl TimerWatcher {
|
2013-11-01 11:36:21 -05:00
|
|
|
pub fn new(loop_: &mut Loop) -> ~TimerWatcher {
|
|
|
|
let handle = UvHandle::alloc(None::<TimerWatcher>, uvll::UV_TIMER);
|
|
|
|
assert_eq!(unsafe {
|
2013-11-05 13:29:45 -06:00
|
|
|
uvll::uv_timer_init(loop_.handle, handle)
|
2013-11-01 11:36:21 -05:00
|
|
|
}, 0);
|
|
|
|
let me = ~TimerWatcher {
|
|
|
|
handle: handle,
|
|
|
|
action: None,
|
|
|
|
home: get_handle_to_current_scheduler!(),
|
|
|
|
};
|
|
|
|
return me.install();
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2013-11-01 11:36:21 -05:00
|
|
|
fn start(&mut self, msecs: u64, period: u64) {
|
|
|
|
assert_eq!(unsafe {
|
2013-11-04 16:03:32 -06:00
|
|
|
uvll::uv_timer_start(self.handle, timer_cb, msecs, period)
|
2013-11-01 11:36:21 -05:00
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(&mut self) {
|
2013-11-04 16:03:32 -06:00
|
|
|
assert_eq!(unsafe { uvll::uv_timer_stop(self.handle) }, 0)
|
2013-11-01 11:36:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HomingIO for TimerWatcher {
|
|
|
|
fn home<'r>(&'r mut self) -> &'r mut SchedHandle { &mut self.home }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UvHandle<uvll::uv_timer_t> for TimerWatcher {
|
|
|
|
fn uv_handle(&self) -> *uvll::uv_timer_t { self.handle }
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-01 11:36:21 -05:00
|
|
|
impl RtioTimer for TimerWatcher {
|
|
|
|
fn sleep(&mut self, msecs: u64) {
|
2013-11-06 01:29:11 -06:00
|
|
|
let (_m, sched) = self.fire_homing_missile_sched();
|
2013-11-04 16:03:32 -06:00
|
|
|
do sched.deschedule_running_task_and_then |_sched, task| {
|
|
|
|
self.action = Some(WakeTask(task));
|
|
|
|
self.start(msecs, 0);
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
2013-11-04 16:03:32 -06:00
|
|
|
self.stop();
|
2013-11-01 11:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn oneshot(&mut self, msecs: u64) -> PortOne<()> {
|
|
|
|
let (port, chan) = oneshot();
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-06 01:29:11 -06:00
|
|
|
let _m = self.fire_homing_missile();
|
2013-11-05 02:27:41 -06:00
|
|
|
self.action = Some(SendOnce(chan));
|
2013-11-04 16:03:32 -06:00
|
|
|
self.start(msecs, 0);
|
2013-11-01 11:36:21 -05:00
|
|
|
|
|
|
|
return port;
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2013-11-01 11:36:21 -05:00
|
|
|
fn period(&mut self, msecs: u64) -> Port<()> {
|
|
|
|
let (port, chan) = stream();
|
|
|
|
|
2013-11-06 01:29:11 -06:00
|
|
|
let _m = self.fire_homing_missile();
|
2013-11-05 02:27:41 -06:00
|
|
|
self.action = Some(SendMany(chan));
|
2013-11-04 16:03:32 -06:00
|
|
|
self.start(msecs, msecs);
|
2013-11-01 11:36:21 -05:00
|
|
|
|
|
|
|
return port;
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-01 11:36:21 -05:00
|
|
|
extern fn timer_cb(handle: *uvll::uv_timer_t, _status: c_int) {
|
2013-11-01 13:13:22 -05:00
|
|
|
let timer: &mut TimerWatcher = unsafe { UvHandle::from_uv_handle(&handle) };
|
2013-11-01 11:36:21 -05:00
|
|
|
|
2013-11-01 12:26:43 -05:00
|
|
|
match timer.action.take_unwrap() {
|
2013-11-01 11:36:21 -05:00
|
|
|
WakeTask(task) => {
|
|
|
|
let sched: ~Scheduler = Local::take();
|
|
|
|
sched.resume_blocked_task_immediately(task);
|
|
|
|
}
|
2013-11-04 23:08:25 -06:00
|
|
|
SendOnce(chan) => chan.send_deferred(()),
|
2013-11-01 11:36:21 -05:00
|
|
|
SendMany(chan) => {
|
2013-11-04 23:08:25 -06:00
|
|
|
chan.send_deferred(());
|
2013-11-01 12:26:43 -05:00
|
|
|
timer.action = Some(SendMany(chan));
|
2013-11-01 11:36:21 -05:00
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
2013-11-01 11:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TimerWatcher {
|
|
|
|
fn drop(&mut self) {
|
2013-11-06 01:29:11 -06:00
|
|
|
let _m = self.fire_homing_missile();
|
2013-11-04 16:03:32 -06:00
|
|
|
self.action = None;
|
|
|
|
self.stop();
|
|
|
|
self.close_async_();
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2013-11-06 13:03:11 -06:00
|
|
|
use std::rt::rtio::RtioTimer;
|
|
|
|
use super::super::run_uv_loop;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-11-06 13:03:11 -06:00
|
|
|
fn oneshot() {
|
|
|
|
do run_uv_loop |l| {
|
|
|
|
let mut timer = TimerWatcher::new(l);
|
|
|
|
let port = timer.oneshot(1);
|
|
|
|
port.recv();
|
|
|
|
let port = timer.oneshot(1);
|
|
|
|
port.recv();
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-11-06 13:03:11 -06:00
|
|
|
fn override() {
|
|
|
|
do run_uv_loop |l| {
|
|
|
|
let mut timer = TimerWatcher::new(l);
|
|
|
|
let oport = timer.oneshot(1);
|
|
|
|
let pport = timer.period(1);
|
|
|
|
timer.sleep(1);
|
|
|
|
assert_eq!(oport.try_recv(), None);
|
|
|
|
assert_eq!(pport.try_recv(), None);
|
|
|
|
timer.oneshot(1).recv();
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-11-06 13:03:11 -06:00
|
|
|
fn period() {
|
|
|
|
do run_uv_loop |l| {
|
|
|
|
let mut timer = TimerWatcher::new(l);
|
|
|
|
let port = timer.period(1);
|
|
|
|
port.recv();
|
|
|
|
port.recv();
|
|
|
|
let port = timer.period(1);
|
|
|
|
port.recv();
|
|
|
|
port.recv();
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 13:03:11 -06:00
|
|
|
#[test]
|
|
|
|
fn sleep() {
|
|
|
|
do run_uv_loop |l| {
|
|
|
|
let mut timer = TimerWatcher::new(l);
|
|
|
|
timer.sleep(1);
|
|
|
|
timer.sleep(1);
|
|
|
|
}
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|