worker: initial implementation #20876

Closed
wants to merge 26 commits into
from

Conversation

@addaleax
Member

addaleax commented May 22, 2018

Hi everyone! 👋

This PR adds threading support for to Node.js. I realize that this is not exactly a small PR and is going to take a while to review, so: I appreciate comments, questions (any kind, as long as it’s somewhat related 😺), partial reviews and all other feedback from anybody, not just Node.js core collaborators.

The super-high-level description of the implementation here is that Workers can share and transfer memory, but not JS objects (they have to be cloned for transferring), and not yet handles like network sockets.

FAQ

See https://gist.github.com/benjamingr/3d5e86e2fb8ae4abe2ab98ffe4758665

Example usage

const { Worker, isMainThread, postMessage, workerData } = require('worker_threads');

if (isMainThread) {
  module.exports = async function parseJSAsync(script) {
    return new Promise((resolve, reject) => {
      const worker = new Worker(__filename, {
        workerData: script
      });
      worker.on('message', resolve);
      worker.on('error', reject);
      worker.on('exit', (code) => {
        if (code !== 0)
          reject(new Error(`Worker stopped with exit code ${code}`));
      });
    });
  };
} else {
  const { parse } = require('some-js-parsing-library');
  const script = workerData;
  postMessage(parse(script));
}

Feature set

The communication between threads largely builds on the MessageChannel Web API. Transferring ArrayBuffers and sharing memory through SharedArrayBuffers is supported.
Almost the entire Node.js core API is require()able or importable.

Some notable differences:

  • stdio streams may be captured by the parent thread.
  • Some functions, e.g. process.chdir() don’t exist in worker threads.
  • Native addons are not loadable from worker threads (yet).
  • No inspector support (yet).

(Keep in mind that PRs can change significantly based on reviews.)

Comparison with child_process and cluster

Workers are conceptually very similar to child_process and cluster.
Some of the key differences are:

  • Communication between Workers is different: Unlike child_process IPC, we don’t use JSON, but rather do the same thing that postMessage() does in browsers.
    • This isn’t necessarily faster, although it can be and there might be more room for optimization. (Keep in mind how long JSON has been around and how much work has therefore been put into making it fast.)
    • The serialized data doesn’t actually need to leave the process, so overall there’s less overhead in communication involved.
    • Memory in the form of typed arrays can be transferred or shared between Workers and/or the main thread, which enables really fast communication for specific use cases.
    • Handles, like network sockets, can not be transferred or shared (yet).
  • There are some limitations on the usable API within workers, since parts of it (e.g. process.chdir()) affect per-process state, loading native addons, etc.
  • Each workers have its own event loop, but some of the resources are shared between workers (e.g. the libuv thread pool for file system work)

Benchmarks

    $ ./node benchmark/cluster/echo.js
    cluster/echo.js n=100000 sendsPerBroadcast=1 payload="string" workers=1: 33,647.30473442063
    cluster/echo.js n=100000 sendsPerBroadcast=10 payload="string" workers=1: 12,927.907405288383
    cluster/echo.js n=100000 sendsPerBroadcast=1 payload="object" workers=1: 28,496.37373941151
    cluster/echo.js n=100000 sendsPerBroadcast=10 payload="object" workers=1: 8,975.53747186485
    $ ./node --experimental-worker benchmark/worker/echo.js
    worker/echo.js n=100000 sendsPerBroadcast=1 payload="string" workers=1: 88,044.32902365089
    worker/echo.js n=100000 sendsPerBroadcast=10 payload="string" workers=1: 39,873.33697018837
    worker/echo.js n=100000 sendsPerBroadcast=1 payload="object" workers=1: 64,451.29132425621
    worker/echo.js n=100000 sendsPerBroadcast=10 payload="object" workers=1: 22,325.635443739284

A caveat here is that startup performance for Workers using this model is still relatively slow (I don’t have exact numbers, but there’s definitely overhead).

Regarding semverness:

The only breaking change here is the introduction of a new top-level module. The name is currently worker, this is not under a scope as suggested in nodejs/TSC#389. It seems like the most natural name for this by far.

I’ve reached out to the owner of the worker module on npm, who declined to provide the name for this purpose – the package has 57 downloads/week, so whether we consider this semver-major because of that is probably a judgement call.

Alternatively, I’d suggest using workers – it’s not quite what we’re used to in core (e.g. child_process), but the corresponding npm package is essentially just a placeholder.

