]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/io/timer.rs
std: make check appeasement
[rust.git] / src / libstd / rt / io / timer.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use option::{Option, Some, None};
12 use result::{Ok, Err};
13 use rt::io::{io_error};
14 use rt::rtio::{IoFactory, IoFactoryObject,
15                RtioTimer, RtioTimerObject};
16 use rt::local::Local;
17
18 pub struct Timer(~RtioTimerObject);
19
20 impl Timer {
21     fn new_on_rt(i: ~RtioTimerObject) -> Timer {
22         Timer(i)
23     }
24
25     pub fn new() -> Option<Timer> {
26         let timer = unsafe {
27             rtdebug!("Timer::init: borrowing io to init timer");
28             let io = Local::unsafe_borrow::<IoFactoryObject>();
29             rtdebug!("about to init timer");
30             (*io).timer_init()
31         };
32         match timer {
33             Ok(t) => Some(Timer::new_on_rt(t)),
34             Err(ioerr) => {
35                 rtdebug!("Timer::init: failed to init: %?", ioerr);
36                 io_error::cond.raise(ioerr);
37                 None
38             }
39         }
40     }
41 }
42
43 impl RtioTimer for Timer {
44     fn sleep(&self, msecs: u64) {
45         (**self).sleep(msecs);
46     }
47 }
48
49 #[cfg(test)]
50 mod test {
51     use super::*;
52     use rt::test::*;
53     use option::{Some, None};
54     #[test]
55     fn test_io_timer_sleep_simple() {
56         do run_in_newsched_task {
57             let timer = Timer::new();
58             match timer {
59                 Some(t) => t.sleep(1),
60                 None => assert!(false)
61             }
62         }
63     }
64 }