2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
//! Utilities that leverage libuv's `uv_timer_*` API
|
2012-04-20 07:00:14 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use uv;
|
2012-09-04 13:23:53 -05:00
|
|
|
use uv::iotask;
|
2012-12-13 15:05:22 -06:00
|
|
|
use uv::iotask::IoTask;
|
2012-08-14 16:17:27 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::libc;
|
2013-01-25 02:52:50 -06:00
|
|
|
use core::libc::c_void;
|
|
|
|
use core::cast::transmute;
|
2013-02-02 05:10:12 -06:00
|
|
|
use core::comm::{stream, Chan, SharedChan, Port, select2i};
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Wait for timeout period then send provided value over a channel
|
|
|
|
*
|
|
|
|
* This call returns immediately. Useful as the building block for a number
|
|
|
|
* of higher-level timer functions.
|
|
|
|
*
|
|
|
|
* Is not guaranteed to wait for exactly the specified time, but will wait
|
|
|
|
* for *at least* that period of time.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `hl_loop` - a `uv::hl::high_level_loop` that the tcp request will run on
|
|
|
|
* * msecs - a timeout period, in milliseconds, to wait
|
|
|
|
* * ch - a channel of type T to send a `val` on
|
|
|
|
* * val - a value of type T to send over the provided `ch`
|
|
|
|
*/
|
2013-02-20 19:07:17 -06:00
|
|
|
pub fn delayed_send<T:Owned>(iotask: &IoTask,
|
2012-12-23 16:41:37 -06:00
|
|
|
msecs: uint,
|
2013-01-25 02:52:50 -06:00
|
|
|
ch: &Chan<T>,
|
2012-12-23 16:41:37 -06:00
|
|
|
val: T) {
|
2013-04-09 00:31:42 -05:00
|
|
|
let (timer_done_po, timer_done_ch) = stream::<()>();
|
2013-04-17 01:45:29 -05:00
|
|
|
let timer_done_ch = SharedChan::new(timer_done_ch);
|
2013-04-09 00:31:42 -05:00
|
|
|
let timer = uv::ll::timer_t();
|
|
|
|
let timer_ptr = ptr::addr_of(&timer);
|
|
|
|
do iotask::interact(iotask) |loop_ptr| {
|
2012-04-20 07:00:14 -05:00
|
|
|
unsafe {
|
2013-04-09 00:31:42 -05:00
|
|
|
let init_result = uv::ll::timer_init(loop_ptr, timer_ptr);
|
|
|
|
if (init_result == 0i32) {
|
|
|
|
let start_result = uv::ll::timer_start(
|
|
|
|
timer_ptr, delayed_send_cb, msecs, 0u);
|
|
|
|
if (start_result == 0i32) {
|
|
|
|
// Note: putting the channel into a ~
|
|
|
|
// to cast to *c_void
|
|
|
|
let timer_done_ch_clone = ~timer_done_ch.clone();
|
|
|
|
let timer_done_ch_ptr = transmute::<
|
|
|
|
~SharedChan<()>, *c_void>(
|
|
|
|
timer_done_ch_clone);
|
|
|
|
uv::ll::set_data_for_uv_handle(
|
|
|
|
timer_ptr,
|
|
|
|
timer_done_ch_ptr);
|
|
|
|
} else {
|
|
|
|
let error_msg = uv::ll::get_last_err_info(
|
|
|
|
loop_ptr);
|
|
|
|
fail!(~"timer::delayed_send() start failed: " +
|
|
|
|
error_msg);
|
2012-04-20 07:00:14 -05:00
|
|
|
}
|
2013-04-09 00:31:42 -05:00
|
|
|
} else {
|
|
|
|
let error_msg = uv::ll::get_last_err_info(loop_ptr);
|
|
|
|
fail!(~"timer::delayed_send() init failed: " +
|
|
|
|
error_msg);
|
|
|
|
}
|
|
|
|
}
|
2012-04-20 07:00:14 -05:00
|
|
|
};
|
2013-04-09 00:31:42 -05:00
|
|
|
// delayed_send_cb has been processed by libuv
|
|
|
|
timer_done_po.recv();
|
|
|
|
// notify the caller immediately
|
|
|
|
ch.send(val);
|
|
|
|
// uv_close for this timer has been processed
|
|
|
|
timer_done_po.recv();
|
2012-04-20 07:00:14 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Blocks the current task for (at least) the specified time period.
|
|
|
|
*
|
|
|
|
* Is not guaranteed to sleep for exactly the specified time, but will sleep
|
|
|
|
* for *at least* that period of time.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `iotask` - a `uv::iotask` that the tcp request will run on
|
|
|
|
* * msecs - an amount of time, in milliseconds, for the current task to block
|
|
|
|
*/
|
2013-01-20 01:38:17 -06:00
|
|
|
pub fn sleep(iotask: &IoTask, msecs: uint) {
|
2013-01-25 02:52:50 -06:00
|
|
|
let (exit_po, exit_ch) = stream::<()>();
|
|
|
|
delayed_send(iotask, msecs, &exit_ch, ());
|
|
|
|
exit_po.recv();
|
2012-04-20 07:00:14 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2013-01-25 13:29:51 -06:00
|
|
|
* Receive on a port for (up to) a specified time, then return an `Option<T>`
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
|
|
|
* This call will block to receive on the provided port for up to the
|
|
|
|
* specified timeout. Depending on whether the provided port receives in that
|
2013-01-25 13:29:51 -06:00
|
|
|
* time period, `recv_timeout` will return an `Option<T>` representing the
|
2012-07-04 16:53:12 -05:00
|
|
|
* result.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * `iotask' - `uv::iotask` that the tcp request will run on
|
|
|
|
* * msecs - an mount of time, in milliseconds, to wait to receive
|
2012-08-14 16:17:27 -05:00
|
|
|
* * wait_port - a `core::comm::port<T>` to receive on
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
|
|
|
* # Returns
|
|
|
|
*
|
2013-01-25 13:29:51 -06:00
|
|
|
* An `Option<T>` representing the outcome of the call. If the call `recv`'d
|
2012-07-04 16:53:12 -05:00
|
|
|
* on the provided port in the allotted timeout period, then the result will
|
2013-01-25 13:29:51 -06:00
|
|
|
* be a `Some(T)`. If not, then `None` will be returned.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2013-02-20 19:07:17 -06:00
|
|
|
pub fn recv_timeout<T:Copy + Owned>(iotask: &IoTask,
|
2012-12-23 16:41:37 -06:00
|
|
|
msecs: uint,
|
2013-01-25 02:52:50 -06:00
|
|
|
wait_po: &Port<T>)
|
2012-12-23 16:41:37 -06:00
|
|
|
-> Option<T> {
|
2013-01-25 02:52:50 -06:00
|
|
|
let (timeout_po, timeout_ch) = stream::<()>();
|
|
|
|
delayed_send(iotask, msecs, &timeout_ch, ());
|
2012-06-14 21:01:12 -05:00
|
|
|
// FIXME: This could be written clearer (#2618)
|
2012-04-20 11:08:02 -05:00
|
|
|
either::either(
|
2013-01-25 02:52:50 -06:00
|
|
|
|_| {
|
2012-08-20 14:23:37 -05:00
|
|
|
None
|
2013-01-25 02:52:50 -06:00
|
|
|
}, |_| {
|
|
|
|
Some(wait_po.recv())
|
|
|
|
}, &select2i(&timeout_po, wait_po)
|
2012-04-20 11:08:02 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2012-04-20 07:00:14 -05:00
|
|
|
// INTERNAL API
|
2012-07-03 18:32:02 -05:00
|
|
|
extern fn delayed_send_cb(handle: *uv::ll::uv_timer_t,
|
2013-01-23 13:43:58 -06:00
|
|
|
status: libc::c_int) {
|
|
|
|
unsafe {
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(
|
|
|
|
"delayed_send_cb handle %? status %?", handle, status);
|
2013-01-25 02:52:50 -06:00
|
|
|
// Faking a borrowed pointer to our ~SharedChan
|
|
|
|
let timer_done_ch_ptr: &*c_void = &uv::ll::get_data_for_uv_handle(
|
|
|
|
handle);
|
|
|
|
let timer_done_ch_ptr = transmute::<&*c_void, &~SharedChan<()>>(
|
|
|
|
timer_done_ch_ptr);
|
2013-01-23 13:43:58 -06:00
|
|
|
let stop_result = uv::ll::timer_stop(handle);
|
|
|
|
if (stop_result == 0i32) {
|
2013-01-25 02:52:50 -06:00
|
|
|
timer_done_ch_ptr.send(());
|
2013-01-23 13:43:58 -06:00
|
|
|
uv::ll::close(handle, delayed_send_close_cb);
|
|
|
|
} else {
|
|
|
|
let loop_ptr = uv::ll::get_loop_for_uv_handle(handle);
|
|
|
|
let error_msg = uv::ll::get_last_err_info(loop_ptr);
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(~"timer::sleep() init failed: "+error_msg);
|
2013-01-23 13:43:58 -06:00
|
|
|
}
|
2012-04-20 07:00:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
extern fn delayed_send_close_cb(handle: *uv::ll::uv_timer_t) {
|
|
|
|
unsafe {
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!("delayed_send_close_cb handle %?", handle);
|
2013-01-25 02:52:50 -06:00
|
|
|
let timer_done_ch_ptr = uv::ll::get_data_for_uv_handle(handle);
|
|
|
|
let timer_done_ch = transmute::<*c_void, ~SharedChan<()>>(
|
|
|
|
timer_done_ch_ptr);
|
|
|
|
timer_done_ch.send(());
|
2013-01-23 13:43:58 -06:00
|
|
|
}
|
2012-04-20 07:00:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2013-01-08 21:37:25 -06:00
|
|
|
|
|
|
|
use timer::*;
|
2012-12-27 20:24:18 -06:00
|
|
|
use uv;
|
2013-03-12 15:00:50 -05:00
|
|
|
use core::rand::RngUtil;
|
2013-01-25 02:52:50 -06:00
|
|
|
use core::pipes::{stream, SharedChan};
|
2012-12-27 20:24:18 -06:00
|
|
|
|
2012-04-20 07:00:14 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_gl_timer_simple_sleep_test() {
|
2013-01-20 01:38:17 -06:00
|
|
|
let hl_loop = &uv::global_loop::get();
|
2012-05-22 18:33:33 -05:00
|
|
|
sleep(hl_loop, 1u);
|
2012-04-20 11:08:02 -05:00
|
|
|
}
|
|
|
|
|
2012-04-28 16:13:25 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_gl_timer_sleep_stress1() {
|
2013-01-20 01:38:17 -06:00
|
|
|
let hl_loop = &uv::global_loop::get();
|
2012-08-16 16:22:09 -05:00
|
|
|
for iter::repeat(50u) {
|
2012-05-22 18:33:33 -05:00
|
|
|
sleep(hl_loop, 1u);
|
2012-04-28 16:13:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_gl_timer_sleep_stress2() {
|
2013-01-25 02:52:50 -06:00
|
|
|
let (po, ch) = stream();
|
2013-04-17 01:45:29 -05:00
|
|
|
let ch = SharedChan::new(ch);
|
2013-01-20 01:38:17 -06:00
|
|
|
let hl_loop = &uv::global_loop::get();
|
2012-04-28 16:13:25 -05:00
|
|
|
|
2012-05-02 14:03:21 -05:00
|
|
|
let repeat = 20u;
|
2012-04-28 16:13:25 -05:00
|
|
|
let spec = {
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
~[(1u, 20u),
|
2012-04-28 16:13:25 -05:00
|
|
|
(10u, 10u),
|
2012-06-29 18:26:56 -05:00
|
|
|
(20u, 2u)]
|
2012-04-28 16:13:25 -05:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-07-04 14:04:28 -05:00
|
|
|
for iter::repeat(repeat) {
|
2012-04-28 16:13:25 -05:00
|
|
|
|
2013-01-25 02:52:50 -06:00
|
|
|
let ch = ch.clone();
|
2012-06-30 18:19:07 -05:00
|
|
|
for spec.each |spec| {
|
2012-09-19 18:55:01 -05:00
|
|
|
let (times, maxms) = *spec;
|
2013-01-25 02:52:50 -06:00
|
|
|
let ch = ch.clone();
|
2013-01-20 01:38:17 -06:00
|
|
|
let hl_loop_clone = hl_loop.clone();
|
2012-07-04 14:04:28 -05:00
|
|
|
do task::spawn {
|
2013-03-01 12:44:43 -06:00
|
|
|
use core::rand::*;
|
2013-04-23 09:00:43 -05:00
|
|
|
let rng = rng();
|
2012-07-04 14:04:28 -05:00
|
|
|
for iter::repeat(times) {
|
2013-01-20 01:38:17 -06:00
|
|
|
sleep(&hl_loop_clone, rng.next() as uint % maxms);
|
2012-04-28 16:13:25 -05:00
|
|
|
}
|
2013-01-25 02:52:50 -06:00
|
|
|
ch.send(());
|
2012-04-28 16:13:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 14:04:28 -05:00
|
|
|
for iter::repeat(repeat * spec.len()) {
|
2013-01-25 02:52:50 -06:00
|
|
|
po.recv()
|
2012-04-28 16:13:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-21 19:29:00 -05:00
|
|
|
// Because valgrind serializes multithreaded programs it can
|
|
|
|
// make timing-sensitive tests fail in wierd ways. In these
|
|
|
|
// next test we run them many times and expect them to pass
|
|
|
|
// the majority of tries.
|
|
|
|
|
2012-04-20 11:08:02 -05:00
|
|
|
#[test]
|
2012-05-18 10:54:43 -05:00
|
|
|
#[cfg(ignore)]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_gl_timer_recv_timeout_before_time_passes() {
|
2012-05-21 19:29:00 -05:00
|
|
|
let times = 100;
|
|
|
|
let mut successes = 0;
|
|
|
|
let mut failures = 0;
|
2012-05-22 18:33:33 -05:00
|
|
|
let hl_loop = uv::global_loop::get();
|
2012-04-20 11:08:02 -05:00
|
|
|
|
2012-07-04 14:04:28 -05:00
|
|
|
for iter::repeat(times as uint) {
|
2012-05-21 19:29:00 -05:00
|
|
|
task::yield();
|
2012-04-20 11:08:02 -05:00
|
|
|
|
2012-05-21 19:29:00 -05:00
|
|
|
let expected = rand::rng().gen_str(16u);
|
2013-01-25 02:52:50 -06:00
|
|
|
let (test_po, test_ch) = stream::<~str>();
|
2012-05-21 19:29:00 -05:00
|
|
|
|
2012-07-04 14:04:28 -05:00
|
|
|
do task::spawn() {
|
2013-01-25 02:52:50 -06:00
|
|
|
delayed_send(hl_loop, 1u, &test_ch, expected);
|
2012-05-21 19:29:00 -05:00
|
|
|
};
|
|
|
|
|
2013-01-25 02:52:50 -06:00
|
|
|
match recv_timeout(hl_loop, 10u, &test_po) {
|
2013-01-25 13:31:45 -06:00
|
|
|
Some(val) => {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(val == expected);
|
2012-08-03 21:59:04 -05:00
|
|
|
successes += 1;
|
|
|
|
}
|
|
|
|
_ => failures += 1
|
2012-05-21 19:29:00 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(successes > times / 2);
|
2012-04-20 11:08:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_gl_timer_recv_timeout_after_time_passes() {
|
2012-05-21 19:29:00 -05:00
|
|
|
let times = 100;
|
|
|
|
let mut successes = 0;
|
|
|
|
let mut failures = 0;
|
2012-05-22 18:33:33 -05:00
|
|
|
let hl_loop = uv::global_loop::get();
|
2012-04-20 11:08:02 -05:00
|
|
|
|
2012-07-04 14:04:28 -05:00
|
|
|
for iter::repeat(times as uint) {
|
2013-04-23 09:00:43 -05:00
|
|
|
let expected = rand::rng().gen_str(16u);
|
2013-01-25 02:52:50 -06:00
|
|
|
let (test_po, test_ch) = stream::<~str>();
|
2013-01-20 01:38:17 -06:00
|
|
|
let hl_loop_clone = hl_loop.clone();
|
2012-07-04 14:04:28 -05:00
|
|
|
do task::spawn() {
|
2013-01-25 02:52:50 -06:00
|
|
|
delayed_send(&hl_loop_clone, 50u, &test_ch, expected);
|
2012-05-21 19:29:00 -05:00
|
|
|
};
|
|
|
|
|
2013-01-25 02:52:50 -06:00
|
|
|
match recv_timeout(&hl_loop, 1u, &test_po) {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => successes += 1,
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => failures += 1
|
2012-05-21 19:29:00 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(successes > times / 2);
|
2012-04-20 07:00:14 -05:00
|
|
|
}
|
|
|
|
}
|