Acknowledgements

People I’d like to thank for their code, comments and reviews for this work in its original form, in no particular order:

… and finally @petkaantonov for a lot of inspiration and the ability to compare with previous work on this topic.

Individual commits

src: cleanup per-isolate state on platform on isolate unregister

Clean up once all references to an Isolate* are gone from the
NodePlatform, rather than waiting for the PerIsolatePlatformData
struct to be deleted since there may be cyclic references between
that struct and the individual tasks.

src: fix MallocedBuffer move assignment operator

src: break out of timers loop if !can_call_into_js()

Otherwise, this turns into an infinite loop.

src: simplify handle closing

Remove one extra closing state and use a smart pointer for
deleting HandleWraps.

worker: implement MessagePort and MessageChannel

Implement MessagePort and MessageChannel along the lines of
the DOM classes of the same names. MessagePorts initially
support transferring only ArrayBuffers.

worker: support MessagePort passing in messages

Support passing MessagePort instances through other MessagePorts,
as expected by the MessagePort spec.

worker: add SharedArrayBuffer sharing

Logic is added to the MessagePort mechanism that
attaches hidden objects to those instances when they are transferred
that track their lifetime and maintain a reference count, to make
sure that memory is freed at the appropriate times.

src: add Env::profiler_idle_notifier_started()

src: move DeleteFnPtr into util.h

This is more generally useful than just in a crypto context.

worker: initial implementation

Implement multi-threading support for most of the API.

test: add test against unsupported worker features

worker: restrict supported extensions

Only allow .js and .mjs extensions to provide future-proofing
for file type detection.

src: enable stdio for workers

Provide stdin, stdout and stderr options for the Worker
constructor, and make these available to the worker thread
under their usual names.

The default for stdin is an empty stream, the default for
stdout and stderr is redirecting to the parent thread’s
corresponding stdio streams.

benchmark: port cluster/echo to worker

worker: improve error (de)serialization

Rather than passing errors using some sort of string representation,
do a best effort for faithful serialization/deserialization of
uncaught exception objects.

test,tools: enable running tests under workers

Enable running tests inside workers by passing --worker
to tools/test.py. A number of tests are marked as skipped,
or have been slightly altered to fit the different environment.

Other work

I know that teams from Microsoft (/cc @fs-eire @helloshuangzi) and Alibaba (/cc @aaronleeatali) have been working on forms of multithreading that have higher degrees of interaction between threads, such as sharing code and JS objects. I’d love if you could take a look at this PR and see how well it aligns with your own work, and what conflicts there might be. (From what I’ve seen of the other code, I’m actually quite optimistic that this PR is just going to help everybody.)

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
@addaleax

This comment was marked as outdated.

Show comment
Hide comment
@addaleax

addaleax May 22, 2018

Member

CI: https://ci.nodejs.org/job/node-test-commit/18659/
CITGM: https://ci.nodejs.org/view/Node.js-citgm/job/citgm-smoker/1422/

Edit: Will go to sleep now and look at comments tomorrow. Cheers!

Member

addaleax commented May 22, 2018

CI: https://ci.nodejs.org/job/node-test-commit/18659/
CITGM: https://ci.nodejs.org/view/Node.js-citgm/job/citgm-smoker/1422/

Edit: Will go to sleep now and look at comments tomorrow. Cheers!

@devsnek

This comment has been minimized.

Show comment
Hide comment
@devsnek

devsnek May 22, 2018

Member

i'm so heckin' happy to see this 🎉 🎉 🎉 🎉

regarding the whole name thing, why not use @nodejs/workers? we're pratically being shoved into it due to the owner of workers not wanting to give that name up, and we've been looking for excuses to namespace builtin modules anyway.

Member

devsnek commented May 22, 2018

i'm so heckin' happy to see this 🎉 🎉 🎉 🎉

regarding the whole name thing, why not use @nodejs/workers? we're pratically being shoved into it due to the owner of workers not wanting to give that name up, and we've been looking for excuses to namespace builtin modules anyway.

doc/api/process.md
@@ -1204,6 +1213,8 @@ console.log(process.getgroups()); // [ 27, 30, 46, 1000 ]
This function is only available on POSIX platforms (i.e. not Windows or
Android).
+This feature is not available in [`Worker`][] threads.

This comment has been minimized.

@devsnek

