Friday 9 June 2017

Node cpp extension threads

everybody

Trying to write my first node extension for dynamic library ("/usr/local/lib/libkdriveExpress.so")

This extension should connect to device and receive data. On received data it should call back function from javascript. I have found how to store persistent function so it isn't issue to call js function from cpp.

In this example I use simple c++ function just to except issues with js

/// 
void test(const uint8_t *telegram, uint32_t telegram_len, void *user_data){
    kdrive_logger_ex(KDRIVE_LOGGER_ERROR, "kdrive: event callback");
};
void ap_open_ip(const FunctionCallbackInfo <Value> &args) {
/// ...
/// now register
    kdrive_ap_register_telegram_callback(ap, &test, NULL, &key);
/// ...
}

when I call it from js it works fine.

if (kdrive.ap_open_ip(ap, "192.168.3.201") == 0) {
    console.log("great success", ap);
    setTimeout(function () {
        console.log(kdrive.whoami());
   }, 2000)
}

but when I call any function from extension it doesn't work. In the code above I call whoami function and it doesn't use dynamic library, just return string.

here is the log:

great success 1
11:42:45:518 [kdrive.express] kdrive: event callback
11:42:45:590 [kdrive.express] kdrive: event callback
11:42:45:692 [kdrive.express] kdrive: event callback
11:42:45:735 [kdrive.express] kdrive: event callback
11:42:45:831 [kdrive.express] kdrive: event callback
11:42:45:900 [kdrive.express] kdrive: event callback
11:42:46:167 [kdrive.express] kdrive: event callback
11:42:46:717 [kdrive.express] kdrive: event callback
>>> whoami call
11:42:47:525 [kdrive.connector.ConnectorNotificationHandler] onSignal: System exception: cannot lock mutex
11:42:48:067 [kdrive.connector.ConnectorNotificationHandler] onSignal: System exception: cannot lock mutex
11:42:48:420 [kdrive.connector.ConnectorNotificationHandler] onSignal: System exception: cannot lock mutex

Here is description for kdrive_ap_register_telegram_callback function:

Registers a callback function.

This callback function will be called when a telegram is received by the Access Port. A notification thread is used internally so this callback will be in the context of the notification thread (and not the main thread). That is, care should be taken when calling out from the callback. This function generates a unique key to represent the callback. This key can be used to remove the callback at a later state.

So, I guess issue is with threads. I'm newbie with cpp extensions and cpp, writed in js before. So, I guess, I need to learn how threads in cpp works and how v8 engine handle it.

Would appreciate any advice, suggestions.



via Vladimir Shabunin

No comments:

Post a Comment