1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
// 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.
/*!
Synchronous Timers
This module exposes the functionality to create timers, block the current task,
and create ports which will receive notifications after a period of time.
# Example
```rust,ignore
use std::io::Timer;
let mut timer = Timer::new().unwrap();
timer.sleep(10); // block the task for awhile
let timeout = timer.oneshot(10);
// do some work
timeout.recv(); // wait for the timeout to expire
let periodic = timer.periodic(10);
loop {
periodic.recv();
// this loop is only executed once every 10ms
}
```
*/
use comm::Port;
use option::Option;
use rt::rtio::{IoFactory, LocalIo, RtioTimer};
pub struct Timer {
priv obj: ~RtioTimer
}
/// Sleep the current task for `msecs` milliseconds.
pub fn sleep(msecs: u64) {
let mut timer = Timer::new().expect("timer::sleep: could not create a Timer");
timer.sleep(msecs)
}
impl Timer {
/// Creates a new timer which can be used to put the current task to sleep
/// for a number of milliseconds, or to possibly create channels which will
/// get notified after an amount of time has passed.
pub fn new() -> Option<Timer> {
LocalIo::maybe_raise(|io| io.timer_init().map(|t| Timer { obj: t }))
}
/// Blocks the current task for `msecs` milliseconds.
///
/// Note that this function will cause any other ports for this timer to be
/// invalidated (the other end will be closed).
pub fn sleep(&mut self, msecs: u64) {
self.obj.sleep(msecs);
}
/// Creates a oneshot port which will have a notification sent when `msecs`
/// milliseconds has elapsed. This does *not* block the current task, but
/// instead returns immediately.
///
/// Note that this invalidates any previous port which has been created by
/// this timer, and that the returned port will be invalidated once the
/// timer is destroyed (when it falls out of scope).
pub fn oneshot(&mut self, msecs: u64) -> Port<()> {
self.obj.oneshot(msecs)
}
/// Creates a port which will have a continuous stream of notifications
/// being sent every `msecs` milliseconds. This does *not* block the
/// current task, but instead returns immediately. The first notification
/// will not be received immediately, but rather after `msec` milliseconds
/// have passed.
///
/// Note that this invalidates any previous port which has been created by
/// this timer, and that the returned port will be invalidated once the
/// timer is destroyed (when it falls out of scope).
pub fn periodic(&mut self, msecs: u64) -> Port<()> {
self.obj.period(msecs)
}
}
#[cfg(test)]
mod test {
iotest!(fn test_io_timer_sleep_simple() {
let mut timer = Timer::new().unwrap();
timer.sleep(1);
})
iotest!(fn test_io_timer_sleep_oneshot() {
let mut timer = Timer::new().unwrap();
timer.oneshot(1).recv();
})
iotest!(fn test_io_timer_sleep_oneshot_forget() {
let mut timer = Timer::new().unwrap();
timer.oneshot(100000000000);
})
iotest!(fn oneshot_twice() {
let mut timer = Timer::new().unwrap();
let port1 = timer.oneshot(10000);
let port = timer.oneshot(1);
port.recv();
assert_eq!(port1.recv_opt(), None);
})
iotest!(fn test_io_timer_oneshot_then_sleep() {
let mut timer = Timer::new().unwrap();
let port = timer.oneshot(100000000000);
timer.sleep(1); // this should invalidate the port
assert_eq!(port.recv_opt(), None);
})
iotest!(fn test_io_timer_sleep_periodic() {
let mut timer = Timer::new().unwrap();
let port = timer.periodic(1);
port.recv();
port.recv();
port.recv();
})
iotest!(fn test_io_timer_sleep_periodic_forget() {
let mut timer = Timer::new().unwrap();
timer.periodic(100000000000);
})
iotest!(fn test_io_timer_sleep_standalone() {
sleep(1)
})
iotest!(fn oneshot() {
let mut timer = Timer::new().unwrap();
let port = timer.oneshot(1);
port.recv();
assert!(port.recv_opt().is_none());
let port = timer.oneshot(1);
port.recv();
assert!(port.recv_opt().is_none());
})
iotest!(fn override() {
let mut timer = Timer::new().unwrap();
let oport = timer.oneshot(100);
let pport = timer.periodic(100);
timer.sleep(1);
assert_eq!(oport.recv_opt(), None);
assert_eq!(pport.recv_opt(), None);
timer.oneshot(1).recv();
})
iotest!(fn period() {
let mut timer = Timer::new().unwrap();
let port = timer.periodic(1);
port.recv();
port.recv();
let port2 = timer.periodic(1);
port2.recv();
port2.recv();
})
iotest!(fn sleep() {
let mut timer = Timer::new().unwrap();
timer.sleep(1);
timer.sleep(1);
})
iotest!(fn oneshot_fail() {
let mut timer = Timer::new().unwrap();
let _port = timer.oneshot(1);
fail!();
} #[should_fail])
iotest!(fn period_fail() {
let mut timer = Timer::new().unwrap();
let _port = timer.periodic(1);
fail!();
} #[should_fail])
iotest!(fn normal_fail() {
let _timer = Timer::new().unwrap();
fail!();
} #[should_fail])
iotest!(fn closing_channel_during_drop_doesnt_kill_everything() {
// see issue #10375
let mut timer = Timer::new().unwrap();
let timer_port = timer.periodic(1000);
spawn(proc() {
timer_port.recv_opt();
});
// when we drop the TimerWatcher we're going to destroy the channel,
// which must wake up the task on the other end
})
iotest!(fn reset_doesnt_switch_tasks() {
// similar test to the one above.
let mut timer = Timer::new().unwrap();
let timer_port = timer.periodic(1000);
spawn(proc() {
timer_port.recv_opt();
});
timer.oneshot(1);
})
iotest!(fn reset_doesnt_switch_tasks2() {
// similar test to the one above.
let mut timer = Timer::new().unwrap();
let timer_port = timer.periodic(1000);
spawn(proc() {
timer_port.recv_opt();
});
timer.sleep(1);
})
iotest!(fn sender_goes_away_oneshot() {
let port = {
let mut timer = Timer::new().unwrap();
timer.oneshot(1000)
};
assert_eq!(port.recv_opt(), None);
})
iotest!(fn sender_goes_away_period() {
let port = {
let mut timer = Timer::new().unwrap();
timer.periodic(1000)
};
assert_eq!(port.recv_opt(), None);
})
iotest!(fn receiver_goes_away_oneshot() {
let mut timer1 = Timer::new().unwrap();
timer1.oneshot(1);
let mut timer2 = Timer::new().unwrap();
// while sleeping, the prevous timer should fire and not have its
// callback do something terrible.
timer2.sleep(2);
})
iotest!(fn receiver_goes_away_period() {
let mut timer1 = Timer::new().unwrap();
timer1.periodic(1);
let mut timer2 = Timer::new().unwrap();
// while sleeping, the prevous timer should fire and not have its
// callback do something terrible.
timer2.sleep(2);
})
}
|