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 use super::{Loop, UvHandle};
16 use std::rt::rtio::{Callback, PausableIdleCallback};
18 pub struct IdleWatcher {
19 handle: *uvll::uv_idle_t,
22 callback: Box<Callback:Send>,
26 pub fn new(loop_: &mut Loop, cb: Box<Callback:Send>) -> Box<IdleWatcher> {
27 let handle = UvHandle::alloc(None::<IdleWatcher>, uvll::UV_IDLE);
29 uvll::uv_idle_init(loop_.handle, handle)
31 let me = box IdleWatcher {
40 pub fn onetime(loop_: &mut Loop, f: proc()) {
41 let handle = UvHandle::alloc(None::<IdleWatcher>, uvll::UV_IDLE);
43 assert_eq!(uvll::uv_idle_init(loop_.handle, handle), 0);
44 let data: *c_void = cast::transmute(box f);
45 uvll::set_data_for_uv_handle(handle, data);
46 assert_eq!(uvll::uv_idle_start(handle, onetime_cb), 0)
49 extern fn onetime_cb(handle: *uvll::uv_idle_t) {
51 let data = uvll::get_data_for_uv_handle(handle);
52 let f: Box<proc()> = cast::transmute(data);
54 assert_eq!(uvll::uv_idle_stop(handle), 0);
55 uvll::uv_close(handle, close_cb);
59 extern fn close_cb(handle: *uvll::uv_handle_t) {
60 unsafe { uvll::free_handle(handle) }
65 impl PausableIdleCallback for IdleWatcher {
67 if self.idle_flag == true {
68 assert_eq!(unsafe {uvll::uv_idle_stop(self.handle) }, 0);
69 self.idle_flag = false;
72 fn resume(&mut self) {
73 if self.idle_flag == false {
74 assert_eq!(unsafe { uvll::uv_idle_start(self.handle, idle_cb) }, 0)
75 self.idle_flag = true;
80 impl UvHandle<uvll::uv_idle_t> for IdleWatcher {
81 fn uv_handle(&self) -> *uvll::uv_idle_t { self.handle }
84 extern fn idle_cb(handle: *uvll::uv_idle_t) {
85 let idle: &mut IdleWatcher = unsafe { UvHandle::from_uv_handle(&handle) };
89 impl Drop for IdleWatcher {
99 use std::cell::RefCell;
101 use std::rt::rtio::{Callback, PausableIdleCallback};
102 use std::rt::task::{BlockedTask, Task};
103 use std::rt::local::Local;
104 use super::IdleWatcher;
105 use super::super::local_loop;
107 type Chan = Rc<RefCell<(Option<BlockedTask>, uint)>>;
109 struct MyCallback(Rc<RefCell<(Option<BlockedTask>, uint)>>, uint);
110 impl Callback for MyCallback {
112 let task = match *self {
113 MyCallback(ref rc, n) => {
114 match *rc.borrow_mut().deref_mut() {
115 (ref mut task, ref mut val) => {
125 let _ = task.wake().map(|t| t.reawaken());
129 fn mk(v: uint) -> (Box<IdleWatcher>, Chan) {
130 let rc = Rc::new(RefCell::new((None, 0)));
131 let cb = box MyCallback(rc.clone(), v);
132 let cb = cb as Box<Callback:>;
133 let cb = unsafe { cast::transmute(cb) };
134 (IdleWatcher::new(&mut local_loop().loop_, cb), rc)
137 fn sleep(chan: &Chan) -> uint {
138 let task: Box<Task> = Local::take();
139 task.deschedule(1, |task| {
140 match *chan.borrow_mut().deref_mut() {
141 (ref mut slot, _) => {
142 assert!(slot.is_none());
149 match *chan.borrow() { (_, n) => n }
154 let (_idle, _chan) = mk(1);
159 let (mut idle, chan) = mk(1);
161 assert_eq!(sleep(&chan), 1);
164 #[test] #[should_fail]
166 // By default, the test harness is capturing our stderr output through a
167 // channel. This means that when we start failing and "print" our error
168 // message, we could be switched to running on another test. The
169 // IdleWatcher assumes that we're already running on the same task, so
170 // it can cause serious problems and internal race conditions.
172 // To fix this bug, we just set our stderr to a null writer which will
173 // never reschedule us, so we're guaranteed to stay on the same
176 drop(io::stdio::set_stderr(box io::util::NullWriter));
178 let (mut idle, _chan) = mk(1);
184 fn fun_combinations_of_methods() {
185 let (mut idle, chan) = mk(1);
187 assert_eq!(sleep(&chan), 1);
191 assert_eq!(sleep(&chan), 1);
195 assert_eq!(sleep(&chan), 1);
200 let (mut idle1, chan1) = mk(1);
201 let (mut idle2, chan2) = mk(2);
203 assert_eq!(sleep(&chan2), 2);
206 assert_eq!(sleep(&chan1), 1);