2013-07-19 18:24:07 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
2013-07-19 18:03:02 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
2013-07-19 18:24:07 -05:00
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
2013-07-19 18:03:02 -05:00
|
|
|
//
|
2013-07-19 18:24:07 -05:00
|
|
|
// 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
|
2013-07-19 18:03:02 -05:00
|
|
|
// except according to those terms.
|
2013-07-19 18:24:07 -05:00
|
|
|
|
2013-07-19 18:03:02 -05:00
|
|
|
use option::{Option, Some, None};
|
|
|
|
use result::{Ok, Err};
|
|
|
|
use rt::io::{io_error};
|
|
|
|
use rt::rtio::{IoFactory, IoFactoryObject,
|
|
|
|
RtioTimer, RtioTimerObject};
|
|
|
|
use rt::local::Local;
|
|
|
|
|
|
|
|
pub struct Timer(~RtioTimerObject);
|
|
|
|
|
|
|
|
impl Timer {
|
2013-07-19 18:22:13 -05:00
|
|
|
fn new_on_rt(i: ~RtioTimerObject) -> Timer {
|
2013-07-19 18:03:02 -05:00
|
|
|
Timer(i)
|
|
|
|
}
|
|
|
|
|
2013-07-19 18:22:13 -05:00
|
|
|
pub fn new() -> Option<Timer> {
|
2013-07-19 18:03:02 -05:00
|
|
|
let timer = unsafe {
|
|
|
|
rtdebug!("Timer::init: borrowing io to init timer");
|
|
|
|
let io = Local::unsafe_borrow::<IoFactoryObject>();
|
|
|
|
rtdebug!("about to init timer");
|
|
|
|
(*io).timer_init()
|
|
|
|
};
|
|
|
|
match timer {
|
2013-07-19 18:22:13 -05:00
|
|
|
Ok(t) => Some(Timer::new_on_rt(t)),
|
2013-07-19 18:03:02 -05:00
|
|
|
Err(ioerr) => {
|
|
|
|
rtdebug!("Timer::init: failed to init: %?", ioerr);
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RtioTimer for Timer {
|
|
|
|
fn sleep(&self, msecs: u64) {
|
|
|
|
(**self).sleep(msecs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use rt::test::*;
|
|
|
|
use option::{Some, None};
|
|
|
|
#[test]
|
|
|
|
fn test_io_timer_sleep_simple() {
|
|
|
|
do run_in_newsched_task {
|
2013-07-19 18:22:13 -05:00
|
|
|
let timer = Timer::new();
|
2013-07-19 18:03:02 -05:00
|
|
|
match timer {
|
|
|
|
Some(t) => t.sleep(1),
|
|
|
|
None => assert!(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|