V8 Multithreaded function

南楼画角 提交于 2019-11-29 08:04:45

You probably need a bit of libuv magic to get the main node.js/v8 thread to execute your callback from another thread. This will involve:

  • A uv_async_t handle which acts as the wake-up call for the main v8 thread:

    extern uv_async_t       async;
    
  • A uv_async_init call which binds the uv_async_t to the V8 default loop:

    uv_async_init(uv_default_loop(), &async, async_cb_handler);
    
  • And a event handler which will act on the uvasync_t event on the v8 main thread:

    void async_cb_handler(uv_async_t *handle) {
        NotifInfo *notif;
        mutex::scoped_lock sl(zqueue_mutex);
        while (!zqueue.empty()) {
            notif = zqueue.front();
            handleNotification(notif);
            delete notif;
            zqueue.pop();
        }
    }
    
  • Finally, you'll also probably need a mutex-protected queue to be able to pass some data from the c++ addon thread to Node/V8:

    extern mutex                   zqueue_mutex;
    extern std::queue<NotifInfo *> zqueue;
    
  • When something happens in your C++ thread, just push a new item to the mutex-protected queue, and call uv_async_send to wake up V8's default event loop (the so called 'main thread') to process the item (which can then call your Javascript callbacks)

    void ozw_watcher_callback(OpenZWave::Notification const *cb, void *ctx) {
        NotifInfo *notif = new NotifInfo();
        notif->type   = cb->GetType();
        notif->homeid = cb->GetHomeId();
        ...
        mutex::scoped_lock sl(zqueue_mutex);
        zqueue.push(notif);
        uv_async_send(&async);
    }
    

(Code snippets taken from the official Node.JS addon for OpenZWave)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!