Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

worker: initial implementation (large/base PR) #58

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ test-with-async-hooks:
$(CI_JS_SUITES) \
$(CI_NATIVE_SUITES)

test-worker:
$(PYTHON) tools/test.py --mode=release workers

ifneq ("","$(wildcard deps/v8/tools/run-tests.py)")
test-v8: v8
Expand Down
7 changes: 7 additions & 0 deletions benchmark/fixtures/echo.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const worker = require('worker');

worker.on('workerMessage', (msg) => {
worker.postMessage(msg);
});
72 changes: 72 additions & 0 deletions benchmark/worker/echo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

const { Worker } = require('worker');
const common = require('../common.js');
const path = require('path');
const bench = common.createBenchmark(main, {
workers: [1],
payload: ['string', 'object'],
sendsPerBroadcast: [1, 10],
n: [1e5]
});

const workerPath = path.resolve(__dirname, '..', 'fixtures', 'echo.worker.js');

function main(conf) {
const n = +conf.n;
const workers = +conf.workers;
const sends = +conf.sendsPerBroadcast;
const expectedPerBroadcast = sends * workers;
var payload;
var readies = 0;
var broadcasts = 0;
var msgCount = 0;

switch (conf.payload) {
case 'string':
payload = 'hello world!';
break;
case 'object':
payload = { action: 'pewpewpew', powerLevel: 9001 };
break;
default:
throw new Error('Unsupported payload type');
}

const workerObjs = [];

for (var i = 0; i < workers; ++i) {
const worker = new Worker(workerPath);
workerObjs.push(worker);
worker.on('online', onOnline);
worker.on('message', onMessage);
}

function onOnline() {
if (++readies === workers) {
bench.start();
broadcast();
}
}

function broadcast() {
if (broadcasts++ === n) {
bench.end(n);
for (const worker of workerObjs) {
worker.unref();
}
return;
}
for (const worker of workerObjs) {
for (var i = 0; i < sends; ++i)
worker.postMessage(payload);
}
}

function onMessage() {
if (++msgCount === expectedPerBroadcast) {
msgCount = 0;
broadcast();
}
}
}
5 changes: 5 additions & 0 deletions common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
# Enable disassembler for `--print-code` v8 options
'v8_enable_disassembler': 1,

'v8_extra_library_files': [
'./lib/extras/events.js',
'./lib/extras/messaging.js'
],

# Don't bake anything extra into the snapshot.
'v8_use_external_startup_data%': 0,

Expand Down
1 change: 1 addition & 0 deletions doc/api/_toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* [Utilities](util.html)
* [V8](v8.html)
* [VM](vm.html)
* [Worker](worker.html)
* [ZLIB](zlib.html)

<div class="line"></div>
Expand Down
3 changes: 3 additions & 0 deletions doc/api/domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ will be notified, rather than losing the context of the error in the
`process.on('uncaughtException')` handler, or causing the program to
exit immediately with an error code.

*Note*: This module is not available in [`Worker`][]s.

## Warning: Don't Ignore Errors!

