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
|
|
|
pub fn new() -> Option<Timer> {
|
2013-07-19 18:03:02 -05:00
|
|
|
let timer = unsafe {
|
|
|
|
rtdebug!("Timer::init: borrowing io to init timer");
|
2013-08-08 13:38:10 -05:00
|
|
|
let io: *mut IoFactoryObject = Local::unsafe_borrow();
|
2013-07-19 18:03:02 -05:00
|
|
|
rtdebug!("about to init timer");
|
|
|
|
(*io).timer_init()
|
|
|
|
};
|
|
|
|
match timer {
|
2013-08-20 11:53:02 -05:00
|
|
|
Ok(t) => Some(Timer(t)),
|
2013-07-19 18:03:02 -05:00
|
|
|
Err(ioerr) => {
|
|
|
|
rtdebug!("Timer::init: failed to init: %?", ioerr);
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:53:02 -05:00
|
|
|
pub fn sleep(&mut self, msecs: u64) {
|
2013-07-19 18:03:02 -05:00
|
|
|
(**self).sleep(msecs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use rt::test::*;
|
|
|
|
#[test]
|
|
|
|
fn test_io_timer_sleep_simple() {
|
2013-09-04 20:09:03 -05:00
|
|
|
do run_in_mt_newsched_task {
|
2013-07-19 18:22:13 -05:00
|
|
|
let timer = Timer::new();
|
2013-08-08 20:58:18 -05:00
|
|
|
do timer.map_move |mut t| { t.sleep(1) };
|
2013-07-19 18:03:02 -05:00
|
|
|
}
|
|
|
|
}
|
2013-08-08 20:58:18 -05:00
|
|
|
}
|