Skip to content

Commit

Permalink
squash! worker: improve integration with native addons
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Oct 7, 2018
1 parent f93bca3 commit b7685b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
18 changes: 18 additions & 0 deletions doc/api/addons.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b7685b6

Please sign in to comment.