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.
13 use std::rt::rtio::RtioTimer;
14 use std::rt::task::BlockedTask;
16 use homing::{HomeHandle, HomingIO};
17 use super::{UvHandle, ForbidUnwind, ForbidSwitch, wait_until_woken_after};
18 use uvio::UvIoFactory;
21 pub struct TimerWatcher {
22 handle: *uvll::uv_timer_t,
24 action: Option<NextAction>,
25 blocker: Option<BlockedTask>,
26 id: uint, // see comments in timer_cb
32 SendMany(Sender<()>, uint),
36 pub fn new(io: &mut UvIoFactory) -> ~TimerWatcher {
37 let handle = UvHandle::alloc(None::<TimerWatcher>, uvll::UV_TIMER);
39 uvll::uv_timer_init(io.uv_loop(), handle)
41 let me = ~TimerWatcher {
45 home: io.make_handle(),
51 pub fn start(&mut self, f: uvll::uv_timer_cb, msecs: u64, period: u64) {
53 uvll::uv_timer_start(self.handle, f, msecs, period)
57 pub fn stop(&mut self) {
58 assert_eq!(unsafe { uvll::uv_timer_stop(self.handle) }, 0)
61 pub unsafe fn set_data<T>(&mut self, data: *T) {
62 uvll::set_data_for_uv_handle(self.handle, data);
66 impl HomingIO for TimerWatcher {
67 fn home<'r>(&'r mut self) -> &'r mut HomeHandle { &mut self.home }
70 impl UvHandle<uvll::uv_timer_t> for TimerWatcher {
71 fn uv_handle(&self) -> *uvll::uv_timer_t { self.handle }
74 impl RtioTimer for TimerWatcher {
75 fn sleep(&mut self, msecs: u64) {
76 // As with all of the below functions, we must be extra careful when
77 // destroying the previous action. If the previous action was a channel,
78 // destroying it could invoke a context switch. For these situtations,
79 // we must temporarily un-home ourselves, then destroy the action, and
80 // then re-home again.
81 let missile = self.fire_homing_missile();
84 let _missile = match mem::replace(&mut self.action, None) {
85 None => missile, // no need to do a homing dance
87 drop(missile); // un-home ourself
88 drop(action); // destroy the previous action
89 self.fire_homing_missile() // re-home ourself
93 // If the descheduling operation unwinds after the timer has been
94 // started, then we need to call stop on the timer.
95 let _f = ForbidUnwind::new("timer");
97 self.action = Some(WakeTask);
98 wait_until_woken_after(&mut self.blocker, &self.uv_loop(), || {
99 self.start(timer_cb, msecs, 0);
104 fn oneshot(&mut self, msecs: u64) -> Receiver<()> {
105 let (tx, rx) = channel();
107 // similarly to the destructor, we must drop the previous action outside
108 // of the homing missile
110 let _m = self.fire_homing_missile();
113 self.start(timer_cb, msecs, 0);
114 mem::replace(&mut self.action, Some(SendOnce(tx)))
120 fn period(&mut self, msecs: u64) -> Receiver<()> {
121 let (tx, rx) = channel();
123 // similarly to the destructor, we must drop the previous action outside
124 // of the homing missile
126 let _m = self.fire_homing_missile();
129 self.start(timer_cb, msecs, msecs);
130 mem::replace(&mut self.action, Some(SendMany(tx, self.id)))
137 extern fn timer_cb(handle: *uvll::uv_timer_t, status: c_int) {
138 let _f = ForbidSwitch::new("timer callback can't switch");
139 assert_eq!(status, 0);
140 let timer: &mut TimerWatcher = unsafe { UvHandle::from_uv_handle(&handle) };
142 match timer.action.take_unwrap() {
144 let task = timer.blocker.take_unwrap();
145 let _ = task.wake().map(|t| t.reawaken());
147 SendOnce(chan) => { let _ = chan.send_opt(()); }
148 SendMany(chan, id) => {
149 let _ = chan.send_opt(());
151 // Note that the above operation could have performed some form of
152 // scheduling. This means that the timer may have decided to insert
153 // some other action to happen. This 'id' keeps track of the updates
154 // to the timer, so we only reset the action back to sending on this
155 // channel if the id has remained the same. This is essentially a
156 // bug in that we have mutably aliasable memory, but that's libuv
157 // for you. We're guaranteed to all be running on the same thread,
158 // so there's no need for any synchronization here.
160 timer.action = Some(SendMany(chan, id));
166 impl Drop for TimerWatcher {
168 // note that this drop is a little subtle. Dropping a channel which is
169 // held internally may invoke some scheduling operations. We can't take
170 // the channel unless we're on the home scheduler, but once we're on the
171 // home scheduler we should never move. Hence, we take the timer's
172 // action item and then move it outside of the homing block.
174 let _m = self.fire_homing_missile();
184 use std::rt::rtio::RtioTimer;
185 use super::super::local_loop;
186 use super::TimerWatcher;
190 let mut timer = TimerWatcher::new(local_loop());
191 let port = timer.oneshot(1);
193 let port = timer.oneshot(1);
199 let mut timer = TimerWatcher::new(local_loop());
200 let oport = timer.oneshot(1);
201 let pport = timer.period(1);
203 assert_eq!(oport.recv_opt(), Err(()));
204 assert_eq!(pport.recv_opt(), Err(()));
205 timer.oneshot(1).recv();
210 let mut timer = TimerWatcher::new(local_loop());
211 let port = timer.period(1);
214 let port2 = timer.period(1);
221 let mut timer = TimerWatcher::new(local_loop());
226 #[test] #[should_fail]
228 let mut timer = TimerWatcher::new(local_loop());
229 let _port = timer.oneshot(1);
233 #[test] #[should_fail]
235 let mut timer = TimerWatcher::new(local_loop());
236 let _port = timer.period(1);
240 #[test] #[should_fail]
242 let _timer = TimerWatcher::new(local_loop());
247 fn closing_channel_during_drop_doesnt_kill_everything() {
249 let mut timer = TimerWatcher::new(local_loop());
250 let timer_port = timer.period(1000);
253 let _ = timer_port.recv_opt();
256 // when we drop the TimerWatcher we're going to destroy the channel,
257 // which must wake up the task on the other end
261 fn reset_doesnt_switch_tasks() {
262 // similar test to the one above.
263 let mut timer = TimerWatcher::new(local_loop());
264 let timer_port = timer.period(1000);
267 let _ = timer_port.recv_opt();
270 drop(timer.oneshot(1));
273 fn reset_doesnt_switch_tasks2() {
274 // similar test to the one above.
275 let mut timer = TimerWatcher::new(local_loop());
276 let timer_port = timer.period(1000);
279 let _ = timer_port.recv_opt();
286 fn sender_goes_away_oneshot() {
288 let mut timer = TimerWatcher::new(local_loop());
291 assert_eq!(port.recv_opt(), Err(()));
295 fn sender_goes_away_period() {
297 let mut timer = TimerWatcher::new(local_loop());
300 assert_eq!(port.recv_opt(), Err(()));
304 fn receiver_goes_away_oneshot() {
305 let mut timer1 = TimerWatcher::new(local_loop());
306 drop(timer1.oneshot(1));
307 let mut timer2 = TimerWatcher::new(local_loop());
308 // while sleeping, the prevous timer should fire and not have its
309 // callback do something terrible.
314 fn receiver_goes_away_period() {
315 let mut timer1 = TimerWatcher::new(local_loop());
316 drop(timer1.period(1));
317 let mut timer2 = TimerWatcher::new(local_loop());
318 // while sleeping, the prevous timer should fire and not have its
319 // callback do something terrible.