Skip to content

Commit 5da29e3

Browse files
committed
std: add rt::io::Timer
1 parent 921d991 commit 5da29e3

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/libstd/rt/io/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ pub use self::stdio::print;
252252
pub use self::stdio::println;
253253

254254
pub use self::file::FileStream;
255+
pub use self::timer::Timer;
255256
pub use self::net::ip::IpAddr;
256257
pub use self::net::tcp::TcpListener;
257258
pub use self::net::tcp::TcpStream;
@@ -296,6 +297,9 @@ mod extensions;
296297
/// Non-I/O things needed by the I/O module
297298
mod support;
298299

300+
/// Basic Timer
301+
mod timer;
302+
299303
/// Thread-blocking implementations
300304
pub mod native {
301305
/// Posix file I/O

src/libstd/rt/io/timer.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
use option::{Option, Some, None};
11+
use result::{Ok, Err};
12+
use rt::io::{io_error};
13+
use rt::rtio::{IoFactory, IoFactoryObject,
14+
RtioTimer, RtioTimerObject};
15+
use rt::local::Local;
16+
17+
pub struct Timer(~RtioTimerObject);
18+
19+
impl Timer {
20+
fn new(i: ~RtioTimerObject) -> Timer {
21+
Timer(i)
22+
}
23+
24+
pub fn init() -> Option<Timer> {
25+
let timer = unsafe {
26+
rtdebug!("Timer::init: borrowing io to init timer");
27+
let io = Local::unsafe_borrow::<IoFactoryObject>();
28+
rtdebug!("about to init timer");
29+
(*io).timer_init()
30+
};
31+
match timer {
32+
Ok(t) => Some(Timer::new(t)),
33+
Err(ioerr) => {
34+
rtdebug!("Timer::init: failed to init: %?", ioerr);
35+
io_error::cond.raise(ioerr);
36+
None
37+
}
38+
}
39+
}
40+
}
41+
42+
impl RtioTimer for Timer {
43+
fn sleep(&self, msecs: u64) {
44+
(**self).sleep(msecs);
45+
}
46+
}
47+
48+
#[cfg(test)]
49+
mod test {
50+
use super::*;
51+
use rt::test::*;
52+
use option::{Some, None};
53+
#[test]
54+
fn test_io_timer_sleep_simple() {
55+
do run_in_newsched_task {
56+
let timer = Timer::init();
57+
match timer {
58+
Some(t) => t.sleep(1),
59+
None => assert!(false)
60+
}
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)