diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2018-12-23 11:53:05 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2018-12-31 14:19:49 +0800 |
commit | af237152cdb0b7b5a4495e84cdf4b170f6ddd361 (patch) | |
tree | ec95c3845292c6f7f9efcc861efe14465e4e2f10 /lib/internal/process/worker_thread_only.js | |
parent | 7163fbf066fe3e84a2203c01a385df1765d53ab6 (diff) | |
download | android-node-v8-af237152cdb0b7b5a4495e84cdf4b170f6ddd361.tar.gz android-node-v8-af237152cdb0b7b5a4495e84cdf4b170f6ddd361.tar.bz2 android-node-v8-af237152cdb0b7b5a4495e84cdf4b170f6ddd361.zip |
process: split worker IO into internal/worker/io.js
- Move `setupProcessStdio` which contains write access to
the process object into `bootstrap/node.js`
- Move `MessagePort`, `MessageChannel`, `ReadableWorkerStdio`,
and `WritableWorkerStdio` into `internal/worker/io.js`
- Move more worker-specific bootstrap code into
`internal/process/worker_thread_only` from `setupChild`
in `internal/worker.js`, and move the `process._fatalException`
overwrite into `bootstrap/node.js` for clarity.
PR-URL: https://github.com/nodejs/node/pull/25199
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/process/worker_thread_only.js')
-rw-r--r-- | lib/internal/process/worker_thread_only.js | 51 |
1 files changed, 41 insertions, 10 deletions
diff --git a/lib/internal/process/worker_thread_only.js b/lib/internal/process/worker_thread_only.js index 834ba6078f..a9332fb427 100644 --- a/lib/internal/process/worker_thread_only.js +++ b/lib/internal/process/worker_thread_only.js @@ -2,23 +2,54 @@ // This file contains process bootstrappers that can only be // run in the worker thread. +const { + getEnvMessagePort, + threadId +} = internalBinding('worker'); + +const debug = require('util').debuglog('worker'); const { - setupProcessStdio -} = require('internal/process/stdio'); + kWaitingStreams, + ReadableWorkerStdio, + WritableWorkerStdio +} = require('internal/worker/io'); const { - workerStdio + createMessageHandler, + createWorkerFatalExeception } = require('internal/worker'); -function setupStdio() { - setupProcessStdio({ - getStdout: () => workerStdio.stdout, - getStderr: () => workerStdio.stderr, - getStdin: () => workerStdio.stdin - }); +const workerStdio = {}; + +function initializeWorkerStdio() { + const port = getEnvMessagePort(); + port[kWaitingStreams] = 0; + workerStdio.stdin = new ReadableWorkerStdio(port, 'stdin'); + workerStdio.stdout = new WritableWorkerStdio(port, 'stdout'); + workerStdio.stderr = new WritableWorkerStdio(port, 'stderr'); + + return { + getStdout() { return workerStdio.stdout; }, + getStderr() { return workerStdio.stderr; }, + getStdin() { return workerStdio.stdin; } + }; +} + +function setup() { + debug(`[${threadId}] is setting up worker child environment`); + + const port = getEnvMessagePort(); + const publicWorker = require('worker_threads'); + port.on('message', createMessageHandler(publicWorker, port, workerStdio)); + port.start(); + + return { + workerFatalExeception: createWorkerFatalExeception(port) + }; } module.exports = { - setupStdio + initializeWorkerStdio, + setup }; |