devsnek May 22, 2018

Member

maybe we should set up some sort of doc macro for this so that we can keep them all in sync if we decide to change the formatting of it.

@devsnek

devsnek May 22, 2018

Member

maybe we should set up some sort of doc macro for this so that we can keep them all in sync if we decide to change the formatting of it.

This comment has been minimized.

@addaleax

addaleax May 22, 2018

Member

Do we know how to do that?

@addaleax

addaleax May 22, 2018

Member

Do we know how to do that?

This comment was marked as resolved.

@cedric05

cedric05 May 23, 2018

does node runs on android?
is it experimental?
where can i refer

@cedric05

cedric05 May 23, 2018

does node runs on android?
is it experimental?
where can i refer

This comment was marked as resolved.

@vsemozhetbyt

vsemozhetbyt May 23, 2018

Member

@cedric05 see BUILDING.md#androidandroid-based-devices-eg-firefox-os for building on Android and package-manager/#android for installing precompiled app.

@vsemozhetbyt

vsemozhetbyt May 23, 2018

Member

@cedric05 see BUILDING.md#androidandroid-based-devices-eg-firefox-os for building on Android and package-manager/#android for installing precompiled app.

lib/worker.js
+
+if (!process.binding('config').experimentalWorker) {
+ // TODO(addaleax): Is this the right way to do this?
+ // eslint-disable-next-line no-restricted-syntax

This comment has been minimized.

@devsnek

devsnek May 22, 2018

Member

i would try and gate it in the nativemodule loader instead

@devsnek

devsnek May 22, 2018

Member

i would try and gate it in the nativemodule loader instead

@@ -1375,6 +1375,8 @@ def BuildOptions():
help="Expect test cases to fail", default=False, action="store_true")
result.add_option("--valgrind", help="Run tests through valgrind",
default=False, action="store_true")
+ result.add_option("--worker", help="Run parallel tests inside a worker context",

This comment was marked as resolved.

@devsnek

devsnek May 22, 2018

Member

❤️

@devsnek

devsnek May 22, 2018

Member

❤️

doc/api/errors.md
+<a id="ERR_CLOSED_MESSAGE_PORT"></a>
+### ERR_CLOSED_MESSAGE_PORT
+
+Used when there was an attempt to use a `MessagePort` instance in a closed

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Nit: Remove Used when.

@Trott

Trott May 22, 2018

Member

Nit: Remove Used when.

doc/api/errors.md
+<a id="ERR_MISSING_PLATFORM_FOR_WORKER"></a>
+### ERR_MISSING_PLATFORM_FOR_WORKER
+
+The V8 platform used by this instance of Node does not support creating Workers.

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Nit: Node -> Node.js

@Trott

Trott May 22, 2018

Member

Nit: Node -> Node.js

doc/api/errors.md
+<a id="ERR_WORKER_DOMAIN"></a>
+### ERR_WORKER_DOMAIN
+
+Used when trying to access the `domain` module inside of a worker thread.

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Remove Used when and reword:

The `domain` module cannot be used inside of a worker thread.
@Trott

Trott May 22, 2018

Member

Remove Used when and reword:

The `domain` module cannot be used inside of a worker thread.

This comment has been minimized.

@addaleax

addaleax May 22, 2018

Member

This was one of two outdated error entries anyway. :) It might be cool if our linter could check that entries in this markdown file are also present in lib/internal/errors.h or src/node_errors.h

@addaleax

addaleax May 22, 2018

Member

This was one of two outdated error entries anyway. :) It might be cool if our linter could check that entries in this markdown file are also present in lib/internal/errors.h or src/node_errors.h

doc/api/errors.md
+<a id="ERR_WORKER_NEED_ABSOLUTE_PATH"></a>
+### ERR_WORKER_NEED_ABSOLUTE_PATH
+
+Used when the path for the main script of a worker is not an absolute path.

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Remove Used when

@Trott

Trott May 22, 2018

Member

Remove Used when

doc/api/errors.md
+<a id="ERR_WORKER_OUT_OF_MEMORY"></a>
+### ERR_WORKER_OUT_OF_MEMORY
+
+Used when a worker hits its memory limit.

This comment has been minimized.

@Trott

Trott May 22, 2018

Member
A worker has exceeded its memory limit.
@Trott

Trott May 22, 2018

