6

I am newbie in nodeJS and node extension. I m writing a native extension for node js which will receives a callback on an virtual function OnEvent(param1, param2, param3). The code follows:

bool MyExt::OnEvent(int eventType, string param1, string param2)
{
    printf("MyExt:: onevent___ \n");
    {
        //// Crashes here, but if I use Locker, it get stuck!!!!!!
        //Locker l;
        Local<Value> argv[3] = {
            Local<Value>::New(Integer::New(1)),
            Local<Value>::New(String::New("parameter 1")),
            Local<String>::New(String::New("parameter 2"))
        };

        TryCatch try_catch;
        //// I need to call this
        m_EventCallback->Call(Context::GetCurrent()->Global(), 3, argv);
        if (try_catch.HasCaught()){
                printf("Callback is Exception()  \n");
        }
        printf("Callback is IsCallable() \n");
    }
    return true;
}

I need to forward this callback parameters to the server script using m_EventCallback.The function bool OnEvent is called from a different thread.

I tried using uv_async_send but couldn't succeed.

Any help or guidance will be appreciated.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
Rahul Shukla
  • 1,292
  • 9
  • 25

2 Answers2

6

Using uv_async_send is the correct way:

  • call uv_async_init on the 'main' thread.
  • then call uv_async_send from your worker.
  • don't forget uv_close back on main.

http://nikhilm.github.com/uvbook/threads.html

laktak
  • 57,064
  • 17
  • 134
  • 164
  • After posting the question, I studied a bit more and implemented using uv_async_send only. Using following { /// in Module::Init function uv_async_init(uv_default_loop(), &b.async_request, async_cb); /// in the onEvent function uv_async_send(&b.async_request); } But I am not sure where to put uv_close()? In async_cb function??? – Rahul Shukla Mar 29 '13 at 11:25
  • Yes you should add the uv_close in the async_cb function – Nikhil Mar 29 '13 at 23:48
  • @trivalent Since you were able to get this working, any chance for a complete example? Thanks! – logidelic May 05 '16 at 19:44
  • To answer my own question: http://stackoverflow.com/questions/36987273/callback-nodejs-javascript-function-from-multithreaded-c-addon/37146620#37146620 – logidelic May 10 '16 at 20:27
1

Maybe uv_callback would be an option.

With it we can call functions on other threads.

It can handle non-coalescing calls.

We can even get the result in the caller thread on another function asynchronously. eg:

In the called thread:

uv_callback_t send_data;

void * on_data(uv_callback_t *handle, void *data) {
  int result = do_something(data);
  free(data);
  return (void*)result;
}

uv_callback_init(loop, &send_data, on_data, UV_DEFAULT);

In the calling thread:

uv_callback_t result_cb;

void * on_result(uv_callback_t *handle, void *result) {
  printf("The result is %d\n", (int)result);
}

uv_callback_init(loop, &result_cb, on_result, UV_DEFAULT);

uv_callback_fire(&send_data, data, &result_cb);
Bernardo Ramos
  • 4,048
  • 30
  • 28