2012-04-20 07:00:14 -05:00
|
|
|
#[doc ="
|
|
|
|
Utilities that leverage libuv's `uv_timer_*` API
|
|
|
|
"];
|
|
|
|
|
|
|
|
import uv = uv;
|
2012-05-25 01:42:12 -05:00
|
|
|
import uv::iotask;
|
|
|
|
import iotask::iotask;
|
2012-04-20 11:08:02 -05:00
|
|
|
export delayed_send, sleep, recv_timeout;
|
2012-04-20 07:00:14 -05:00
|
|
|
|
|
|
|
#[doc = "
|
|
|
|
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
|
|
|
|
|
2012-05-22 18:33:33 -05:00
|
|
|
* `hl_loop` - a `uv::hl::high_level_loop` that the tcp request will run on
|
2012-04-20 11:08:02 -05:00
|
|
|
* 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`
|
2012-04-20 07:00:14 -05:00
|
|
|
"]
|
2012-05-25 01:42:12 -05:00
|
|
|
fn delayed_send<T: copy send>(iotask: iotask,
|
2012-05-22 20:24:28 -05:00
|
|
|
msecs: uint, ch: comm::chan<T>, val: T) {
|
2012-05-24 18:42:05 -05:00
|
|
|
// FIME: Looks like we don't need to spawn here
|
2012-04-20 07:00:14 -05:00
|
|
|
task::spawn() {||
|
|
|
|
unsafe {
|
|
|
|
let timer_done_po = comm::port::<()>();
|
|
|
|
let timer_done_ch = comm::chan(timer_done_po);
|
|
|
|
let timer_done_ch_ptr = ptr::addr_of(timer_done_ch);
|
|
|
|
let timer = uv::ll::timer_t();
|
|
|
|
let timer_ptr = ptr::addr_of(timer);
|
2012-05-25 01:42:12 -05:00
|
|
|
iotask::interact(iotask) {|loop_ptr|
|
2012-04-20 07:00:14 -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) {
|
|
|
|
uv::ll::set_data_for_uv_handle(
|
|
|
|
timer_ptr,
|
|
|
|
timer_done_ch_ptr as *libc::c_void);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let error_msg = uv::ll::get_last_err_info(loop_ptr);
|
|
|
|
fail "timer::delayed_send() start failed: "+error_msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let error_msg = uv::ll::get_last_err_info(loop_ptr);
|
|
|
|
fail "timer::delayed_send() init failed: "+error_msg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// delayed_send_cb has been processed by libuv
|
|
|
|
comm::recv(timer_done_po);
|
|
|
|
// notify the caller immediately
|
|
|
|
comm::send(ch, copy(val));
|
|
|
|
// uv_close for this timer has been processed
|
|
|
|
comm::recv(timer_done_po);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "
|
|
|
|
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
|
|
|
|
|
2012-05-25 01:42:12 -05:00
|
|
|
* `iotask` - a `uv::iotask` that the tcp request will run on
|
2012-04-20 07:00:14 -05:00
|
|
|
* msecs - an amount of time, in milliseconds, for the current task to block
|
|
|
|
"]
|
2012-05-25 01:42:12 -05:00
|
|
|
fn sleep(iotask: iotask, msecs: uint) {
|
2012-04-20 07:00:14 -05:00
|
|
|
let exit_po = comm::port::<()>();
|
|
|
|
let exit_ch = comm::chan(exit_po);
|
2012-05-25 01:42:12 -05:00
|
|
|
delayed_send(iotask, msecs, exit_ch, ());
|
2012-04-20 07:00:14 -05:00
|
|
|
comm::recv(exit_po);
|
|
|
|
}
|
|
|
|
|
2012-04-20 11:08:02 -05:00
|
|
|
#[doc = "
|
|
|
|
Receive on a port for (up to) a specified time, then return an `option<T>`
|
|
|
|
|
|
|
|
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 time period,
|
|
|
|
`recv_timeout` will return an `option<T>` representing the result.
|
|
|
|
|
|
|
|
# Arguments
|
|
|
|
|
2012-05-25 01:42:12 -05:00
|
|
|
* `iotask' - `uv::iotask` that the tcp request will run on
|
2012-04-20 11:08:02 -05:00
|
|
|
* msecs - an mount of time, in milliseconds, to wait to receive
|
|
|
|
* wait_port - a `comm::port<T>` to receive on
|
|
|
|
|
|
|
|
# Returns
|
|
|
|
|
|
|
|
An `option<T>` representing the outcome of the call. If the call `recv`'d on
|
|
|
|
the provided port in the allotted timeout period, then the result will be a
|
|
|
|
`some(T)`. If not, then `none` will be returned.
|
|
|
|
"]
|
2012-05-25 01:42:12 -05:00
|
|
|
fn recv_timeout<T: copy send>(iotask: iotask,
|
2012-05-22 20:24:28 -05:00
|
|
|
msecs: uint,
|
|
|
|
wait_po: comm::port<T>) -> option<T> {
|
2012-04-20 11:08:02 -05:00
|
|
|
let timeout_po = comm::port::<()>();
|
|
|
|
let timeout_ch = comm::chan(timeout_po);
|
2012-05-25 01:42:12 -05:00
|
|
|
delayed_send(iotask, msecs, timeout_ch, ());
|
2012-05-24 18:42:05 -05:00
|
|
|
// FIXME: This could be written clearer
|
2012-04-20 11:08:02 -05:00
|
|
|
either::either(
|
|
|
|
{|left_val|
|
|
|
|
log(debug, #fmt("recv_time .. left_val %?",
|
|
|
|
left_val));
|
|
|
|
none
|
|
|
|
}, {|right_val|
|
|
|
|
some(right_val)
|
|
|
|
}, comm::select2(timeout_po, wait_po)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2012-04-20 07:00:14 -05:00
|
|
|
// INTERNAL API
|
|
|
|
crust fn delayed_send_cb(handle: *uv::ll::uv_timer_t,
|
|
|
|
status: libc::c_int) unsafe {
|
|
|
|
log(debug, #fmt("delayed_send_cb handle %? status %?", handle, status));
|
|
|
|
let timer_done_ch =
|
|
|
|
*(uv::ll::get_data_for_uv_handle(handle) as *comm::chan<()>);
|
|
|
|
let stop_result = uv::ll::timer_stop(handle);
|
|
|
|
if (stop_result == 0i32) {
|
|
|
|
comm::send(timer_done_ch, ());
|
2012-04-27 23:42:04 -05:00
|
|
|
uv::ll::close(handle, delayed_send_close_cb);
|
2012-04-20 07:00:14 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
let loop_ptr = uv::ll::get_loop_for_uv_handle(handle);
|
|
|
|
let error_msg = uv::ll::get_last_err_info(loop_ptr);
|
|
|
|
fail "timer::sleep() init failed: "+error_msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
crust fn delayed_send_close_cb(handle: *uv::ll::uv_timer_t) unsafe {
|
|
|
|
log(debug, #fmt("delayed_send_close_cb handle %?", handle));
|
|
|
|
let timer_done_ch =
|
|
|
|
*(uv::ll::get_data_for_uv_handle(handle) as *comm::chan<()>);
|
|
|
|
comm::send(timer_done_ch, ());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
#[test]
|
2012-04-27 23:42:04 -05:00
|
|
|
fn test_gl_timer_simple_sleep_test() {
|
2012-05-22 18:33:33 -05:00
|
|
|
let hl_loop = uv::global_loop::get();
|
|
|
|
sleep(hl_loop, 1u);
|
2012-04-20 11:08:02 -05:00
|
|
|
}
|
|
|
|
|
2012-04-28 16:13:25 -05:00
|
|
|
#[test]
|
|
|
|
fn test_gl_timer_sleep_stress1() {
|
2012-05-22 18:33:33 -05:00
|
|
|
let hl_loop = uv::global_loop::get();
|
2012-05-02 14:03:21 -05:00
|
|
|
iter::repeat(200u) {||
|
2012-05-22 18:33:33 -05:00
|
|
|
sleep(hl_loop, 1u);
|
2012-04-28 16:13:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_gl_timer_sleep_stress2() {
|
|
|
|
let po = comm::port();
|
|
|
|
let ch = comm::chan(po);
|
2012-05-22 18:33:33 -05: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-05-02 14:03:21 -05:00
|
|
|
[(1u, 20u),
|
2012-04-28 16:13:25 -05:00
|
|
|
(10u, 10u),
|
2012-05-02 14:03:21 -05:00
|
|
|
(20u, 2u)]
|
2012-04-28 16:13:25 -05:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
iter::repeat(repeat) {||
|
|
|
|
|
|
|
|
for spec.each {|spec|
|
|
|
|
let (times, maxms) = spec;
|
|
|
|
task::spawn {||
|
|
|
|
import rand::*;
|
|
|
|
let rng = rng();
|
|
|
|
iter::repeat(times) {||
|
2012-05-22 18:33:33 -05:00
|
|
|
sleep(hl_loop, rng.next() as uint % maxms);
|
2012-04-28 16:13:25 -05:00
|
|
|
}
|
|
|
|
comm::send(ch, ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iter::repeat(repeat * spec.len()) {||
|
|
|
|
comm::recv(po)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)]
|
2012-04-27 23:42:04 -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-05-21 19:29:00 -05:00
|
|
|
iter::repeat(times as uint) {||
|
|
|
|
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);
|
|
|
|
let test_po = comm::port::<str>();
|
|
|
|
let test_ch = comm::chan(test_po);
|
|
|
|
|
|
|
|
task::spawn() {||
|
2012-05-22 18:33:33 -05:00
|
|
|
delayed_send(hl_loop, 1u, test_ch, expected);
|
2012-05-21 19:29:00 -05:00
|
|
|
};
|
|
|
|
|
2012-05-22 18:33:33 -05:00
|
|
|
alt recv_timeout(hl_loop, 10u, test_po) {
|
2012-05-21 19:29:00 -05:00
|
|
|
some(val) { assert val == expected; successes += 1; }
|
|
|
|
_ { failures += 1; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
assert successes > times / 2;
|
2012-04-20 11:08:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-04-27 23:42:04 -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-05-21 19:29:00 -05:00
|
|
|
iter::repeat(times as uint) {||
|
|
|
|
let expected = rand::rng().gen_str(16u);
|
|
|
|
let test_po = comm::port::<str>();
|
|
|
|
let test_ch = comm::chan(test_po);
|
2012-04-20 11:08:02 -05:00
|
|
|
|
2012-05-21 19:29:00 -05:00
|
|
|
task::spawn() {||
|
2012-05-22 18:33:33 -05:00
|
|
|
delayed_send(hl_loop, 1000u, test_ch, expected);
|
2012-05-21 19:29:00 -05:00
|
|
|
};
|
|
|
|
|
2012-05-24 22:31:20 -05:00
|
|
|
alt recv_timeout(hl_loop, 1u, test_po) {
|
2012-05-21 19:29:00 -05:00
|
|
|
none { successes += 1; }
|
|
|
|
_ { failures += 1; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
assert successes > times / 2;
|
2012-04-20 07:00:14 -05:00
|
|
|
}
|
|
|
|
}
|