Member
A worker has exceeded its memory limit.
doc/api/errors.md
+Used when a worker hits its memory limit.
+
+<a id="ERR_WORKER_UNAVAILABLE_FEATURE"></a>
+### ERR_WORKER_UNAVAILABLE_FEATURE

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Missing description?

@Trott

Trott May 22, 2018

Member

Missing description?

doc/api/errors.md
+<a id="ERR_WORKER_UNSERIALIZABLE_ERROR"></a>
+### ERR_WORKER_UNSERIALIZABLE_ERROR
+
+Used when all attempts at serializing an uncaught exception from a worker fail.

This comment has been minimized.

@Trott

Trott May 22, 2018

Member
All attempts to serialize an uncaught exception from a worker failed.

...or perhaps...

All attempts to serialize an uncaught worker exception failed.
@Trott

Trott May 22, 2018

Member
All attempts to serialize an uncaught exception from a worker failed.

...or perhaps...

All attempts to serialize an uncaught worker exception failed.
doc/api/errors.md
+<a id="ERR_WORKER_UNSUPPORTED_EXTENSION"></a>
+### ERR_WORKER_UNSUPPORTED_EXTENSION
+
+Used when the pathname used for the main script of a worker has an

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Remove Used when

@Trott

Trott May 22, 2018

Member

Remove Used when

doc/api/process.md
@@ -918,6 +922,8 @@ console.log(process.env.test);
// => 1
```
+*Note*: `process.env` is read-only in [`Worker`][] threads.

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

My opinion only but I prefer we leave out *Note*: in nearly all cases.

@Trott

Trott May 22, 2018

Member

My opinion only but I prefer we leave out *Note*: in nearly all cases.

This comment has been minimized.

@ChALkeR

ChALkeR May 22, 2018

Member

I believe *Note*: prefixes were eradicated earlier from the docs.

@ChALkeR

ChALkeR May 22, 2018

Member

I believe *Note*: prefixes were eradicated earlier from the docs.

doc/api/process.md
@@ -1030,6 +1036,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

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

My opinion only but I prefer we leave out *Note*: in nearly all cases.

@Trott

Trott May 22, 2018

Member

My opinion only but I prefer we leave out *Note*: in nearly all cases.

doc/api/worker.md
+added: REPLACEME
+-->
+
+Opposite of `unref`, calling `ref` on a previously `unref`d worker will *not*

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Nit: Add parentheses, so unfref() and ref(). Also elsewhere in this doc.

Nit: Change comma to period on this line.

Nit: We use `unref`ed rather than `unref'd` in existing documentation so use that here and in line 97, 105, and anywhere else for consistency.

@Trott

Trott May 22, 2018

Member

Nit: Add parentheses, so unfref() and ref(). Also elsewhere in this doc.

Nit: Change comma to period on this line.

Nit: We use `unref`ed rather than `unref'd` in existing documentation so use that here and in line 97, 105, and anywhere else for consistency.

doc/api/worker.md
+
+Opposite of `unref`, calling `ref` on a previously `unref`d worker will *not*
+let the program exit if it's the only active handle left (the default behavior).
+If the worker is `ref`d calling `ref` again will have no effect.

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Add comma before "calling".

@Trott

Trott May 22, 2018

Member

Add comma before "calling".

doc/api/worker.md
+* Returns: {undefined}
+
+Disables further sending of messages on either side of the connection.
+This this method can be called once you know that no further communication

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Typo: "This this"

@Trott

Trott May 22, 2018

Member

Typo: "This this"

doc/api/worker.md
+behind this API, see the [serialization API of the `v8` module][v8.serdes].
+
+*Note*: Because the object cloning uses the structured clone algorithm,
+non-enumberable properties, property accessors, and object prototypes are

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Typo: non-enumberable -> non-enumerable

@Trott

Trott May 22, 2018

Member

Typo: non-enumberable -> non-enumerable

doc/api/worker.md
+- [`process.chdir()`][] as well as `process` methods that set group or user ids
+ are not available.
+- [`process.env`][] is a read-only reference to the environment variables.
+- [`process.title`][] can not be modified.

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

can not -> cannot

@Trott

Trott May 22, 2018

Member

can not -> cannot

doc/api/worker.md
+child thread.
+
+To create custom messaging channels (which is encouraged over using the default
+global channel because it facilitates seperation of concerns), users can create

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Typo: seperation -> separation

@Trott

Trott May 22, 2018

Member

