2012-04-02 13:03:45 -05:00
|
|
|
#[doc = "
|
|
|
|
|
2012-05-25 01:42:12 -05:00
|
|
|
A task-based interface to the uv loop
|
|
|
|
|
|
|
|
The I/O task runs in its own single-threaded scheduler. By using the
|
|
|
|
`interact` function you can execute code in a uv callback.
|
|
|
|
|
2012-04-02 13:03:45 -05:00
|
|
|
"];
|
|
|
|
|
2012-05-25 01:42:12 -05:00
|
|
|
export iotask::{};
|
|
|
|
export spawn_iotask;
|
2012-05-25 00:52:51 -05:00
|
|
|
export interact;
|
|
|
|
export exit;
|
2012-04-16 15:32:38 -05:00
|
|
|
|
2012-05-24 21:53:08 -05:00
|
|
|
import libc::c_void;
|
|
|
|
import ptr::addr_of;
|
2012-05-25 03:06:45 -05:00
|
|
|
import comm::{port, chan, methods, listen};
|
2012-04-02 13:03:45 -05:00
|
|
|
import ll = uv_ll;
|
|
|
|
|
|
|
|
#[doc = "
|
|
|
|
Used to abstract-away direct interaction with a libuv loop.
|
|
|
|
"]
|
2012-05-25 01:42:12 -05:00
|
|
|
enum iotask {
|
|
|
|
iotask_({
|
|
|
|
async_handle: *ll::uv_async_t,
|
|
|
|
op_chan: chan<iotask_msg>
|
|
|
|
})
|
|
|
|
}
|
2012-04-19 00:43:11 -05:00
|
|
|
|
2012-05-25 01:42:12 -05:00
|
|
|
fn spawn_iotask(-builder: task::builder) -> iotask {
|
2012-05-25 00:52:51 -05:00
|
|
|
|
|
|
|
import task::{set_opts, get_opts, single_threaded, run};
|
|
|
|
|
|
|
|
set_opts(builder, {
|
|
|
|
sched: some({
|
|
|
|
mode: single_threaded,
|
2012-06-26 18:18:37 -05:00
|
|
|
foreign_stack_size: none
|
2012-05-25 00:52:51 -05:00
|
|
|
})
|
|
|
|
with get_opts(builder)
|
|
|
|
});
|
|
|
|
|
2012-06-26 15:55:56 -05:00
|
|
|
do listen {|iotask_ch|
|
2012-05-25 03:06:45 -05:00
|
|
|
|
2012-06-26 15:55:56 -05:00
|
|
|
do run(copy(builder)) {||
|
2012-05-25 03:06:45 -05:00
|
|
|
#debug("entering libuv task");
|
|
|
|
run_loop(iotask_ch);
|
|
|
|
#debug("libuv task exiting");
|
|
|
|
};
|
2012-05-25 00:52:51 -05:00
|
|
|
|
2012-05-25 03:06:45 -05:00
|
|
|
iotask_ch.recv()
|
|
|
|
}
|
2012-05-25 00:52:51 -05:00
|
|
|
}
|
|
|
|
|
2012-05-25 01:42:12 -05:00
|
|
|
|
|
|
|
#[doc = "
|
|
|
|
Provide a callback to be processed by `iotask`
|
|
|
|
|
|
|
|
The primary way to do operations again a running `iotask` that
|
|
|
|
doesn't involve creating a uv handle via `safe_handle`
|
|
|
|
|
|
|
|
# Warning
|
|
|
|
|
|
|
|
This function is the only safe way to interact with _any_ `iotask`.
|
|
|
|
Using functions in the `uv::ll` module outside of the `cb` passed into
|
|
|
|
this function is _very dangerous_.
|
|
|
|
|
|
|
|
# Arguments
|
|
|
|
|
|
|
|
* iotask - a uv I/O task that you want to do operations against
|
|
|
|
* cb - a function callback to be processed on the running loop's
|
|
|
|
thread. The only parameter passed in is an opaque pointer representing the
|
|
|
|
running `uv_loop_t*`. In the context of this callback, it is safe to use
|
|
|
|
this pointer to do various uv_* API calls contained within the `uv::ll`
|
|
|
|
module. It is not safe to send the `loop_ptr` param to this callback out
|
|
|
|
via ports/chans.
|
2012-04-16 15:32:38 -05:00
|
|
|
"]
|
2012-05-25 01:42:12 -05:00
|
|
|
unsafe fn interact(iotask: iotask,
|
|
|
|
-cb: fn~(*c_void)) {
|
|
|
|
send_msg(iotask, interaction(cb));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc="
|
|
|
|
Shut down the I/O task
|
2012-04-28 00:19:20 -05:00
|
|
|
|
|
|
|
Is used to signal to the loop that it should close the internally-held
|
|
|
|
async handle and do a sanity check to make sure that all other handles are
|
2012-05-25 01:42:12 -05:00
|
|
|
closed, causing a failure otherwise.
|
|
|
|
"]
|
|
|
|
fn exit(iotask: iotask) unsafe {
|
|
|
|
send_msg(iotask, teardown_loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// INTERNAL API
|
|
|
|
|
|
|
|
enum iotask_msg {
|
|
|
|
interaction (fn~(*libc::c_void)),
|
2012-04-27 23:42:04 -05:00
|
|
|
teardown_loop
|
2012-04-16 15:32:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "
|
2012-05-25 01:42:12 -05:00
|
|
|
Run the loop and begin handling messages
|
2012-04-16 15:32:38 -05:00
|
|
|
"]
|
2012-05-25 01:42:12 -05:00
|
|
|
fn run_loop(iotask_ch: chan<iotask>) unsafe {
|
2012-05-25 03:06:45 -05:00
|
|
|
|
2012-05-24 23:44:10 -05:00
|
|
|
let loop_ptr = ll::loop_new();
|
2012-05-25 03:06:45 -05:00
|
|
|
|
2012-04-16 15:32:38 -05:00
|
|
|
// set up the special async handle we'll use to allow multi-task
|
|
|
|
// communication with this loop
|
|
|
|
let async = ll::async_t();
|
2012-05-24 21:53:08 -05:00
|
|
|
let async_handle = addr_of(async);
|
2012-05-25 03:06:45 -05:00
|
|
|
|
2012-04-16 15:32:38 -05:00
|
|
|
// associate the async handle with the loop
|
2012-05-25 01:42:12 -05:00
|
|
|
ll::async_init(loop_ptr, async_handle, wake_up_cb);
|
2012-04-16 17:29:07 -05:00
|
|
|
|
2012-04-16 15:32:38 -05:00
|
|
|
// initialize our loop data and store it in the loop
|
2012-05-25 01:42:12 -05:00
|
|
|
let data: iotask_loop_data = {
|
2012-04-16 15:32:38 -05:00
|
|
|
async_handle: async_handle,
|
2012-05-25 03:06:45 -05:00
|
|
|
msg_po: port()
|
2012-05-24 21:53:08 -05:00
|
|
|
};
|
|
|
|
ll::set_data_for_uv_handle(async_handle, addr_of(data));
|
2012-04-16 15:32:38 -05:00
|
|
|
|
2012-05-25 00:03:14 -05:00
|
|
|
// Send out a handle through which folks can talk to us
|
|
|
|
// while we dwell in the I/O loop
|
2012-05-25 01:42:12 -05:00
|
|
|
let iotask = iotask_({
|
2012-05-25 00:03:14 -05:00
|
|
|
async_handle: async_handle,
|
2012-05-25 03:06:45 -05:00
|
|
|
op_chan: data.msg_po.chan()
|
2012-05-25 00:03:14 -05:00
|
|
|
});
|
2012-05-25 01:42:12 -05:00
|
|
|
iotask_ch.send(iotask);
|
2012-04-16 15:32:38 -05:00
|
|
|
|
2012-05-25 01:42:12 -05:00
|
|
|
log(debug, "about to run uv loop");
|
2012-04-16 15:32:38 -05:00
|
|
|
// enter the loop... this blocks until the loop is done..
|
|
|
|
ll::run(loop_ptr);
|
2012-05-25 01:42:12 -05:00
|
|
|
log(debug, "uv loop ended");
|
2012-05-24 23:44:10 -05:00
|
|
|
ll::loop_delete(loop_ptr);
|
2012-04-16 15:32:38 -05:00
|
|
|
}
|
|
|
|
|
2012-04-17 14:05:04 -05:00
|
|
|
// data that lives for the lifetime of the high-evel oo
|
2012-05-25 01:42:12 -05:00
|
|
|
type iotask_loop_data = {
|
2012-05-24 21:53:08 -05:00
|
|
|
async_handle: *ll::uv_async_t,
|
2012-05-25 03:06:45 -05:00
|
|
|
msg_po: port<iotask_msg>
|
2012-05-24 21:53:08 -05:00
|
|
|
};
|
2012-04-02 13:03:45 -05:00
|
|
|
|
2012-05-25 01:42:12 -05:00
|
|
|
fn send_msg(iotask: iotask,
|
|
|
|
-msg: iotask_msg) unsafe {
|
|
|
|
iotask.op_chan.send(msg);
|
|
|
|
ll::async_send(iotask.async_handle);
|
2012-04-02 13:03:45 -05:00
|
|
|
}
|
|
|
|
|
2012-05-25 03:06:45 -05:00
|
|
|
#[doc ="Dispatch all pending messages"]
|
2012-05-25 01:42:12 -05:00
|
|
|
crust fn wake_up_cb(async_handle: *ll::uv_async_t,
|
|
|
|
status: int) unsafe {
|
2012-05-25 03:06:45 -05:00
|
|
|
|
2012-05-25 01:42:12 -05:00
|
|
|
log(debug, #fmt("wake_up_cb crust.. handle: %? status: %?",
|
2012-04-17 14:05:04 -05:00
|
|
|
async_handle, status));
|
2012-05-25 03:06:45 -05:00
|
|
|
|
2012-04-16 15:32:38 -05:00
|
|
|
let loop_ptr = ll::get_loop_for_uv_handle(async_handle);
|
2012-05-25 01:42:12 -05:00
|
|
|
let data = ll::get_data_for_uv_handle(async_handle) as *iotask_loop_data;
|
2012-05-25 03:06:45 -05:00
|
|
|
let msg_po = (*data).msg_po;
|
|
|
|
|
2012-05-25 01:59:25 -05:00
|
|
|
while msg_po.peek() {
|
2012-05-25 03:06:45 -05:00
|
|
|
alt msg_po.recv() {
|
2012-05-25 01:59:25 -05:00
|
|
|
interaction(cb) {
|
|
|
|
cb(loop_ptr);
|
|
|
|
}
|
|
|
|
teardown_loop {
|
|
|
|
begin_teardown(data);
|
|
|
|
}
|
2012-04-16 15:32:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-25 01:59:25 -05:00
|
|
|
fn begin_teardown(data: *iotask_loop_data) unsafe {
|
|
|
|
log(debug, "iotask begin_teardown() called, close async_handle");
|
|
|
|
let async_handle = (*data).async_handle;
|
|
|
|
ll::close(async_handle as *c_void, tear_down_close_cb);
|
|
|
|
}
|
|
|
|
|
2012-04-16 15:32:38 -05:00
|
|
|
crust fn tear_down_close_cb(handle: *ll::uv_async_t) unsafe {
|
2012-04-27 23:42:04 -05:00
|
|
|
let loop_ptr = ll::get_loop_for_uv_handle(handle);
|
|
|
|
let loop_refs = ll::loop_refcount(loop_ptr);
|
|
|
|
log(debug, #fmt("tear_down_close_cb called, closing handle at %? refs %?",
|
|
|
|
handle, loop_refs));
|
|
|
|
assert loop_refs == 1i32;
|
2012-04-16 15:32:38 -05:00
|
|
|
}
|
|
|
|
|
2012-04-19 17:36:07 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
crust fn async_close_cb(handle: *ll::uv_async_t) unsafe {
|
|
|
|
log(debug, #fmt("async_close_cb handle %?", handle));
|
|
|
|
let exit_ch = (*(ll::get_data_for_uv_handle(handle)
|
|
|
|
as *ah_data)).exit_ch;
|
|
|
|
comm::send(exit_ch, ());
|
|
|
|
}
|
|
|
|
crust fn async_handle_cb(handle: *ll::uv_async_t, status: libc::c_int)
|
|
|
|
unsafe {
|
|
|
|
log(debug, #fmt("async_handle_cb handle %? status %?",handle,status));
|
2012-04-27 23:42:04 -05:00
|
|
|
ll::close(handle, async_close_cb);
|
2012-04-19 17:36:07 -05:00
|
|
|
}
|
|
|
|
type ah_data = {
|
2012-05-25 01:42:12 -05:00
|
|
|
iotask: iotask,
|
2012-04-19 17:36:07 -05:00
|
|
|
exit_ch: comm::chan<()>
|
|
|
|
};
|
2012-05-25 01:42:12 -05:00
|
|
|
fn impl_uv_iotask_async(iotask: iotask) unsafe {
|
2012-04-19 17:36:07 -05:00
|
|
|
let async_handle = ll::async_t();
|
|
|
|
let ah_ptr = ptr::addr_of(async_handle);
|
|
|
|
let exit_po = comm::port::<()>();
|
|
|
|
let exit_ch = comm::chan(exit_po);
|
|
|
|
let ah_data = {
|
2012-05-25 01:42:12 -05:00
|
|
|
iotask: iotask,
|
2012-04-19 17:36:07 -05:00
|
|
|
exit_ch: exit_ch
|
|
|
|
};
|
|
|
|
let ah_data_ptr = ptr::addr_of(ah_data);
|
2012-06-26 15:55:56 -05:00
|
|
|
do interact(iotask) {|loop_ptr|
|
2012-04-19 17:36:07 -05:00
|
|
|
ll::async_init(loop_ptr, ah_ptr, async_handle_cb);
|
|
|
|
ll::set_data_for_uv_handle(ah_ptr, ah_data_ptr as *libc::c_void);
|
|
|
|
ll::async_send(ah_ptr);
|
|
|
|
};
|
|
|
|
comm::recv(exit_po);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this fn documents the bear minimum neccesary to roll your own
|
|
|
|
// high_level_loop
|
2012-05-25 01:42:12 -05:00
|
|
|
unsafe fn spawn_test_loop(exit_ch: comm::chan<()>) -> iotask {
|
|
|
|
let iotask_port = comm::port::<iotask>();
|
|
|
|
let iotask_ch = comm::chan(iotask_port);
|
2012-06-26 15:55:56 -05:00
|
|
|
do task::spawn_sched(task::manual_threads(1u)) {||
|
2012-05-25 01:42:12 -05:00
|
|
|
run_loop(iotask_ch);
|
2012-05-25 00:03:14 -05:00
|
|
|
exit_ch.send(());
|
2012-04-19 17:36:07 -05:00
|
|
|
};
|
2012-05-25 01:42:12 -05:00
|
|
|
ret comm::recv(iotask_port);
|
2012-04-19 17:36:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
crust fn lifetime_handle_close(handle: *libc::c_void) unsafe {
|
|
|
|
log(debug, #fmt("lifetime_handle_close ptr %?", handle));
|
|
|
|
}
|
|
|
|
|
|
|
|
crust fn lifetime_async_callback(handle: *libc::c_void,
|
|
|
|
status: libc::c_int) {
|
|
|
|
log(debug, #fmt("lifetime_handle_close ptr %? status %?",
|
|
|
|
handle, status));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-05-25 01:42:12 -05:00
|
|
|
fn test_uv_iotask_async() unsafe {
|
2012-04-19 17:36:07 -05:00
|
|
|
let exit_po = comm::port::<()>();
|
|
|
|
let exit_ch = comm::chan(exit_po);
|
2012-05-25 01:42:12 -05:00
|
|
|
let iotask = spawn_test_loop(exit_ch);
|
2012-04-19 17:36:07 -05:00
|
|
|
|
|
|
|
// using this handle to manage the lifetime of the high_level_loop,
|
|
|
|
// as it will exit the first time one of the impl_uv_hl_async() is
|
|
|
|
// cleaned up with no one ref'd handles on the loop (Which can happen
|
|
|
|
// under race-condition type situations.. this ensures that the loop
|
|
|
|
// lives until, at least, all of the impl_uv_hl_async() runs have been
|
|
|
|
// called, at least.
|
2012-04-27 23:42:04 -05:00
|
|
|
let work_exit_po = comm::port::<()>();
|
|
|
|
let work_exit_ch = comm::chan(work_exit_po);
|
2012-06-26 15:55:56 -05:00
|
|
|
do iter::repeat(7u) {||
|
|
|
|
do task::spawn_sched(task::manual_threads(1u)) {||
|
2012-05-25 01:42:12 -05:00
|
|
|
impl_uv_iotask_async(iotask);
|
2012-04-27 23:42:04 -05:00
|
|
|
comm::send(work_exit_ch, ());
|
2012-06-26 15:55:56 -05:00
|
|
|
};
|
2012-04-19 17:36:07 -05:00
|
|
|
};
|
2012-06-26 15:55:56 -05:00
|
|
|
do iter::repeat(7u) {||
|
2012-04-27 23:42:04 -05:00
|
|
|
comm::recv(work_exit_po);
|
2012-04-19 17:36:07 -05:00
|
|
|
};
|
2012-04-27 23:42:04 -05:00
|
|
|
log(debug, "sending teardown_loop msg..");
|
2012-05-25 01:42:12 -05:00
|
|
|
exit(iotask);
|
2012-04-19 17:36:07 -05:00
|
|
|
comm::recv(exit_po);
|
2012-04-27 23:42:04 -05:00
|
|
|
log(debug, "after recv on exit_po.. exiting..");
|
2012-04-19 17:36:07 -05:00
|
|
|
}
|
|
|
|
}
|