Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Schedule async actions on the dedicated event loop
  • Loading branch information
srajko committed Dec 16, 2015
commit a53d6229c7062bac592d9a5bf6d651566c47db88
54 changes: 54 additions & 0 deletions generate/templates/manual/include/async_libgit2_queue_worker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef ASYNC_LIBGIT2_QUEUE_WORKER_H
#define ASYNC_LIBGIT2_QUEUE_WORKER_H

#include <nan.h>
#include <uv.h>
#include "nodegit.h"

// Frees the uv_async_t handle
NAN_INLINE void AsyncLibgit2Free(uv_handle_t* uv_async) {
// freeing the handle can crash the app... perhaps libuv needs it
// after the async callback returns, in which case we can't free it from
// within the async callback...
// free(uv_async);
}

// Runs WorkComplete of the scheduled AsyncWorker,
// and destroys it. This is run in the uv_default_loop event loop.
NAN_INLINE void AsyncLibgit2Complete (uv_async_t* uv_async) {
Nan::AsyncWorker *worker = static_cast<Nan::AsyncWorker*>(uv_async->data);
worker->WorkComplete();
worker->Destroy();
// it doesn't look like this is a good place to close the handle
// - it can cause the tests to hang - perhaps libuv expects it to be
// upen throughout the callback
// uv_close((uv_handle_t *)uv_async, AsyncLibgit2Free);
}

// Runs Execute of the scheduled AyncWorker on the dedicated libgit2 thread /
// event loop, and schedules the WorkComplete callback to run on the
// uv_default_loop event loop
NAN_INLINE void AsyncLibgit2Execute (uv_async_t* uv_async) {
Nan::AsyncWorker *worker = static_cast<Nan::AsyncWorker*>(uv_async->data);
worker->Execute();
// it doesn't look like this is a good place to clean up the handle
// - it can cause the tests to hang - perhaps libuv expects it to be
// upen throughout the callback
// uv_close((uv_handle_t *)uv_async, AsyncLibgit2Free);

// maybe we can reuse the first handle? not sure, making a new one
uv_async = (uv_async_t *)malloc(sizeof(uv_async_t));
uv_async_init(uv_default_loop(), uv_async, AsyncLibgit2Complete);
uv_async->data = worker;
uv_async_send(uv_async);
}

// Schedules the AsyncWorker to run on the dedicated libgit2 thread / event loop.
NAN_INLINE void AsyncLibgit2QueueWorker (Nan::AsyncWorker* worker) {
uv_async_t *uv_async = (uv_async_t *)malloc(sizeof(uv_async_t));
uv_async_init(libgit2_loop, uv_async, AsyncLibgit2Execute);
uv_async->data = worker;
uv_async_send(uv_async);
}

#endif
2 changes: 1 addition & 1 deletion generate/templates/partials/async_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) {
{%endif%}
{%endeach%}

Nan::AsyncQueueWorker(worker);
AsyncLibgit2QueueWorker(worker);
return;
}

Expand Down
1 change: 1 addition & 0 deletions generate/templates/templates/class_content.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {
#include "../include/functions/copy.h"
#include "../include/{{ filename }}.h"
#include "../include/functions/sleep_for_ms.h"
#include "../include/async_libgit2_queue_worker.h"

{% each dependencies as dependency %}
#include "{{ dependency }}"
Expand Down