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
|
|
|
/*!
|
|
|
|
* 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-10-04 21:58:31 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
2012-09-19 20:17:04 -05:00
|
|
|
|
2012-09-04 13:23:53 -05:00
|
|
|
use ll = uv_ll;
|
2012-04-02 13:03:45 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::libc::c_void;
|
|
|
|
use core::libc;
|
2013-01-20 01:38:17 -06:00
|
|
|
use core::pipes::{stream, Port, Chan, SharedChan};
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::ptr::addr_of;
|
|
|
|
use core::task::TaskBuilder;
|
|
|
|
use core::task;
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Used to abstract-away direct interaction with a libuv loop.
|
2012-10-01 19:26:50 -05:00
|
|
|
pub enum IoTask {
|
2012-08-29 19:41:38 -05:00
|
|
|
IoTask_({
|
2012-05-25 01:42:12 -05:00
|
|
|
async_handle: *ll::uv_async_t,
|
2013-01-20 01:38:17 -06:00
|
|
|
op_chan: SharedChan<IoTaskMsg>
|
2012-05-25 01:42:12 -05:00
|
|
|
})
|
|
|
|
}
|
2012-04-19 00:43:11 -05:00
|
|
|
|
2013-01-20 01:38:17 -06:00
|
|
|
impl IoTask: Clone {
|
|
|
|
fn clone(&self) -> IoTask {
|
|
|
|
IoTask_({
|
|
|
|
async_handle: self.async_handle,
|
|
|
|
op_chan: self.op_chan.clone()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 14:21:48 -05:00
|
|
|
pub fn spawn_iotask(task: task::TaskBuilder) -> IoTask {
|
2012-05-25 00:52:51 -05:00
|
|
|
|
2013-01-20 01:38:17 -06:00
|
|
|
let (iotask_port, iotask_chan) = stream();
|
2012-05-25 03:06:45 -05:00
|
|
|
|
2013-01-20 01:38:17 -06:00
|
|
|
do task.sched_mode(task::SingleThreaded).spawn {
|
|
|
|
debug!("entering libuv task");
|
|
|
|
run_loop(&iotask_chan);
|
|
|
|
debug!("libuv task exiting");
|
|
|
|
};
|
2012-05-25 00:52:51 -05:00
|
|
|
|
2013-01-20 01:38:17 -06:00
|
|
|
iotask_port.recv()
|
2012-05-25 00:52:51 -05:00
|
|
|
}
|
|
|
|
|
2012-05-25 01:42:12 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-01-20 01:38:17 -06:00
|
|
|
pub unsafe fn interact(iotask: &IoTask,
|
2012-10-04 21:58:31 -05:00
|
|
|
cb: fn~(*c_void)) {
|
2012-09-10 19:50:48 -05:00
|
|
|
send_msg(iotask, Interaction(move cb));
|
2012-05-25 01:42:12 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Shut down the I/O task
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* closed, causing a failure otherwise.
|
|
|
|
*/
|
2013-01-25 19:51:53 -06:00
|
|
|
pub fn exit(iotask: &IoTask) {
|
2013-01-23 13:43:58 -06:00
|
|
|
unsafe {
|
|
|
|
send_msg(iotask, TeardownLoop);
|
|
|
|
}
|
2012-05-25 01:42:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// INTERNAL API
|
|
|
|
|
2012-08-29 19:41:38 -05:00
|
|
|
enum IoTaskMsg {
|
|
|
|
Interaction (fn~(*libc::c_void)),
|
|
|
|
TeardownLoop
|
2012-04-16 15:32:38 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Run the loop and begin handling messages
|
2013-01-25 19:51:53 -06:00
|
|
|
fn run_loop(iotask_ch: &Chan<IoTask>) {
|
2012-05-25 03:06:45 -05:00
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
unsafe {
|
2013-01-25 19:51:53 -06:00
|
|
|
debug!("creating loop");
|
2013-01-23 13:43:58 -06:00
|
|
|
let loop_ptr = ll::loop_new();
|
|
|
|
|
|
|
|
// set up the special async handle we'll use to allow multi-task
|
|
|
|
// communication with this loop
|
|
|
|
let async = ll::async_t();
|
|
|
|
let async_handle = addr_of(&async);
|
|
|
|
|
|
|
|
// associate the async handle with the loop
|
|
|
|
ll::async_init(loop_ptr, async_handle, wake_up_cb);
|
|
|
|
|
2013-01-25 19:51:53 -06:00
|
|
|
let (msg_po, msg_ch) = stream::<IoTaskMsg>();
|
2013-01-20 01:38:17 -06:00
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
// initialize our loop data and store it in the loop
|
2013-01-25 19:51:53 -06:00
|
|
|
let data: IoTaskLoopData = IoTaskLoopData {
|
2013-01-23 13:43:58 -06:00
|
|
|
async_handle: async_handle,
|
2013-01-25 19:51:53 -06:00
|
|
|
msg_po: msg_po
|
2013-01-23 13:43:58 -06:00
|
|
|
};
|
|
|
|
ll::set_data_for_uv_handle(async_handle, addr_of(&data));
|
|
|
|
|
|
|
|
// Send out a handle through which folks can talk to us
|
|
|
|
// while we dwell in the I/O loop
|
|
|
|
let iotask = IoTask_({
|
|
|
|
async_handle: async_handle,
|
2013-01-25 19:51:53 -06:00
|
|
|
op_chan: SharedChan(msg_ch)
|
2013-01-23 13:43:58 -06:00
|
|
|
});
|
|
|
|
iotask_ch.send(iotask);
|
|
|
|
|
|
|
|
log(debug, ~"about to run uv loop");
|
|
|
|
// enter the loop... this blocks until the loop is done..
|
|
|
|
ll::run(loop_ptr);
|
|
|
|
log(debug, ~"uv loop ended");
|
|
|
|
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
|
2013-01-22 10:44:24 -06:00
|
|
|
struct IoTaskLoopData {
|
2012-05-24 21:53:08 -05:00
|
|
|
async_handle: *ll::uv_async_t,
|
2013-01-22 10:44:24 -06:00
|
|
|
msg_po: Port<IoTaskMsg>,
|
|
|
|
}
|
2012-04-02 13:03:45 -05:00
|
|
|
|
2013-01-20 01:38:17 -06:00
|
|
|
fn send_msg(iotask: &IoTask,
|
2013-01-25 19:51:53 -06:00
|
|
|
msg: IoTaskMsg) {
|
2012-09-10 19:50:48 -05:00
|
|
|
iotask.op_chan.send(move msg);
|
2013-01-23 13:43:58 -06:00
|
|
|
unsafe {
|
|
|
|
ll::async_send(iotask.async_handle);
|
|
|
|
}
|
2012-04-02 13:03:45 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Dispatch all pending messages
|
2012-07-03 18:32:02 -05:00
|
|
|
extern fn wake_up_cb(async_handle: *ll::uv_async_t,
|
2013-01-23 13:43:58 -06:00
|
|
|
status: int) {
|
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
log(debug, fmt!("wake_up_cb extern.. handle: %? status: %?",
|
|
|
|
async_handle, status));
|
2012-05-25 03:06:45 -05:00
|
|
|
|
2013-01-25 19:51:53 -06:00
|
|
|
unsafe {
|
2013-01-23 13:43:58 -06:00
|
|
|
let loop_ptr = ll::get_loop_for_uv_handle(async_handle);
|
2013-01-25 19:51:53 -06:00
|
|
|
let data =
|
|
|
|
ll::get_data_for_uv_handle(async_handle) as *IoTaskLoopData;
|
|
|
|
let msg_po = &(*data).msg_po;
|
2013-01-23 13:43:58 -06:00
|
|
|
|
|
|
|
while msg_po.peek() {
|
|
|
|
match msg_po.recv() {
|
2013-01-25 19:51:53 -06:00
|
|
|
Interaction(ref cb) => (*cb)(loop_ptr),
|
|
|
|
TeardownLoop => begin_teardown(data)
|
2013-01-23 13:43:58 -06:00
|
|
|
}
|
2012-04-16 15:32:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
fn begin_teardown(data: *IoTaskLoopData) {
|
|
|
|
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-05-25 01:59:25 -05:00
|
|
|
}
|
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
extern fn tear_down_close_cb(handle: *ll::uv_async_t) {
|
|
|
|
unsafe {
|
|
|
|
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 {
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-27 20:24:18 -06:00
|
|
|
use uv::ll;
|
2013-01-08 21:37:25 -06:00
|
|
|
use uv_iotask::*;
|
2012-12-27 20:24:18 -06:00
|
|
|
|
2012-12-28 14:46:08 -06:00
|
|
|
use core::iter;
|
|
|
|
use core::libc;
|
2012-12-27 20:24:18 -06:00
|
|
|
use core::oldcomm;
|
|
|
|
use core::ptr;
|
2012-12-28 14:46:08 -06:00
|
|
|
use core::task;
|
2012-12-27 20:24:18 -06:00
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
extern 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 *AhData)).exit_ch;
|
|
|
|
oldcomm::send(exit_ch, ());
|
|
|
|
}
|
2012-04-19 17:36:07 -05:00
|
|
|
}
|
2013-01-23 13:43:58 -06:00
|
|
|
extern fn async_handle_cb(handle: *ll::uv_async_t, status: libc::c_int) {
|
2012-04-19 17:36:07 -05:00
|
|
|
unsafe {
|
2013-01-23 13:43:58 -06:00
|
|
|
log(debug,
|
|
|
|
fmt!("async_handle_cb handle %? status %?",handle,status));
|
|
|
|
ll::close(handle, async_close_cb);
|
|
|
|
}
|
2012-04-19 17:36:07 -05:00
|
|
|
}
|
2013-01-22 10:44:24 -06:00
|
|
|
struct AhData {
|
2012-08-29 19:41:38 -05:00
|
|
|
iotask: IoTask,
|
2012-12-13 16:18:47 -06:00
|
|
|
exit_ch: oldcomm::Chan<()>
|
2013-01-22 10:44:24 -06:00
|
|
|
}
|
2013-01-25 19:51:53 -06:00
|
|
|
fn impl_uv_iotask_async(iotask: &IoTask) {
|
2013-01-23 13:43:58 -06:00
|
|
|
unsafe {
|
|
|
|
let async_handle = ll::async_t();
|
|
|
|
let ah_ptr = ptr::addr_of(&async_handle);
|
|
|
|
let exit_po = oldcomm::Port::<()>();
|
|
|
|
let exit_ch = oldcomm::Chan(&exit_po);
|
2013-01-25 19:51:53 -06:00
|
|
|
let ah_data = AhData {
|
|
|
|
iotask: iotask.clone(),
|
2013-01-23 13:43:58 -06:00
|
|
|
exit_ch: exit_ch
|
|
|
|
};
|
2013-01-25 19:51:53 -06:00
|
|
|
let ah_data_ptr: *AhData = unsafe {
|
|
|
|
ptr::to_unsafe_ptr(&ah_data)
|
|
|
|
};
|
|
|
|
debug!("about to interact");
|
2013-01-23 13:43:58 -06:00
|
|
|
do interact(iotask) |loop_ptr| {
|
|
|
|
unsafe {
|
2013-01-25 19:51:53 -06:00
|
|
|
debug!("interacting");
|
2013-01-23 13:43:58 -06:00
|
|
|
ll::async_init(loop_ptr, ah_ptr, async_handle_cb);
|
2013-01-25 19:51:53 -06:00
|
|
|
ll::set_data_for_uv_handle(
|
|
|
|
ah_ptr, ah_data_ptr as *libc::c_void);
|
2013-01-23 13:43:58 -06:00
|
|
|
ll::async_send(ah_ptr);
|
|
|
|
}
|
|
|
|
};
|
2013-01-25 19:51:53 -06:00
|
|
|
debug!("waiting for async close");
|
2013-01-23 13:43:58 -06:00
|
|
|
oldcomm::recv(exit_po);
|
|
|
|
}
|
2012-04-19 17:36:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// this fn documents the bear minimum neccesary to roll your own
|
|
|
|
// high_level_loop
|
2012-12-13 16:18:47 -06:00
|
|
|
unsafe fn spawn_test_loop(exit_ch: oldcomm::Chan<()>) -> IoTask {
|
2013-01-20 01:38:17 -06:00
|
|
|
let (iotask_port, iotask_ch) = stream::<IoTask>();
|
2012-08-15 16:10:46 -05:00
|
|
|
do task::spawn_sched(task::ManualThreads(1u)) {
|
2013-01-20 01:38:17 -06:00
|
|
|
debug!("about to run a test loop");
|
|
|
|
run_loop(&iotask_ch);
|
2012-05-25 00:03:14 -05:00
|
|
|
exit_ch.send(());
|
2012-04-19 17:36:07 -05:00
|
|
|
};
|
2013-01-20 01:38:17 -06:00
|
|
|
return iotask_port.recv();
|
2012-04-19 17:36:07 -05:00
|
|
|
}
|
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
extern fn lifetime_handle_close(handle: *libc::c_void) {
|
|
|
|
unsafe {
|
|
|
|
log(debug, fmt!("lifetime_handle_close ptr %?", handle));
|
|
|
|
}
|
2012-04-19 17:36:07 -05:00
|
|
|
}
|
|
|
|
|
2012-07-03 18:32:02 -05:00
|
|
|
extern fn lifetime_async_callback(handle: *libc::c_void,
|
2012-04-19 17:36:07 -05:00
|
|
|
status: libc::c_int) {
|
2012-08-22 19:24:52 -05:00
|
|
|
log(debug, fmt!("lifetime_handle_close ptr %? status %?",
|
|
|
|
handle, status));
|
2012-04-19 17:36:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-23 13:43:58 -06:00
|
|
|
fn test_uv_iotask_async() {
|
|
|
|
unsafe {
|
|
|
|
let exit_po = oldcomm::Port::<()>();
|
|
|
|
let exit_ch = oldcomm::Chan(&exit_po);
|
2013-01-25 19:51:53 -06:00
|
|
|
let iotask = &spawn_test_loop(exit_ch);
|
|
|
|
|
|
|
|
debug!("spawned iotask");
|
2013-01-23 13:43:58 -06:00
|
|
|
|
|
|
|
// using this handle to manage the lifetime of the
|
2013-01-25 19:51:53 -06:00
|
|
|
// 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.
|
2013-01-23 13:43:58 -06:00
|
|
|
let work_exit_po = oldcomm::Port::<()>();
|
|
|
|
let work_exit_ch = oldcomm::Chan(&work_exit_po);
|
|
|
|
for iter::repeat(7u) {
|
2013-01-25 19:51:53 -06:00
|
|
|
let iotask_clone = iotask.clone();
|
2013-01-23 13:43:58 -06:00
|
|
|
do task::spawn_sched(task::ManualThreads(1u)) {
|
2013-01-25 19:51:53 -06:00
|
|
|
debug!("async");
|
|
|
|
impl_uv_iotask_async(&iotask_clone);
|
|
|
|
debug!("done async");
|
2013-01-23 13:43:58 -06:00
|
|
|
oldcomm::send(work_exit_ch, ());
|
|
|
|
};
|
2012-06-26 15:55:56 -05:00
|
|
|
};
|
2013-01-23 13:43:58 -06:00
|
|
|
for iter::repeat(7u) {
|
2013-01-25 19:51:53 -06:00
|
|
|
debug!("waiting");
|
2013-01-23 13:43:58 -06:00
|
|
|
oldcomm::recv(work_exit_po);
|
|
|
|
};
|
|
|
|
log(debug, ~"sending teardown_loop msg..");
|
|
|
|
exit(iotask);
|
|
|
|
oldcomm::recv(exit_po);
|
|
|
|
log(debug, ~"after recv on exit_po.. exiting..");
|
|
|
|
}
|
2012-04-19 17:36:07 -05:00
|
|
|
}
|
|
|
|
}
|