2013-10-22 17:13:18 -05:00
|
|
|
// 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.
|
|
|
|
|
2013-11-04 14:45:05 -06:00
|
|
|
use std::cast;
|
|
|
|
use std::libc::{c_int, c_void};
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
use uvll;
|
2013-11-04 14:45:05 -06:00
|
|
|
use super::{Loop, UvHandle};
|
|
|
|
use std::rt::rtio::{Callback, PausibleIdleCallback};
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-04 14:45:05 -06:00
|
|
|
pub struct IdleWatcher {
|
|
|
|
handle: *uvll::uv_idle_t,
|
|
|
|
idle_flag: bool,
|
|
|
|
closed: bool,
|
2013-11-06 13:38:53 -06:00
|
|
|
callback: ~Callback,
|
2013-11-04 14:45:05 -06:00
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
impl IdleWatcher {
|
2013-11-06 13:38:53 -06:00
|
|
|
pub fn new(loop_: &mut Loop, cb: ~Callback) -> ~IdleWatcher {
|
2013-11-04 14:45:05 -06:00
|
|
|
let handle = UvHandle::alloc(None::<IdleWatcher>, uvll::UV_IDLE);
|
|
|
|
assert_eq!(unsafe {
|
2013-11-05 13:29:45 -06:00
|
|
|
uvll::uv_idle_init(loop_.handle, handle)
|
2013-11-04 14:45:05 -06:00
|
|
|
}, 0);
|
|
|
|
let me = ~IdleWatcher {
|
|
|
|
handle: handle,
|
|
|
|
idle_flag: false,
|
|
|
|
closed: false,
|
2013-11-06 13:38:53 -06:00
|
|
|
callback: cb,
|
2013-11-04 14:45:05 -06:00
|
|
|
};
|
|
|
|
return me.install();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn onetime(loop_: &mut Loop, f: proc()) {
|
|
|
|
let handle = UvHandle::alloc(None::<IdleWatcher>, uvll::UV_IDLE);
|
2013-10-22 17:13:18 -05:00
|
|
|
unsafe {
|
2013-11-05 13:29:45 -06:00
|
|
|
assert_eq!(uvll::uv_idle_init(loop_.handle, handle), 0);
|
2013-11-04 14:45:05 -06:00
|
|
|
let data: *c_void = cast::transmute(~f);
|
|
|
|
uvll::set_data_for_uv_handle(handle, data);
|
2013-11-04 16:03:32 -06:00
|
|
|
assert_eq!(uvll::uv_idle_start(handle, onetime_cb), 0)
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2013-11-04 14:45:05 -06:00
|
|
|
extern fn onetime_cb(handle: *uvll::uv_idle_t, status: c_int) {
|
|
|
|
assert_eq!(status, 0);
|
|
|
|
unsafe {
|
|
|
|
let data = uvll::get_data_for_uv_handle(handle);
|
|
|
|
let f: ~proc() = cast::transmute(data);
|
|
|
|
(*f)();
|
2013-11-04 16:03:32 -06:00
|
|
|
uvll::uv_idle_stop(handle);
|
|
|
|
uvll::uv_close(handle, close_cb);
|
2013-11-04 14:45:05 -06:00
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2013-11-04 14:45:05 -06:00
|
|
|
extern fn close_cb(handle: *uvll::uv_handle_t) {
|
|
|
|
unsafe { uvll::free_handle(handle) }
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-04 14:45:05 -06:00
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-04 14:45:05 -06:00
|
|
|
impl PausibleIdleCallback for IdleWatcher {
|
|
|
|
fn pause(&mut self) {
|
|
|
|
if self.idle_flag == true {
|
2013-11-04 16:03:32 -06:00
|
|
|
assert_eq!(unsafe {uvll::uv_idle_stop(self.handle) }, 0);
|
2013-11-04 14:45:05 -06:00
|
|
|
self.idle_flag = false;
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-04 14:45:05 -06:00
|
|
|
fn resume(&mut self) {
|
|
|
|
if self.idle_flag == false {
|
2013-11-04 16:03:32 -06:00
|
|
|
assert_eq!(unsafe { uvll::uv_idle_start(self.handle, idle_cb) }, 0)
|
2013-11-04 14:45:05 -06:00
|
|
|
self.idle_flag = true;
|
|
|
|
}
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2013-11-04 14:45:05 -06:00
|
|
|
impl UvHandle<uvll::uv_idle_t> for IdleWatcher {
|
|
|
|
fn uv_handle(&self) -> *uvll::uv_idle_t { self.handle }
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern fn idle_cb(handle: *uvll::uv_idle_t, status: c_int) {
|
2013-11-04 14:45:05 -06:00
|
|
|
assert_eq!(status, 0);
|
|
|
|
let idle: &mut IdleWatcher = unsafe { UvHandle::from_uv_handle(&handle) };
|
2013-11-06 13:38:53 -06:00
|
|
|
idle.callback.call();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for IdleWatcher {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.pause();
|
|
|
|
self.close_async_();
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2013-11-06 13:38:53 -06:00
|
|
|
use std::rt::tube::Tube;
|
|
|
|
use std::rt::rtio::{Callback, PausibleIdleCallback};
|
2013-11-07 17:13:06 -06:00
|
|
|
use super::super::local_loop;
|
2013-11-06 13:38:53 -06:00
|
|
|
|
|
|
|
struct MyCallback(Tube<int>, int);
|
|
|
|
impl Callback for MyCallback {
|
|
|
|
fn call(&mut self) {
|
|
|
|
match *self {
|
|
|
|
MyCallback(ref mut tube, val) => tube.send(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-11-06 13:38:53 -06:00
|
|
|
fn not_used() {
|
2013-11-07 17:13:06 -06:00
|
|
|
let cb = ~MyCallback(Tube::new(), 1);
|
|
|
|
let _idle = IdleWatcher::new(local_loop(), cb as ~Callback);
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-11-06 13:38:53 -06:00
|
|
|
fn smoke_test() {
|
2013-11-07 17:13:06 -06:00
|
|
|
let mut tube = Tube::new();
|
|
|
|
let cb = ~MyCallback(tube.clone(), 1);
|
|
|
|
let mut idle = IdleWatcher::new(local_loop(), cb as ~Callback);
|
|
|
|
idle.resume();
|
|
|
|
tube.recv();
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2013-11-07 22:13:25 -06:00
|
|
|
#[test] #[should_fail]
|
|
|
|
fn smoke_fail() {
|
|
|
|
let tube = Tube::new();
|
|
|
|
let cb = ~MyCallback(tube.clone(), 1);
|
|
|
|
let mut idle = IdleWatcher::new(local_loop(), cb as ~Callback);
|
|
|
|
idle.resume();
|
|
|
|
fail!();
|
|
|
|
}
|
|
|
|
|
2013-10-22 17:13:18 -05:00
|
|
|
#[test]
|
2013-11-06 13:38:53 -06:00
|
|
|
fn fun_combinations_of_methods() {
|
2013-11-07 17:13:06 -06:00
|
|
|
let mut tube = Tube::new();
|
|
|
|
let cb = ~MyCallback(tube.clone(), 1);
|
|
|
|
let mut idle = IdleWatcher::new(local_loop(), cb as ~Callback);
|
|
|
|
idle.resume();
|
|
|
|
tube.recv();
|
|
|
|
idle.pause();
|
|
|
|
idle.resume();
|
|
|
|
idle.resume();
|
|
|
|
tube.recv();
|
|
|
|
idle.pause();
|
|
|
|
idle.pause();
|
|
|
|
idle.resume();
|
|
|
|
tube.recv();
|
2013-11-06 13:38:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pause_pauses() {
|
2013-11-07 17:13:06 -06:00
|
|
|
let mut tube = Tube::new();
|
|
|
|
let cb = ~MyCallback(tube.clone(), 1);
|
|
|
|
let mut idle1 = IdleWatcher::new(local_loop(), cb as ~Callback);
|
|
|
|
let cb = ~MyCallback(tube.clone(), 2);
|
|
|
|
let mut idle2 = IdleWatcher::new(local_loop(), cb as ~Callback);
|
|
|
|
idle2.resume();
|
|
|
|
assert_eq!(tube.recv(), 2);
|
|
|
|
idle2.pause();
|
|
|
|
idle1.resume();
|
|
|
|
assert_eq!(tube.recv(), 1);
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|