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, Loop};
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 = io.make_handle();
38 let me = ~TimerWatcher::new_home(&io.loop_, handle);
42 pub fn new_home(loop_: &Loop, home: HomeHandle) -> TimerWatcher {
43 let handle = UvHandle::alloc(None::<TimerWatcher>, uvll::UV_TIMER);
44 assert_eq!(unsafe { uvll::uv_timer_init(loop_.handle, handle) }, 0);
54 pub fn start(&mut self, f: uvll::uv_timer_cb, msecs: u64, period: u64) {
56 uvll::uv_timer_start(self.handle, f, msecs, period)
60 pub fn stop(&mut self) {
61 assert_eq!(unsafe { uvll::uv_timer_stop(self.handle) }, 0)
64 pub unsafe fn set_data<T>(&mut self, data: *T) {
65 uvll::set_data_for_uv_handle(self.handle, data);
69 impl HomingIO for TimerWatcher {
70 fn home<'r>(&'r mut self) -> &'r mut HomeHandle { &mut self.home }
73 impl UvHandle<uvll::uv_timer_t> for TimerWatcher {
74 fn uv_handle(&self) -> *uvll::uv_timer_t { self.handle }
77 impl RtioTimer for TimerWatcher {
78 fn sleep(&mut self, msecs: u64) {
79 // As with all of the below functions, we must be extra careful when
80 // destroying the previous action. If the previous action was a channel,
81 // destroying it could invoke a context switch. For these situtations,
82 // we must temporarily un-home ourselves, then destroy the action, and
83 // then re-home again.
84 let missile = self.fire_homing_missile();
87 let _missile = match mem::replace(&mut self.action, None) {
88 None => missile, // no need to do a homing dance
90 drop(missile); // un-home ourself
91 drop(action); // destroy the previous action
92 self.fire_homing_missile() // re-home ourself
96 // If the descheduling operation unwinds after the timer has been
97 // started, then we need to call stop on the timer.
98 let _f = ForbidUnwind::new("timer");
100 self.action = Some(WakeTask);
101 wait_until_woken_after(&mut self.blocker, &self.uv_loop(), || {
102 self.start(timer_cb, msecs, 0);
107 fn oneshot(&mut self, msecs: u64) -> Receiver<()> {
108 let (tx, rx) = channel();
110 // similarly to the destructor, we must drop the previous action outside
111 // of the homing missile
113 let _m = self.fire_homing_missile();
116 self.start(timer_cb, msecs, 0);
117 mem::replace(&mut self.action, Some(SendOnce(tx)))
123 fn period(&mut self, msecs: u64) -> Receiver<()> {
124 let (tx, rx) = channel();
126 // similarly to the destructor, we must drop the previous action outside
127 // of the homing missile
129 let _m = self.fire_homing_missile();
132 self.start(timer_cb, msecs, msecs);
133 mem::replace(&mut self.action, Some(SendMany(tx, self.id)))
140 extern fn timer_cb(handle: *uvll::uv_timer_t, status: c_int) {
141 let _f = ForbidSwitch::new("timer callback can't switch");
142 assert_eq!(status, 0);
143 let timer: &mut TimerWatcher = unsafe { UvHandle::from_uv_handle(&handle) };
145 match timer.action.take_unwrap() {
147 let task = timer.blocker.take_unwrap();
148 let _ = task.wake().map(|t| t.reawaken());
150 SendOnce(chan) => { let _ = chan.send_opt(()); }
151 SendMany(chan, id) => {
152 let _ = chan.send_opt(());
154 // Note that the above operation could have performed some form of
155 // scheduling. This means that the timer may have decided to insert
156 // some other action to happen. This 'id' keeps track of the updates
157 // to the timer, so we only reset the action back to sending on this
158 // channel if the id has remained the same. This is essentially a
159 // bug in that we have mutably aliasable memory, but that's libuv
160 // for you. We're guaranteed to all be running on the same thread,
161 // so there's no need for any synchronization here.
163 timer.action = Some(SendMany(chan, id));
169 impl Drop for TimerWatcher {
171 // note that this drop is a little subtle. Dropping a channel which is
172 // held internally may invoke some scheduling operations. We can't take
173 // the channel unless we're on the home scheduler, but once we're on the
174 // home scheduler we should never move. Hence, we take the timer's
175 // action item and then move it outside of the homing block.
177 let _m = self.fire_homing_missile();
187 use std::rt::rtio::RtioTimer;
188 use super::super::local_loop;
189 use super::TimerWatcher;
193 let mut timer = TimerWatcher::new(local_loop());
194 let port = timer.oneshot(1);
196 let port = timer.oneshot(1);
202 let mut timer = TimerWatcher::new(local_loop());
203 let oport = timer.oneshot(1);
204 let pport = timer.period(1);
206 assert_eq!(oport.recv_opt(), Err(()));
207 assert_eq!(pport.recv_opt(), Err(()));
208 timer.oneshot(1).recv();
213 let mut timer = TimerWatcher::new(local_loop());
214 let port = timer.period(1);
217 let port2 = timer.period(1);
224 let mut timer = TimerWatcher::new(local_loop());
229 #[test] #[should_fail]
231 let mut timer = TimerWatcher::new(local_loop());
232 let _port = timer.oneshot(1);
236 #[test] #[should_fail]
238 let mut timer = TimerWatcher::new(local_loop());
239 let _port = timer.period(1);
243 #[test] #[should_fail]
245 let _timer = TimerWatcher::new(local_loop());
250 fn closing_channel_during_drop_doesnt_kill_everything() {
252 let mut timer = TimerWatcher::new(local_loop());
253 let timer_port = timer.period(1000);
256 let _ = timer_port.recv_opt();
259 // when we drop the TimerWatcher we're going to destroy the channel,
260 // which must wake up the task on the other end
264 fn reset_doesnt_switch_tasks() {
265 // similar test to the one above.
266 let mut timer = TimerWatcher::new(local_loop());
267 let timer_port = timer.period(1000);
270 let _ = timer_port.recv_opt();
273 drop(timer.oneshot(1));
276 fn reset_doesnt_switch_tasks2() {
277 // similar test to the one above.
278 let mut timer = TimerWatcher::new(local_loop());
279 let timer_port = timer.period(1000);
282 let _ = timer_port.recv_opt();
289 fn sender_goes_away_oneshot() {
291 let mut timer = TimerWatcher::new(local_loop());
294 assert_eq!(port.recv_opt(), Err(()));
298 fn sender_goes_away_period() {
300 let mut timer = TimerWatcher::new(local_loop());
303 assert_eq!(port.recv_opt(), Err(()));
307 fn receiver_goes_away_oneshot() {
308 let mut timer1 = TimerWatcher::new(local_loop());
309 drop(timer1.oneshot(1));
310 let mut timer2 = TimerWatcher::new(local_loop());
311 // while sleeping, the prevous timer should fire and not have its
312 // callback do something terrible.
317 fn receiver_goes_away_period() {
318 let mut timer1 = TimerWatcher::new(local_loop());
319 drop(timer1.period(1));
320 let mut timer2 = TimerWatcher::new(local_loop());
321 // while sleeping, the prevous timer should fire and not have its
322 // callback do something terrible.