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 receivers which will receive notifications after a period of time.
25 use rt::rtio::{IoFactory, LocalIo, RtioTimer};
27 /// A synchronous timer object
29 /// Values of this type can be used to put the current task to sleep for a
30 /// period of time. Handles to this timer can also be created in the form of
31 /// receivers which will receive notifications over time.
38 /// use std::io::Timer;
40 /// let mut timer = Timer::new().unwrap();
41 /// timer.sleep(10); // block the task for awhile
43 /// let timeout = timer.oneshot(10);
45 /// timeout.recv(); // wait for the timeout to expire
47 /// let periodic = timer.periodic(10);
50 /// // this loop is only executed once every 10ms
55 /// If only sleeping is necessary, then a convenience api is provided through
56 /// the `io::timer` module.
61 /// use std::io::timer;
63 /// // Put this task to sleep for 5 seconds
64 /// timer::sleep(5000);
68 obj: Box<RtioTimer:Send>,
71 /// Sleep the current task for `msecs` milliseconds.
72 pub fn sleep(msecs: u64) {
73 let timer = Timer::new();
74 let mut timer = timer.ok().expect("timer::sleep: could not create a Timer");
80 /// Creates a new timer which can be used to put the current task to sleep
81 /// for a number of milliseconds, or to possibly create channels which will
82 /// get notified after an amount of time has passed.
83 pub fn new() -> IoResult<Timer> {
84 LocalIo::maybe_raise(|io| io.timer_init().map(|t| Timer { obj: t }))
87 /// Blocks the current task for `msecs` milliseconds.
89 /// Note that this function will cause any other receivers for this timer to
90 /// be invalidated (the other end will be closed).
91 pub fn sleep(&mut self, msecs: u64) {
92 self.obj.sleep(msecs);
95 /// Creates a oneshot receiver which will have a notification sent when
96 /// `msecs` milliseconds has elapsed. This does *not* block the current
97 /// task, but instead returns immediately.
99 /// Note that this invalidates any previous receiver which has been created
100 /// by this timer, and that the returned receiver will be invalidated once
101 /// the timer is destroyed (when it falls out of scope).
102 pub fn oneshot(&mut self, msecs: u64) -> Receiver<()> {
103 self.obj.oneshot(msecs)
106 /// Creates a receiver which will have a continuous stream of notifications
107 /// being sent every `msecs` milliseconds. This does *not* block the
108 /// current task, but instead returns immediately. The first notification
109 /// will not be received immediately, but rather after `msec` milliseconds
112 /// Note that this invalidates any previous receiver which has been created
113 /// by this timer, and that the returned receiver will be invalidated once
114 /// the timer is destroyed (when it falls out of scope).
115 pub fn periodic(&mut self, msecs: u64) -> Receiver<()> {
116 self.obj.period(msecs)
122 iotest!(fn test_io_timer_sleep_simple() {
123 let mut timer = Timer::new().unwrap();
127 iotest!(fn test_io_timer_sleep_oneshot() {
128 let mut timer = Timer::new().unwrap();
129 timer.oneshot(1).recv();
132 iotest!(fn test_io_timer_sleep_oneshot_forget() {
133 let mut timer = Timer::new().unwrap();
134 timer.oneshot(100000000000);
137 iotest!(fn oneshot_twice() {
138 let mut timer = Timer::new().unwrap();
139 let rx1 = timer.oneshot(10000);
140 let rx = timer.oneshot(1);
142 assert_eq!(rx1.recv_opt(), Err(()));
145 iotest!(fn test_io_timer_oneshot_then_sleep() {
146 let mut timer = Timer::new().unwrap();
147 let rx = timer.oneshot(100000000000);
148 timer.sleep(1); // this should inalidate rx
150 assert_eq!(rx.recv_opt(), Err(()));
153 iotest!(fn test_io_timer_sleep_periodic() {
154 let mut timer = Timer::new().unwrap();
155 let rx = timer.periodic(1);
161 iotest!(fn test_io_timer_sleep_periodic_forget() {
162 let mut timer = Timer::new().unwrap();
163 timer.periodic(100000000000);
166 iotest!(fn test_io_timer_sleep_standalone() {
170 iotest!(fn oneshot() {
171 let mut timer = Timer::new().unwrap();
173 let rx = timer.oneshot(1);
175 assert!(rx.recv_opt().is_err());
177 let rx = timer.oneshot(1);
179 assert!(rx.recv_opt().is_err());
182 iotest!(fn override() {
183 let mut timer = Timer::new().unwrap();
184 let orx = timer.oneshot(100);
185 let prx = timer.periodic(100);
187 assert_eq!(orx.recv_opt(), Err(()));
188 assert_eq!(prx.recv_opt(), Err(()));
189 timer.oneshot(1).recv();
192 iotest!(fn period() {
193 let mut timer = Timer::new().unwrap();
194 let rx = timer.periodic(1);
197 let rx2 = timer.periodic(1);
203 let mut timer = Timer::new().unwrap();
208 iotest!(fn oneshot_fail() {
209 let mut timer = Timer::new().unwrap();
210 let _rx = timer.oneshot(1);
214 iotest!(fn period_fail() {
215 let mut timer = Timer::new().unwrap();
216 let _rx = timer.periodic(1);
220 iotest!(fn normal_fail() {
221 let _timer = Timer::new().unwrap();
225 iotest!(fn closing_channel_during_drop_doesnt_kill_everything() {
227 let mut timer = Timer::new().unwrap();
228 let timer_rx = timer.periodic(1000);
231 let _ = timer_rx.recv_opt();
234 // when we drop the TimerWatcher we're going to destroy the channel,
235 // which must wake up the task on the other end
238 iotest!(fn reset_doesnt_switch_tasks() {
239 // similar test to the one above.
240 let mut timer = Timer::new().unwrap();
241 let timer_rx = timer.periodic(1000);
244 let _ = timer_rx.recv_opt();
250 iotest!(fn reset_doesnt_switch_tasks2() {
251 // similar test to the one above.
252 let mut timer = Timer::new().unwrap();
253 let timer_rx = timer.periodic(1000);
256 let _ = timer_rx.recv_opt();
262 iotest!(fn sender_goes_away_oneshot() {
264 let mut timer = Timer::new().unwrap();
267 assert_eq!(rx.recv_opt(), Err(()));
270 iotest!(fn sender_goes_away_period() {
272 let mut timer = Timer::new().unwrap();
275 assert_eq!(rx.recv_opt(), Err(()));
278 iotest!(fn receiver_goes_away_oneshot() {
279 let mut timer1 = Timer::new().unwrap();
281 let mut timer2 = Timer::new().unwrap();
282 // while sleeping, the prevous timer should fire and not have its
283 // callback do something terrible.
287 iotest!(fn receiver_goes_away_period() {
288 let mut timer1 = Timer::new().unwrap();
290 let mut timer2 = Timer::new().unwrap();
291 // while sleeping, the prevous timer should fire and not have its
292 // callback do something terrible.