Skip to content

Commit b7685b6

Browse files
committed
squash! worker: improve integration with native addons
1 parent f93bca3 commit b7685b6

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

doc/api/addons.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,23 @@ NODE_MODULE_INIT(/* exports, module, context */) {
232232
}
233233
```
234234

235+
#### Worker support
236+
237+
In order to support [`Worker`][] threads, addons need to clean up any resources
238+
they may have allocated when such a thread exists. This can be achieved through
239+
the usage of the `AddEnvironmentCleanupHook()` function:
240+
241+
```c++
242+
void AddEnvironmentCleanupHook(v8::Isolate* isolate,
243+
void (*fun)(void* arg),
244+
void* arg);
245+
```
246+
247+
This function adds a hook that will run before a given Node.js instance shuts
248+
down. If necessary, such hooks can be removed using
249+
`RemoveEnvironmentCleanupHook()` before they are run, which has the same
250+
signature.
251+
235252
### Building
236253
237254
Once the source code has been written, it must be compiled into the binary
@@ -1316,6 +1333,7 @@ Test in JavaScript by running:
13161333
require('./build/Release/addon');
13171334
```
13181335

1336+
[`Worker`]: worker_threads.html#worker_threads_class_worker
13191337
[Electron]: https://electronjs.org/
13201338
[Embedder's Guide]: https://github.com/v8/v8/wiki/Embedder's%20Guide
13211339
[Linking to Node.js' own dependencies]: #addons_linking_to_node_js_own_dependencies

doc/api/worker_threads.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,9 @@ Notable differences inside a Worker environment are:
253253
- Execution may stop at any point as a result of [`worker.terminate()`][]
254254
being invoked.
255255
- IPC channels from parent processes are not accessible.
256-
257-
Currently, the following differences also exist until they are addressed:
258-
259-
- The [`inspector`][] module is not available yet.
260-
- Native addons are not supported yet.
256+
- Native add-ons are unloaded if they are only used inside `Worker` instances
257+
when those workers shut down.
258+
See the [addons documentation][] for more detials.
261259

262260
Creating `Worker` instances inside of other `Worker`s is possible.
263261

@@ -484,7 +482,7 @@ active handle in the event system. If the worker is already `unref()`ed calling
484482
[`require('worker_threads').parentPort`]: #worker_threads_worker_parentport
485483
[`require('worker_threads').threadId`]: #worker_threads_worker_threadid
486484
[`cluster` module]: cluster.html
487-
[`inspector`]: inspector.html
485+
[addons documentation]: addons.html#addons_worker_support
488486
[v8.serdes]: v8.html#v8_serialization_api
489487
[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
490488
[Signals events]: process.html#process_signal_events

0 commit comments

Comments
 (0)