<!-- type=misc -->
Expand Down Expand Up @@ -495,3 +497,4 @@ rejections.
[`setInterval()`]: timers.html#timers_setinterval_callback_delay_args
[`setTimeout()`]: timers.html#timers_settimeout_callback_delay_args
[`throw`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw
[`Worker`]: #worker_class_worker
27 changes: 27 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ added: v0.7.0
The `process.abort()` method causes the Node.js process to exit immediately and
generate a core file.

*Note*: This feature is not available in [`Worker`][] threads.

## process.arch
<!-- YAML
added: v0.5.0
Expand Down Expand Up @@ -518,6 +520,8 @@ try {
}
```

*Note*: This feature is not available in [`Worker`][] threads.

## process.config
<!-- YAML
added: v0.7.7
Expand Down Expand Up @@ -904,6 +908,8 @@ console.log(process.env.test);
// => 1
```

*Note*: `process.env` is read-only in [`Worker`][] threads.

## process.execArgv
<!-- YAML
added: v0.7.7
Expand Down Expand Up @@ -1021,6 +1027,9 @@ If it is necessary to terminate the Node.js process due to an error condition,
throwing an *uncaught* error and allowing the process to terminate accordingly
is safer than calling `process.exit()`.

*Note*: in [`Worker`][] threads, this function stops the current thread rather than
the current process.

## process.exitCode
<!-- YAML
added: v0.11.8
Expand Down Expand Up @@ -1188,6 +1197,8 @@ console.log(process.getgroups()); // [ 27, 30, 46, 1000 ]
*Note*: This function is only available on POSIX platforms (i.e. not Windows
or Android).

*Note*: This feature is not available in [`Worker`][] threads.

## process.kill(pid[, signal])
<!-- YAML
added: v0.0.6
Expand Down Expand Up @@ -1506,6 +1517,7 @@ if (process.getegid && process.setegid) {
*Note*: This function is only available on POSIX platforms (i.e. not Windows
or Android).

*Note*: This feature is not available in [`Worker`][] threads.

## process.seteuid(id)
<!-- YAML
Expand Down Expand Up @@ -1534,6 +1546,8 @@ if (process.geteuid && process.seteuid) {
*Note*: This function is only available on POSIX platforms (i.e. not Windows
or Android).

*Note*: This feature is not available in [`Worker`][] threads.

## process.setgid(id)
<!-- YAML
added: v0.1.31
Expand Down Expand Up @@ -1561,6 +1575,8 @@ if (process.getgid && process.setgid) {
*Note*: This function is only available on POSIX platforms (i.e. not Windows
or Android).

*Note*: This feature is not available in [`Worker`][] threads.

## process.setgroups(groups)
<!-- YAML
added: v0.9.4
Expand All @@ -1577,6 +1593,8 @@ The `groups` array can contain numeric group IDs, group names or both.
*Note*: This function is only available on POSIX platforms (i.e. not Windows
or Android).

*Note*: This feature is not available in [`Worker`][] threads.

## process.setuid(id)
<!-- YAML
added: v0.1.28
Expand All @@ -1602,6 +1620,8 @@ if (process.getuid && process.setuid) {
*Note*: This function is only available on POSIX platforms (i.e. not Windows
or Android).

*Note*: This feature is not available in [`Worker`][] threads.


## process.stderr

Expand All @@ -1615,6 +1635,8 @@ a [Writable][] stream.
*Note*: `process.stderr` differs from other Node.js streams in important ways,
see [note on process I/O][] for more information.

*Note*: This feature is not available in [`Worker`][] threads.

## process.stdin

* {Stream}
Expand Down Expand Up @@ -1649,6 +1671,8 @@ For more information see [Stream compatibility][].
must call `process.stdin.resume()` to read from it. Note also that calling
`process.stdin.resume()` itself would switch stream to "old" mode.

*Note*: This feature is not available in [`Worker`][] threads.

## process.stdout

* {Stream}
Expand All @@ -1667,6 +1691,8 @@ process.stdin.pipe(process.stdout);
*Note*: `process.stdout` differs from other Node.js streams in important ways,
see [note on process I/O][] for more information.

*Note*: This feature is not available in [`Worker`][] threads.

### A note on process I/O

`process.stdout` and `process.stderr` differ from other Node.js streams in
Expand Down Expand Up @@ -1899,6 +1925,7 @@ cases:
[`require.main`]: modules.html#modules_accessing_the_main_module
[`require.resolve()`]: modules.html#modules_require_resolve
[`setTimeout(fn, 0)`]: timers.html#timers_settimeout_callback_delay_args
[`Worker`]: worker.html#worker_worker
[Child Process]: child_process.html
[Cluster]: cluster.html
[Duplex]: stream.html#stream_duplex_and_transform_streams
Expand Down
18 changes: 18 additions & 0 deletions doc/api/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,24 @@ console.log(util.inspect(sandbox));
// { globalVar: 1024 }
```

## vm.moveMessagePortToContext(port, contextifiedSandbox)
<!-- YAML
added: REPLACEME
-->

* `port` {MessagePort}
* `contextifiedSandbox` {Object} A contextified object as returned by the
`vm.createContext()` method.
* Returns: {MessagePort}

Bind a `MessagePort` to a specific VM context. This returns a new `MessagePort`
object, whose prototype and methods act as if they were created in the passed
context. The received messages will also be emitted as objects from the passed
context.

The `port` object on which this method was called can not be used for sending
or receiving further messages.

## vm.runInDebugContext(code)
<!-- YAML
added: v0.11.14
Expand Down
Loading