about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/libstd/rt/io/mod.rs4
-rw-r--r--src/libstd/rt/io/timer.rs63
2 files changed, 67 insertions, 0 deletions
diff --git a/src/libstd/rt/io/mod.rs b/src/libstd/rt/io/mod.rs
index 0ec51a3aa94..e261b3a3c57 100644
--- a/src/libstd/rt/io/mod.rs
+++ b/src/libstd/rt/io/mod.rs
@@ -252,6 +252,7 @@ pub use self::stdio::print;
 pub use self::stdio::println;
 
 pub use self::file::FileStream;
+pub use self::timer::Timer;
 pub use self::net::ip::IpAddr;
 pub use self::net::tcp::TcpListener;
 pub use self::net::tcp::TcpStream;
@@ -296,6 +297,9 @@ mod extensions;
 /// Non-I/O things needed by the I/O module
 mod support;
 
+/// Basic Timer
+mod timer;
+
 /// Thread-blocking implementations
 pub mod native {
     /// Posix file I/O
diff --git a/src/libstd/rt/io/timer.rs b/src/libstd/rt/io/timer.rs
new file mode 100644
index 00000000000..1a5fc66f183
--- /dev/null
+++ b/src/libstd/rt/io/timer.rs
@@ -0,0 +1,63 @@
+// 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.
+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 {
+    fn new(i: ~RtioTimerObject) -> Timer {
+        Timer(i)
+    }
+
+    pub fn init() -> Option<Timer> {
+        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 {
+            Ok(t) => Some(Timer::new(t)),
+            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 {
+            let timer = Timer::init();
+            match timer {
+                Some(t) => t.sleep(1),
+                None => assert!(false)
+            }
+        }
+    }
+}
\ No newline at end of file