From b7685b69c2a4157384167169cb3501bef1b7256d Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 7 Oct 2018 16:51:12 -0700 Subject: [PATCH] squash! worker: improve integration with native addons --- doc/api/addons.md | 18 ++++++++++++++++++ doc/api/worker_threads.md | 10 ++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/doc/api/addons.md b/doc/api/addons.md index b2c52d5128b5bc..334c0642b05cb1 100644 --- a/doc/api/addons.md +++ b/doc/api/addons.md @@ -232,6 +232,23 @@ NODE_MODULE_INIT(/* exports, module, context */) { } ``` +#### Worker support + +In order to support [`Worker`][] threads, addons need to clean up any resources +they may have allocated when such a thread exists. This can be achieved through +the usage of the `AddEnvironmentCleanupHook()` function: + +```c++ +void AddEnvironmentCleanupHook(v8::Isolate* isolate, + void (*fun)(void* arg), + void* arg); +``` + +This function adds a hook that will run before a given Node.js instance shuts +down. If necessary, such hooks can be removed using +`RemoveEnvironmentCleanupHook()` before they are run, which has the same +signature. + ### Building Once the source code has been written, it must be compiled into the binary @@ -1316,6 +1333,7 @@ Test in JavaScript by running: require('./build/Release/addon'); ``` +[`Worker`]: worker_threads.html#worker_threads_class_worker [Electron]: https://electronjs.org/ [Embedder's Guide]: https://github.com/v8/v8/wiki/Embedder's%20Guide [Linking to Node.js' own dependencies]: #addons_linking_to_node_js_own_dependencies diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 37751c21bbb44d..4beae369ebaf8b 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -253,11 +253,9 @@ Notable differences inside a Worker environment are: - Execution may stop at any point as a result of [`worker.terminate()`][] being invoked. - IPC channels from parent processes are not accessible. - -Currently, the following differences also exist until they are addressed: - -- The [`inspector`][] module is not available yet. -- Native addons are not supported yet. +- Native add-ons are unloaded if they are only used inside `Worker` instances + when those workers shut down. + See the [addons documentation][] for more detials. Creating `Worker` instances inside of other `Worker`s is possible. @@ -484,7 +482,7 @@ active handle in the event system. If the worker is already `unref()`ed calling [`require('worker_threads').parentPort`]: #worker_threads_worker_parentport [`require('worker_threads').threadId`]: #worker_threads_worker_threadid [`cluster` module]: cluster.html -[`inspector`]: inspector.html +[addons documentation]: addons.html#addons_worker_support [v8.serdes]: v8.html#v8_serialization_api [`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer [Signals events]: process.html#process_signal_events