Typo: seperation -> separation

doc/api/worker.md
+ * data {any} Any JavaScript value that will be cloned and made
+ available as [`require('worker').workerData`][]. The cloning will occur as
+ described in the [HTML structured clone algorithm][], and an error will be
+ thrown if the object can not be cloned (e.g. because it contains

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

can not -> cannot

@Trott

Trott May 22, 2018

Member

can not -> cannot

doc/api/worker.md
+-->
+
+The `'error'` event is emitted if the worker thread throws an uncaught
+expection. In that case, the worker will be terminated.

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Typo: expection -> exception

@Trott

Trott May 22, 2018

Member

Typo: expection -> exception

doc/api/worker.md
+If the port is `ref`d calling `ref` again will have no effect.
+
+If listeners are attached or removed using `.on('message')`, the port will
+be `ref()`ed and `unref()`ed automatically depending on whether

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Actually, I prefer this style most of all (as opposed to ref'd or ref'ed). Would be great to put this in the net docs and everywhere here.

@Trott

Trott May 22, 2018

Member

Actually, I prefer this style most of all (as opposed to ref'd or ref'ed). Would be great to put this in the net docs and everywhere here.

doc/api/worker.md
+ may be redirected by the parent thread.
+- The [`require('worker').isMainThread`][] property is set to `false`.
+- The [`require('worker').postMessage()`][] method is available and the
+ [`require('worker').on('workerMessage')`][] event is being emitted.

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

is being -> is?

@Trott

Trott May 22, 2018

Member

is being -> is?

doc/api/worker.md
+- Signals will not be delivered through [`process.on('...')`][Signals events].
+- Native addons are not supported.
+- Execution may stop at any point as a result of the [`worker.terminate()`][]
+ method being invoked.

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

Nit/suggestion: Remove the on previous line and method on this line? (It's obviously a method due to the ().)

@Trott

Trott May 22, 2018

Member

Nit/suggestion: Remove the on previous line and method on this line? (It's obviously a method due to the ().)

doc/api/worker.md
+ [`require('worker').on('workerMessage')`][] event is being emitted.
+- [`process.exit()`][] does not stop the whole program, just the single thread,
+ and [`process.abort()`][] is not available.
+- [`process.chdir()`][] as well as `process` methods that set group or user ids

This comment has been minimized.

@Trott

Trott May 22, 2018

Member

as well as -> and

@Trott

Trott May 22, 2018

Member

as well as -> and

@Trott

This comment has been minimized.

Show comment
Hide comment
@Trott

Trott May 22, 2018

Member

@addaleax If you would prefer me to apply the doc nits I suggested myself directly to your branch rather than review and apply them yourself, let me know.

Member

Trott commented May 22, 2018

@addaleax If you would prefer me to apply the doc nits I suggested myself directly to your branch rather than review and apply them yourself, let me know.

doc/api/worker.md
+can be accessed using:
+
+```js
+const worker = require('worker');

This comment has been minimized.

@ChALkeR

ChALkeR May 22, 2018

Member

If this is exposed without a flag, this is a semver-major change as I presume.
/cc @jasnell

$ npm info worker time.created time.modified
time.created = '2017-11-14T00:22:43.069Z'
time.modified = '2018-04-28T07:31:55.508Z'
@ChALkeR

ChALkeR May 22, 2018

Member

If this is exposed without a flag, this is a semver-major change as I presume.
/cc @jasnell

$ npm info worker time.created time.modified
time.created = '2017-11-14T00:22:43.069Z'
time.modified = '2018-04-28T07:31:55.508Z'

This comment has been minimized.

@addaleax

addaleax May 22, 2018

Member

As @benjamingr said below, this module is essentially not in public use, and the creation timestamp is, as I understand it, a bit misleading because the package name was only recently taken over by another author.

@addaleax

addaleax May 22, 2018

Member

As @benjamingr said below, this module is essentially not in public use, and the creation timestamp is, as I understand it, a bit misleading because the package name was only recently taken over by another author.

This comment has been minimized.

@jasnell

jasnell May 22, 2018

Member

That said, yes, the addition of a new top level module automatically makes this semver-major without TSC agreeing otherwise.

@jasnell

jasnell May 22, 2018

Member

That said, yes, the addition of a new top level module automatically makes this semver-major without TSC agreeing otherwise.

@benjamingr

This comment has been minimized.

Show comment
Hide comment
@benjamingr

benjamingr May 22, 2018

Member

@addaleax , I’ve been seeing the activity in your GH feed and waiting ❤️

Awesome work! You really stepped up (again) and tackled a complex topic that has traditionally very hard. Before this PR gets very technical I just want to say thank you.

Also, that if this ever gets “too much” like it did for @petkaantonov - please call it out and speak up.

Member

benjamingr commented May 22, 2018

@addaleax , I’ve been seeing the activity in your GH feed and waiting ❤️

Awesome work! You really stepped up (again) and tackled a complex topic that has traditionally very hard. Before this PR gets very technical I just want to say thank you.

Also, that if this ever gets “too much” like it did for @petkaantonov - please call it out and speak up.

@benjamingr

This comment has been minimized.

Show comment
Hide comment
@benjamingr

benjamingr May 22, 2018

Member

I’ve reached out to the owner of the worker module on npm, who declined to provide the name for this purpose – the package has 57 downloads/week, so whether we consider this semver-major because of that is probably a judgement call.

I also have no issue with just taking the library name from them if they are not willing to concede it. Especially since they are not the original owner of that name and no one is using it yet.

Member

benjamingr commented May 22, 2018

I’ve reached out to the owner of the worker module on npm, who declined to provide the name for this purpose – the package has 57 downloads/week, so whether we consider this semver-major because of that is probably a judgement call.

I also have no issue with just taking the library name from them if they are not willing to concede it. Especially since they are not the original owner of that name and no one is using it yet.

@addaleax

This comment was marked as resolved.

Show comment
Hide comment
@addaleax

addaleax May 22, 2018

Member

@Trott @devsnek I think I’ve addressed most of your comments in 2c8dceb and 3bc8bbb :)

Member

addaleax commented May 22, 2018

@Trott @devsnek I think I’ve addressed most of your comments in 2c8dceb and 3bc8bbb :)

@addaleax addaleax referenced this pull request May 22, 2018

Closed

src: fix MallocedBuffer move assignment operator #20883

2 of 2 tasks complete

shisama added a commit to shisama/node that referenced this pull request Jun 12, 2018

worker: improve error (de)serialization
Rather than passing errors using some sort of string representation,
do a best effort for faithful serialization/deserialization of
uncaught exception objects.

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 12, 2018

test,tools: enable running tests under workers
Enable running tests inside workers by passing `--worker`
to `tools/test.py`. A number of tests are marked as skipped,
or have been slightly altered to fit the different environment.

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 12, 2018

doc: explain Worker semantics in async_hooks.md
PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 12, 2018

worker: rename to worker_threads
PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

src: cleanup per-isolate state on platform on isolate unregister
Clean up once all references to an `Isolate*` are gone from the
`NodePlatform`, rather than waiting for the `PerIsolatePlatformData`
struct to be deleted since there may be cyclic references between
that struct and the individual tasks.

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

src: remove unused fields isolate_
Currently the following compiler warnings are generated:

In file included from ../src/node_platform.cc:1:
../src/node_platform.h:83:16:
warning: private field 'isolate_' is not used [-Wunused-private-field]
  v8::Isolate* isolate_;
               ^
1 warning generated.

This commit removes these unused private member.

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

src: simplify handle closing
Remove one extra closing state and use a smart pointer for
deleting `HandleWrap`s.

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

src: make handle onclose property a Symbol
This makes the property “more” hidden when exposing a `HandleWrap`
as public API, e.g. for upcoming `MessagePort`s.

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

worker: implement `MessagePort` and `MessageChannel`
Implement `MessagePort` and `MessageChannel` along the lines of
the DOM classes of the same names. `MessagePort`s initially
support transferring only `ArrayBuffer`s.

Thanks to Stephen Belanger for reviewing this change in its
original form, to Benjamin Gruenbaum for reviewing the
added tests in their original form, and to Olivia Hugger
for reviewing the documentation in its original form.

Refs: ayojs/ayo#98

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

worker: support MessagePort passing in messages
Support passing `MessagePort` instances through other `MessagePort`s,
as expected by the `MessagePort` spec.

Thanks to Stephen Belanger for reviewing this change in its original PR.

Refs: ayojs/ayo#106

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

worker: add `SharedArrayBuffer` sharing
Logic is added to the `MessagePort` mechanism that
attaches hidden objects to those instances when they are transferred
that track their lifetime and maintain a reference count, to make
sure that memory is freed at the appropriate times.

Thanks to Stephen Belanger for reviewing this change in its original PR.

Refs: ayojs/ayo#106

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

src: remove unused fields msg_ and env_
Currently the following compiler warnings are generated:

../src/node_messaging.cc:74:16:
warning: private field 'env_' is not used [-Wunused-private-field]
  Environment* env_;
               ^
../src/node_messaging.cc:75:12:
warning: private field 'msg_' is not used [-Wunused-private-field]
  Message* msg_;
           ^
2 warnings generated.

This commit removes these unused private members.

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

src: add Env::profiler_idle_notifier_started()
Refs: ayojs/ayo#93
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

worker: initial implementation
Implement multi-threading support for most of the API.

Thanks to Stephen Belanger for reviewing this change in its
original form, to Olivia Hugger for reviewing the
documentation and some of the tests coming along with it,
and to Alexey Orlenko and Timothy Gu for reviewing other
parts of the tests.

Refs: ayojs/ayo#110
Refs: ayojs/ayo#114
Refs: ayojs/ayo#117

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

test: add test against unsupported worker features
Refs: ayojs/ayo#113
Reviewed-By: Anna Henningsen <anna@addaleax.net>

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

worker: restrict supported extensions
Only allow `.js` and `.mjs` extensions to provide future-proofing
for file type detection.

Refs: ayojs/ayo#117
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Olivia Hugger <olivia@fastmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

worker: enable stdio
Provide `stdin`, `stdout` and `stderr` options for the `Worker`
constructor, and make these available to the worker thread
under their usual names.

The default for `stdin` is an empty stream, the default for
`stdout` and `stderr` is redirecting to the parent thread’s
corresponding stdio streams.

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

benchmark: port cluster/echo to worker
    $ ./node benchmark/cluster/echo.js
    cluster/echo.js n=100000 sendsPerBroadcast=1 payload="string" workers=1: 33,647.30473442063
    cluster/echo.js n=100000 sendsPerBroadcast=10 payload="string" workers=1: 12,927.907405288383
    cluster/echo.js n=100000 sendsPerBroadcast=1 payload="object" workers=1: 28,496.37373941151
    cluster/echo.js n=100000 sendsPerBroadcast=10 payload="object" workers=1: 8,975.53747186485
    $ ./node --experimental-worker benchmark/worker/echo.js
    worker/echo.js n=100000 sendsPerBroadcast=1 payload="string" workers=1: 88,044.32902365089
    worker/echo.js n=100000 sendsPerBroadcast=10 payload="string" workers=1: 39,873.33697018837
    worker/echo.js n=100000 sendsPerBroadcast=1 payload="object" workers=1: 64,451.29132425621
    worker/echo.js n=100000 sendsPerBroadcast=10 payload="object" workers=1: 22,325.635443739284

Refs: ayojs/ayo#115
Reviewed-By: Anna Henningsen <anna@addaleax.net>

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

worker: improve error (de)serialization
Rather than passing errors using some sort of string representation,
do a best effort for faithful serialization/deserialization of
uncaught exception objects.

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

test,tools: enable running tests under workers
Enable running tests inside workers by passing `--worker`
to `tools/test.py`. A number of tests are marked as skipped,
or have been slightly altered to fit the different environment.

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

doc: explain Worker semantics in async_hooks.md
PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

targos added a commit that referenced this pull request Jun 13, 2018

worker: rename to worker_threads
PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

src: cleanup per-isolate state on platform on isolate unregister
Clean up once all references to an `Isolate*` are gone from the
`NodePlatform`, rather than waiting for the `PerIsolatePlatformData`
struct to be deleted since there may be cyclic references between
that struct and the individual tasks.

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

src: remove unused fields isolate_
Currently the following compiler warnings are generated:

In file included from ../src/node_platform.cc:1:
../src/node_platform.h:83:16:
warning: private field 'isolate_' is not used [-Wunused-private-field]
  v8::Isolate* isolate_;
               ^
1 warning generated.

This commit removes these unused private member.

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

src: simplify handle closing
Remove one extra closing state and use a smart pointer for
deleting `HandleWrap`s.

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

src: make handle onclose property a Symbol
This makes the property “more” hidden when exposing a `HandleWrap`
as public API, e.g. for upcoming `MessagePort`s.

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

worker: implement `MessagePort` and `MessageChannel`
Implement `MessagePort` and `MessageChannel` along the lines of
the DOM classes of the same names. `MessagePort`s initially
support transferring only `ArrayBuffer`s.

Thanks to Stephen Belanger for reviewing this change in its
original form, to Benjamin Gruenbaum for reviewing the
added tests in their original form, and to Olivia Hugger
for reviewing the documentation in its original form.

Refs: ayojs/ayo#98

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

worker: support MessagePort passing in messages
Support passing `MessagePort` instances through other `MessagePort`s,
as expected by the `MessagePort` spec.

Thanks to Stephen Belanger for reviewing this change in its original PR.

Refs: ayojs/ayo#106

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

worker: add `SharedArrayBuffer` sharing
Logic is added to the `MessagePort` mechanism that
attaches hidden objects to those instances when they are transferred
that track their lifetime and maintain a reference count, to make
sure that memory is freed at the appropriate times.

Thanks to Stephen Belanger for reviewing this change in its original PR.

Refs: ayojs/ayo#106

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

src: remove unused fields msg_ and env_
Currently the following compiler warnings are generated:

../src/node_messaging.cc:74:16:
warning: private field 'env_' is not used [-Wunused-private-field]
  Environment* env_;
               ^
../src/node_messaging.cc:75:12:
warning: private field 'msg_' is not used [-Wunused-private-field]
  Message* msg_;
           ^
2 warnings generated.

This commit removes these unused private members.

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

src: add Env::profiler_idle_notifier_started()
Refs: ayojs/ayo#93
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

worker: initial implementation
Implement multi-threading support for most of the API.

Thanks to Stephen Belanger for reviewing this change in its
original form, to Olivia Hugger for reviewing the
documentation and some of the tests coming along with it,
and to Alexey Orlenko and Timothy Gu for reviewing other
parts of the tests.

Refs: ayojs/ayo#110
Refs: ayojs/ayo#114
Refs: ayojs/ayo#117

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

test: add test against unsupported worker features
Refs: ayojs/ayo#113
Reviewed-By: Anna Henningsen <anna@addaleax.net>

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

worker: restrict supported extensions
Only allow `.js` and `.mjs` extensions to provide future-proofing
for file type detection.

Refs: ayojs/ayo#117
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Olivia Hugger <olivia@fastmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

worker: enable stdio
Provide `stdin`, `stdout` and `stderr` options for the `Worker`
constructor, and make these available to the worker thread
under their usual names.

The default for `stdin` is an empty stream, the default for
`stdout` and `stderr` is redirecting to the parent thread’s
corresponding stdio streams.

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

benchmark: port cluster/echo to worker
    $ ./node benchmark/cluster/echo.js
    cluster/echo.js n=100000 sendsPerBroadcast=1 payload="string" workers=1: 33,647.30473442063
    cluster/echo.js n=100000 sendsPerBroadcast=10 payload="string" workers=1: 12,927.907405288383
    cluster/echo.js n=100000 sendsPerBroadcast=1 payload="object" workers=1: 28,496.37373941151
    cluster/echo.js n=100000 sendsPerBroadcast=10 payload="object" workers=1: 8,975.53747186485
    $ ./node --experimental-worker benchmark/worker/echo.js
    worker/echo.js n=100000 sendsPerBroadcast=1 payload="string" workers=1: 88,044.32902365089
    worker/echo.js n=100000 sendsPerBroadcast=10 payload="string" workers=1: 39,873.33697018837
    worker/echo.js n=100000 sendsPerBroadcast=1 payload="object" workers=1: 64,451.29132425621
    worker/echo.js n=100000 sendsPerBroadcast=10 payload="object" workers=1: 22,325.635443739284

Refs: ayojs/ayo#115
Reviewed-By: Anna Henningsen <anna@addaleax.net>

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

worker: improve error (de)serialization
Rather than passing errors using some sort of string representation,
do a best effort for faithful serialization/deserialization of
uncaught exception objects.

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

test,tools: enable running tests under workers
Enable running tests inside workers by passing `--worker`
to `tools/test.py`. A number of tests are marked as skipped,
or have been slightly altered to fit the different environment.

PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

doc: explain Worker semantics in async_hooks.md
PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>

shisama added a commit to shisama/node that referenced this pull request Jun 17, 2018

worker: rename to worker_threads
PR-URL: nodejs#20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment