V8 Multithreaded function

后端 未结 1 1976
野性不改
野性不改 2020-12-18 09:15

I\'m writing a Node plugin and I\'m having problems trying to call a V8 function object from a C++ worker thread.

My plugin basically starts a C++ std::thread and en

相关标签:
1条回答
  • 2020-12-18 09:24

    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)

    0 讨论(0)
提交回复
热议问题