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.
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.
15 This module exposes the functionality to create timers, block the current task,
16 and create ports which will receive notifications after a period of time.
22 use std::rt::io::Timer;
24 let mut timer = Timer::new().unwrap();
25 timer.sleep(10); // block the task for awhile
27 let timeout = timer.oneshot(10);
29 timeout.recv(); // wait for the timeout to expire
31 let periodic = timer.periodic(10);
34 // this loop is only executed once every 10ms
41 use comm::{Port, PortOne};
42 use option::{Option, Some, None};
43 use result::{Ok, Err};
45 use rt::rtio::{IoFactory, RtioTimer, with_local_io};
51 /// Sleep the current task for `msecs` milliseconds.
52 pub fn sleep(msecs: u64) {
53 let mut timer = Timer::new().expect("timer::sleep: could not create a Timer");
59 /// Creates a new timer which can be used to put the current task to sleep
60 /// for a number of milliseconds, or to possibly create channels which will
61 /// get notified after an amount of time has passed.
62 pub fn new() -> Option<Timer> {
63 do with_local_io |io| {
64 match io.timer_init() {
65 Ok(t) => Some(Timer { obj: t }),
67 rtdebug!("Timer::init: failed to init: {:?}", ioerr);
68 io_error::cond.raise(ioerr);
76 /// Blocks the current task for `msecs` milliseconds.
78 /// Note that this function will cause any other ports for this timer to be
79 /// invalidated (the other end will be closed).
80 pub fn sleep(&mut self, msecs: u64) {
81 self.obj.sleep(msecs);
84 /// Creates a oneshot port which will have a notification sent when `msecs`
85 /// milliseconds has elapsed. This does *not* block the current task, but
86 /// instead returns immediately.
88 /// Note that this invalidates any previous port which has been created by
89 /// this timer, and that the returned port will be invalidated once the
90 /// timer is destroyed (when it falls out of scope).
91 pub fn oneshot(&mut self, msecs: u64) -> PortOne<()> {
92 self.obj.oneshot(msecs)
95 /// Creates a port which will have a continuous stream of notifications
96 /// being sent every `msecs` milliseconds. This does *not* block the
97 /// current task, but instead returns immediately. The first notification
98 /// will not be received immediately, but rather after `msec` milliseconds
101 /// Note that this invalidates any previous port which has been created by
102 /// this timer, and that the returned port will be invalidated once the
103 /// timer is destroyed (when it falls out of scope).
104 pub fn periodic(&mut self, msecs: u64) -> Port<()> {
105 self.obj.period(msecs)
118 fn test_io_timer_sleep_simple() {
119 do run_in_mt_newsched_task {
120 let mut timer = Timer::new().unwrap();
126 fn test_io_timer_sleep_oneshot() {
127 do run_in_mt_newsched_task {
128 let mut timer = Timer::new().unwrap();
129 timer.oneshot(1).recv();
134 fn test_io_timer_sleep_oneshot_forget() {
135 do run_in_mt_newsched_task {
136 let mut timer = Timer::new().unwrap();
137 timer.oneshot(100000000000);
143 do run_in_mt_newsched_task {
144 let mut timer = Timer::new().unwrap();
145 let port1 = timer.oneshot(10000);
146 let port = timer.oneshot(1);
148 assert_eq!(port1.try_recv(), None);
153 fn test_io_timer_oneshot_then_sleep() {
154 do run_in_mt_newsched_task {
155 let mut timer = Timer::new().unwrap();
156 let port = timer.oneshot(100000000000);
157 timer.sleep(1); // this should invalidate the port
159 assert_eq!(port.try_recv(), None);
164 fn test_io_timer_sleep_periodic() {
165 do run_in_mt_newsched_task {
166 let mut timer = Timer::new().unwrap();
167 let port = timer.periodic(1);
175 fn test_io_timer_sleep_periodic_forget() {
176 do run_in_mt_newsched_task {
177 let mut timer = Timer::new().unwrap();
178 timer.periodic(100000000000);
183 fn test_io_timer_sleep_standalone() {
184 do run_in_mt_newsched_task {