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.
12 use std::rt::rtio::{RtioTimer, Callback};
13 use std::rt::task::BlockedTask;
15 use homing::{HomeHandle, HomingIO};
16 use super::{UvHandle, ForbidUnwind, ForbidSwitch, wait_until_woken_after, Loop};
17 use uvio::UvIoFactory;
20 pub struct TimerWatcher {
21 pub handle: *uvll::uv_timer_t,
23 action: Option<NextAction>,
24 blocker: Option<BlockedTask>,
25 id: uint, // see comments in timer_cb
30 CallOnce(Box<Callback + Send>),
31 CallMany(Box<Callback + Send>, uint),
35 pub fn new(io: &mut UvIoFactory) -> Box<TimerWatcher> {
36 let handle = io.make_handle();
37 let me = box TimerWatcher::new_home(&io.loop_, handle);
41 pub fn new_home(loop_: &Loop, home: HomeHandle) -> TimerWatcher {
42 let handle = UvHandle::alloc(None::<TimerWatcher>, uvll::UV_TIMER);
43 assert_eq!(unsafe { uvll::uv_timer_init(loop_.handle, handle) }, 0);
53 pub fn start(&mut self, f: uvll::uv_timer_cb, msecs: u64, period: u64) {
55 uvll::uv_timer_start(self.handle, f, msecs, period)
59 pub fn stop(&mut self) {
60 assert_eq!(unsafe { uvll::uv_timer_stop(self.handle) }, 0)
63 pub unsafe fn set_data<T>(&mut self, data: *T) {
64 uvll::set_data_for_uv_handle(self.handle, data);
68 impl HomingIO for TimerWatcher {
69 fn home<'r>(&'r mut self) -> &'r mut HomeHandle { &mut self.home }
72 impl UvHandle<uvll::uv_timer_t> for TimerWatcher {
73 fn uv_handle(&self) -> *uvll::uv_timer_t { self.handle }
76 impl RtioTimer for TimerWatcher {
77 fn sleep(&mut self, msecs: u64) {
78 // As with all of the below functions, we must be extra careful when
79 // destroying the previous action. If the previous action was a channel,
80 // destroying it could invoke a context switch. For these situtations,
81 // we must temporarily un-home ourselves, then destroy the action, and
82 // then re-home again.
83 let missile = self.fire_homing_missile();
86 let _missile = match mem::replace(&mut self.action, None) {
87 None => missile, // no need to do a homing dance
89 drop(missile); // un-home ourself
90 drop(action); // destroy the previous action
91 self.fire_homing_missile() // re-home ourself
95 // If the descheduling operation unwinds after the timer has been
96 // started, then we need to call stop on the timer.
97 let _f = ForbidUnwind::new("timer");
99 self.action = Some(WakeTask);
100 wait_until_woken_after(&mut self.blocker, &self.uv_loop(), || {
101 self.start(timer_cb, msecs, 0);
106 fn oneshot(&mut self, msecs: u64, cb: Box<Callback + Send>) {
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(CallOnce(cb)))
118 fn period(&mut self, msecs: u64, cb: Box<Callback + Send>) {
119 // similarly to the destructor, we must drop the previous action outside
120 // of the homing missile
122 let _m = self.fire_homing_missile();
125 self.start(timer_cb, msecs, msecs);
126 mem::replace(&mut self.action, Some(CallMany(cb, self.id)))
131 extern fn timer_cb(handle: *uvll::uv_timer_t) {
132 let _f = ForbidSwitch::new("timer callback can't switch");
133 let timer: &mut TimerWatcher = unsafe { UvHandle::from_uv_handle(&handle) };
135 match timer.action.take_unwrap() {
137 let task = timer.blocker.take_unwrap();
138 let _ = task.wake().map(|t| t.reawaken());
140 CallOnce(mut cb) => { cb.call() }
141 CallMany(mut cb, id) => {
144 // Note that the above operation could have performed some form of
145 // scheduling. This means that the timer may have decided to insert
146 // some other action to happen. This 'id' keeps track of the updates
147 // to the timer, so we only reset the action back to sending on this
148 // channel if the id has remained the same. This is essentially a
149 // bug in that we have mutably aliasable memory, but that's libuv
150 // for you. We're guaranteed to all be running on the same thread,
151 // so there's no need for any synchronization here.
153 timer.action = Some(CallMany(cb, id));
159 impl Drop for TimerWatcher {
161 // note that this drop is a little subtle. Dropping a channel which is
162 // held internally may invoke some scheduling operations. We can't take
163 // the channel unless we're on the home scheduler, but once we're on the
164 // home scheduler we should never move. Hence, we take the timer's
165 // action item and then move it outside of the homing block.
167 let _m = self.fire_homing_missile();