rust/src/libstd/uv_global_loop.rs

198 lines
6.7 KiB
Rust
Raw Normal View History

//! A process-wide libuv event loop for library use.
2012-05-25 13:02:56 -05:00
export get;
2012-05-24 23:38:48 -05:00
import ll = uv_ll;
import iotask = uv_iotask;
import get_gl = get;
import iotask::{iotask, spawn_iotask};
2012-05-24 23:38:48 -05:00
import priv::{chan_from_global_ptr, weaken_task};
2012-08-14 16:17:27 -05:00
import comm = core::comm;
2012-08-15 16:10:46 -05:00
import comm::{Port, Chan, port, chan, select2, listen};
import task::TaskBuilder;
2012-08-14 18:54:13 -05:00
import either::{Left, Right};
extern mod rustrt {
fn rust_uv_get_kernel_global_chan_ptr() -> *libc::uintptr_t;
}
/**
* Race-free helper to get access to a global task where a libuv
* loop is running.
*
* Use `uv::hl::interact` to do operations against the global
* loop that this function returns.
*
* # Return
*
* * A `hl::high_level_loop` that encapsulates communication with the global
* loop.
*/
fn get() -> iotask {
2012-08-01 19:30:05 -05:00
return get_monitor_task_gl();
}
#[doc(hidden)]
fn get_monitor_task_gl() -> iotask unsafe {
2012-05-24 23:38:48 -05:00
let monitor_loop_chan_ptr = rustrt::rust_uv_get_kernel_global_chan_ptr();
2012-08-22 19:24:52 -05:00
debug!("ENTERING global_loop::get() loop chan: %?",
monitor_loop_chan_ptr);
2012-05-24 23:38:48 -05:00
2012-08-22 19:24:52 -05:00
debug!("before priv::chan_from_global_ptr");
2012-08-15 16:10:46 -05:00
type monchan = Chan<iotask>;
2012-05-24 23:38:48 -05:00
let monitor_ch =
do chan_from_global_ptr::<monchan>(monitor_loop_chan_ptr,
|| {
task::task().sched_mode
2012-08-15 16:10:46 -05:00
(task::SingleThreaded)
.unlinked()
}) |msg_po| {
2012-08-22 19:24:52 -05:00
debug!("global monitor task starting");
2012-05-24 23:38:48 -05:00
// As a weak task the runtime will notify us when to exit
2012-06-30 18:19:07 -05:00
do weaken_task() |weak_exit_po| {
2012-08-22 19:24:52 -05:00
debug!("global monitor task is now weak");
let hl_loop = spawn_loop();
2012-05-24 23:38:48 -05:00
loop {
2012-08-22 19:24:52 -05:00
debug!("in outer_loop...");
2012-08-06 14:34:08 -05:00
match select2(weak_exit_po, msg_po) {
2012-08-14 18:54:13 -05:00
Left(weak_exit) => {
2012-05-24 23:38:48 -05:00
// all normal tasks have ended, tell the
// libuv loop to tear_down, then exit
2012-08-22 19:24:52 -05:00
debug!("weak_exit_po recv'd msg: %?", weak_exit);
iotask::exit(hl_loop);
2012-05-24 23:38:48 -05:00
break;
}
2012-08-14 18:54:13 -05:00
Right(fetch_ch) => {
2012-08-22 19:24:52 -05:00
debug!("hl_loop req recv'd: %?", fetch_ch);
2012-05-24 23:38:48 -05:00
fetch_ch.send(hl_loop);
}
}
2012-05-24 23:38:48 -05:00
}
2012-08-22 19:24:52 -05:00
debug!("global monitor task is leaving weakend state");
};
2012-08-22 19:24:52 -05:00
debug!("global monitor task exiting");
2012-05-24 23:38:48 -05:00
};
// once we have a chan to the monitor loop, we ask it for
// the libuv loop's async handle
2012-06-30 18:19:07 -05:00
do listen |fetch_ch| {
2012-05-24 23:38:48 -05:00
monitor_ch.send(fetch_ch);
fetch_ch.recv()
}
}
fn spawn_loop() -> iotask unsafe {
let builder = do task::task().add_wrapper |task_body| {
fn~(move task_body) {
// The I/O loop task also needs to be weak so it doesn't keep
// the runtime alive
2012-06-30 18:19:07 -05:00
do weaken_task |weak_exit_po| {
2012-08-22 19:24:52 -05:00
debug!("global libuv task is now weak %?", weak_exit_po);
task_body();
// We don't wait for the exit message on weak_exit_po
// because the monitor task will tell the uv loop when to
// exit
2012-08-22 19:24:52 -05:00
debug!("global libuv task is leaving weakened state");
}
}
};
spawn_iotask(builder)
}
#[cfg(test)]
mod test {
2012-07-03 18:32:02 -05:00
extern fn simple_timer_close_cb(timer_ptr: *ll::uv_timer_t) unsafe {
let exit_ch_ptr = ll::get_data_for_uv_handle(
2012-08-15 16:10:46 -05:00
timer_ptr as *libc::c_void) as *comm::Chan<bool>;
let exit_ch = *exit_ch_ptr;
2012-08-14 16:17:27 -05:00
core::comm::send(exit_ch, true);
2012-08-22 19:24:52 -05:00
log(debug, fmt!("EXIT_CH_PTR simple_timer_close_cb exit_ch_ptr: %?",
exit_ch_ptr));
}
2012-07-03 18:32:02 -05:00
extern fn simple_timer_cb(timer_ptr: *ll::uv_timer_t,
2012-05-24 22:31:20 -05:00
_status: libc::c_int) unsafe {
log(debug, ~"in simple timer cb");
ll::timer_stop(timer_ptr);
let hl_loop = get_gl();
2012-06-30 18:19:07 -05:00
do iotask::interact(hl_loop) |_loop_ptr| {
log(debug, ~"closing timer");
ll::close(timer_ptr, simple_timer_close_cb);
log(debug, ~"about to deref exit_ch_ptr");
log(debug, ~"after msg sent on deref'd exit_ch");
};
log(debug, ~"exiting simple timer cb");
}
fn impl_uv_hl_simple_timer(iotask: iotask) unsafe {
2012-08-14 16:17:27 -05:00
let exit_po = core::comm::port::<bool>();
let exit_ch = core::comm::chan(exit_po);
let exit_ch_ptr = ptr::addr_of(exit_ch);
2012-08-22 19:24:52 -05:00
log(debug, fmt!("EXIT_CH_PTR newly created exit_ch_ptr: %?",
exit_ch_ptr));
let timer_handle = ll::timer_t();
let timer_ptr = ptr::addr_of(timer_handle);
2012-06-30 18:19:07 -05:00
do iotask::interact(iotask) |loop_ptr| {
log(debug, ~"user code inside interact loop!!!");
let init_status = ll::timer_init(loop_ptr, timer_ptr);
if(init_status == 0i32) {
ll::set_data_for_uv_handle(
timer_ptr as *libc::c_void,
exit_ch_ptr as *libc::c_void);
let start_status = ll::timer_start(timer_ptr, simple_timer_cb,
1u, 0u);
if(start_status == 0i32) {
}
else {
fail ~"failure on ll::timer_start()";
}
}
else {
fail ~"failure on ll::timer_init()";
}
};
2012-08-14 16:17:27 -05:00
core::comm::recv(exit_po);
log(debug, ~"global_loop timer test: msg recv on exit_po, done..");
}
#[test]
fn test_gl_uv_global_loop_high_level_global_timer() unsafe {
let hl_loop = get_gl();
let exit_po = comm::port::<()>();
let exit_ch = comm::chan(exit_po);
2012-08-15 16:10:46 -05:00
task::spawn_sched(task::ManualThreads(1u), || {
impl_uv_hl_simple_timer(hl_loop);
2012-08-14 16:17:27 -05:00
core::comm::send(exit_ch, ());
});
impl_uv_hl_simple_timer(hl_loop);
2012-08-14 16:17:27 -05:00
core::comm::recv(exit_po);
}
// keeping this test ignored until some kind of stress-test-harness
// is set up for the build bots
#[test]
#[ignore]
fn test_stress_gl_uv_global_loop_high_level_global_timer() unsafe {
let hl_loop = get_gl();
2012-08-14 16:17:27 -05:00
let exit_po = core::comm::port::<()>();
let exit_ch = core::comm::chan(exit_po);
let cycles = 5000u;
for iter::repeat(cycles) {
2012-08-15 16:10:46 -05:00
task::spawn_sched(task::ManualThreads(1u), || {
impl_uv_hl_simple_timer(hl_loop);
2012-08-14 16:17:27 -05:00
core::comm::send(exit_ch, ());
});
};
for iter::repeat(cycles) {
2012-08-14 16:17:27 -05:00
core::comm::recv(exit_po);
};
log(debug, ~"test_stress_gl_uv_global_loop_high_level_global_timer"+
~" exiting sucessfully!");
}
}