diff options
| author | Jeff Olson <olson.jeffery@gmail.com> | 2012-02-21 07:56:27 -0800 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-02-28 17:56:14 -0800 |
| commit | b68eb507da6a2621a74676fc9a4ca76b37561ec8 (patch) | |
| tree | ca80171b6074131e5c9e37322f0122da5e3a5193 /src/rt/rust_uvtmp.cpp | |
| parent | 974c23cbeb4c0183723bac89aa50bf58e0bf7f6c (diff) | |
| download | rust-b68eb507da6a2621a74676fc9a4ca76b37561ec8.tar.gz rust-b68eb507da6a2621a74676fc9a4ca76b37561ec8.zip | |
cleaning up uv_async stuff and stubbing uv_timer
Diffstat (limited to 'src/rt/rust_uvtmp.cpp')
| -rw-r--r-- | src/rt/rust_uvtmp.cpp | 127 |
1 files changed, 91 insertions, 36 deletions
diff --git a/src/rt/rust_uvtmp.cpp b/src/rt/rust_uvtmp.cpp index 18a30fb6404..f97312eaf44 100644 --- a/src/rt/rust_uvtmp.cpp +++ b/src/rt/rust_uvtmp.cpp @@ -57,26 +57,63 @@ struct timer_start_data { // UVTMP REWORK -typedef void (*async_op_cb)(uv_loop_t* loop, void* data); -typedef void (*rust_async_cb)(uint8_t* id_buf, void* loop_data); +// crust fn pointers +typedef void (*crust_async_op_cb)(uv_loop_t* loop, void* data); +typedef void (*crust_async_cb)(uint8_t* id_buf, void* loop_data); +typedef void (*crust_close_cb)(uint8_t* id_buf, void* handle, + void* data); +// data types +#define RUST_UV_HANDLE_LEN 16 + +struct handle_data { + uint8_t id_buf[RUST_UV_HANDLE_LEN]; + crust_async_cb cb; + crust_close_cb close_cb; +}; + +// helpers static void* current_kernel_malloc(size_t size, const char* tag) { - return rust_task_thread::get_task()->malloc(size, tag); + void* ptr = rust_task_thread::get_task()->kernel->malloc(size, tag); + return ptr; } -/* static void current_kernel_free(void* ptr) { - rust_task_thread::get_task()->free(ptr); + rust_task_thread::get_task()->kernel->free(ptr); +} + +// libuv callback impls +static void +native_crust_async_op_cb(uv_async_t* handle, int status) { + crust_async_op_cb cb = (crust_async_op_cb)handle->data; + void* loop_data = handle->loop->data; + cb(handle->loop, loop_data); +} + +static void +native_async_cb(uv_async_t* handle, int status) { + handle_data* handle_d = (handle_data*)handle->data; + void* loop_data = handle->loop->data; + handle_d->cb(handle_d->id_buf, loop_data); +} + +static void +native_close_cb(uv_handle_t* handle) { + handle_data* data = (handle_data*)handle->data; + data->close_cb(data->id_buf, handle, handle->loop->data); +} + +static void +native_close_op_cb(uv_handle_t* op_handle) { + uv_loop_t* loop = op_handle->loop; + current_kernel_free(op_handle); + loop->data = 0; // a ptr to some stack-allocated rust mem + uv_loop_delete(loop); } -*/ -#define RUST_UV_HANDLE_LEN 16 -struct async_data { - uint8_t id_buf[RUST_UV_HANDLE_LEN]; - rust_async_cb cb; -}; +// native fns bound in rust extern "C" void* rust_uvtmp_uv_loop_new() { return (void*)uv_loop_new(); @@ -87,51 +124,69 @@ rust_uvtmp_uv_loop_set_data(uv_loop_t* loop, void* data) { loop->data = data; } -static void -native_async_op_cb(uv_async_t* handle, int status) { - async_op_cb cb = (async_op_cb)handle->data; - void* loop_data = handle->loop->data; - cb(handle->loop, loop_data); -} - extern "C" void* -rust_uvtmp_uv_bind_op_cb(uv_loop_t* loop, async_op_cb cb) { - uv_async_t* async = (uv_async_t*)current_kernel_malloc( +rust_uvtmp_uv_bind_op_cb(uv_loop_t* loop, crust_async_op_cb cb) { + uv_async_t* async = (uv_async_t*)current_kernel_malloc( sizeof(uv_async_t), "uv_async_t"); - uv_async_init(loop, async, native_async_op_cb); - // decrement the ref count, so that our async bind - // does count towards keeping the loop alive + uv_async_init(loop, async, native_crust_async_op_cb); async->data = (void*)cb; + // decrement the ref count, so that our async bind + // doesn't count towards keeping the loop alive + uv_unref(loop); return async; } -extern "C" void rust_uvtmp_uv_run(uv_loop_t* loop) { +extern "C" void +rust_uvtmp_uv_stop_op_cb(uv_handle_t* op_handle) { + /* // this is a hack to get libuv to cleanup a + // handle that was made to not prevent the loop + // from exiting via uv_unref(). + uv_ref(op_handle->loop); + uv_close(op_handle, native_close_op_cb); + uv_run(op_handle->loop); // this should process the handle's + // close event and then return + */ + // the above code is supposed to work to cleanly close + // a handler that was uv_unref()'d. but it causes much spew + // instead. this is the ugly/quick way to deal w/ it for now. + uv_close(op_handle, native_close_op_cb); + native_close_op_cb(op_handle); +} + +extern "C" void +rust_uvtmp_uv_run(uv_loop_t* loop) { uv_run(loop); } extern "C" void -rust_uvtmp_uv_async_send(uv_async_t* handle) { - uv_async_send(handle); +rust_uvtmp_uv_close(uv_handle_t* handle, crust_close_cb cb) { + handle_data* data = (handle_data*)handle->data; + data->close_cb = cb; + uv_close(handle, native_close_cb); } -static void -native_async_cb(uv_async_t* handle, int status) { - async_data* handle_data = (async_data*)handle->data; - void* loop_data = handle->loop->data; - handle_data->cb(handle_data->id_buf, loop_data); +extern "C" void +rust_uvtmp_uv_close_async(uv_async_t* handle) { + current_kernel_free(handle->data); + current_kernel_free(handle); +} + +extern "C" void +rust_uvtmp_uv_async_send(uv_async_t* handle) { + uv_async_send(handle); } extern "C" void* -rust_uvtmp_uv_async_init(uv_loop_t* loop, rust_async_cb cb, +rust_uvtmp_uv_async_init(uv_loop_t* loop, crust_async_cb cb, uint8_t* buf) { - uv_async_t* async = (uv_async_t*)current_kernel_malloc( + uv_async_t* async = (uv_async_t*)current_kernel_malloc( sizeof(uv_async_t), "uv_async_t"); uv_async_init(loop, async, native_async_cb); - async_data* data = (async_data*)current_kernel_malloc( - sizeof(async_data), - "async_data"); + handle_data* data = (handle_data*)current_kernel_malloc( + sizeof(handle_data), + "handle_data"); memcpy(data->id_buf, buf, RUST_UV_HANDLE_LEN); data->cb = cb; async->data = data; |
