about summary refs log tree commit diff
path: root/src/rt/rust_uvtmp.cpp
diff options
context:
space:
mode:
authorJeff Olson <olson.jeffery@gmail.com>2012-02-17 23:11:02 -0800
committerBrian Anderson <banderson@mozilla.com>2012-02-28 17:56:14 -0800
commit974c23cbeb4c0183723bac89aa50bf58e0bf7f6c (patch)
treeb49f2763f9bde2a47445f6a4c5d4d0d6634ad963 /src/rt/rust_uvtmp.cpp
parentffad8d7f0cc4917f46757f5a431f6207238bf59b (diff)
downloadrust-974c23cbeb4c0183723bac89aa50bf58e0bf7f6c.tar.gz
rust-974c23cbeb4c0183723bac89aa50bf58e0bf7f6c.zip
removed hello world and added uv_async_*
Diffstat (limited to 'src/rt/rust_uvtmp.cpp')
-rw-r--r--src/rt/rust_uvtmp.cpp40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/rt/rust_uvtmp.cpp b/src/rt/rust_uvtmp.cpp
index 4a8df1fa886..18a30fb6404 100644
--- a/src/rt/rust_uvtmp.cpp
+++ b/src/rt/rust_uvtmp.cpp
@@ -57,6 +57,9 @@ 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);
+
 static void*
 current_kernel_malloc(size_t size, const char* tag) {
   return rust_task_thread::get_task()->malloc(size, tag);
@@ -68,6 +71,11 @@ current_kernel_free(void* ptr) {
   rust_task_thread::get_task()->free(ptr);
 }
 */
+#define RUST_UV_HANDLE_LEN 16
+struct async_data {
+	uint8_t id_buf[RUST_UV_HANDLE_LEN];
+	rust_async_cb cb;
+};
 
 extern "C" void*
 rust_uvtmp_uv_loop_new() {
@@ -79,11 +87,11 @@ rust_uvtmp_uv_loop_set_data(uv_loop_t* loop, void* data) {
     loop->data = data;
 }
 
-typedef void (*async_op_cb)(void* data);
-void native_async_op_cb(uv_async_t* handle, int status) {
+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(loop_data);
+	cb(handle->loop, loop_data);
 }
 
 extern "C" void*
@@ -92,6 +100,8 @@ rust_uvtmp_uv_bind_op_cb(uv_loop_t* loop, async_op_cb cb) {
 		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
 	async->data = (void*)cb;
 	return async;
 }
@@ -105,6 +115,30 @@ rust_uvtmp_uv_async_send(uv_async_t* handle) {
     uv_async_send(handle);
 }
 
+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_async_init(uv_loop_t* loop, rust_async_cb cb,
+						 uint8_t* buf) {
+    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");
+	memcpy(data->id_buf, buf, RUST_UV_HANDLE_LEN);
+	data->cb = cb;
+	async->data = data;
+
+	return async;
+}
+
 // UVTMP REWORK
 
 // FIXME: Copied from rust_builtins.cpp. Could bitrot easily