diff --git a/05_threadpool/modulename.cpp b/05_threadpool/modulename.cpp index c1bb60b..cf49c1f 100644 --- a/05_threadpool/modulename.cpp +++ b/05_threadpool/modulename.cpp @@ -12,9 +12,6 @@ void AsyncAfter(uv_work_t* req); // We use a struct to store information about the asynchronous "work request". struct Baton { - // libuv's request struct. - uv_work_t request; - // This handle holds the callback function we'll call after the work request // has been completed in a threadpool thread. It's persistent so that V8 // doesn't garbage collect it away while our request waits to be processed. @@ -43,16 +40,22 @@ Handle Async(const Arguments& args) { // There's no ToFunction(), use a Cast instead. Local callback = Local::Cast(args[0]); - // This creates our work request, including the libuv struct. + // The baton holds our custom status information for this asynchronous call, + // like the callback function we want to call when returning to the main + // thread and the status information. Baton* baton = new Baton(); baton->error = false; - baton->request.data = baton; baton->callback = Persistent::New(callback); + // This creates the work request struct. + uv_work_t *req = new uv_work_t(); + req->data = baton; + // Schedule our work request with libuv. Here you can specify the functions // that should be executed in the threadpool and back in the main thread // after the threadpool function completed. - int status = uv_queue_work(uv_default_loop(), &baton->request, AsyncWork, AsyncAfter); + int status = uv_queue_work(uv_default_loop(), req, AsyncWork, + (uv_after_work_cb)AsyncAfter); assert(status == 0); return Undefined(); @@ -119,7 +122,11 @@ void AsyncAfter(uv_work_t* req) { // The callback is a permanent handle, so we have to dispose of it manually. baton->callback.Dispose(); + + // We also created the baton and the work_req struct with new, so we have to + // manually delete both. delete baton; + delete req; } void RegisterModule(Handle target) { diff --git a/README.md b/README.md index 505e674..65f0fb8 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,6 @@ Further references: * [V8 Doxygen](http://izs.me/v8-docs/main.html) * [uv.h](https://github.com/joyent/libuv/blob/master/include/uv.h) -* [libev](http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod) +* [An introduction to libuv](http://nikhilm.github.com/uvbook/) * [v8 Cookbook](http://create.tpsitulsa.com/wiki/V8_Cookbook) * [Google's C++ style guide](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml) diff --git a/xx_benchmark/modulename.cpp b/xx_benchmark/modulename.cpp index 7b2bb95..253344c 100644 --- a/xx_benchmark/modulename.cpp +++ b/xx_benchmark/modulename.cpp @@ -56,7 +56,8 @@ Handle Fn3(const Arguments& args) { baton->request.data = baton; Local callback = Local::Cast(args[0]); baton->callback = Persistent::New(callback); - uv_queue_work(uv_default_loop(), &baton->request, Fn3Work, Fn3After); + uv_queue_work(uv_default_loop(), &baton->request, Fn3Work, + (uv_after_work_cb)Fn3After); return Undefined(); }