| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -100,6 +100,44 @@ if (isMainThread) { | |||
| 100 | 100 | } | |
| 101 | 101 | ``` | |
| 102 | 102 | ||
| 103 | + ## `worker.isInternalThread` | ||
| 104 | + | ||
| 105 | + <!-- YAML | ||
| 106 | + added: REPLACEME | ||
| 107 | + --> | ||
| 108 | + | ||
| 109 | + * {boolean} | ||
| 110 | + | ||
| 111 | + Is `true` if this code is running inside of an internal [`Worker`][] thread (e.g the loader thread). | ||
| 112 | + | ||
| 113 | + ```bash | ||
| 114 | + node --experimental-loader ./loader.js main.js | ||
| 115 | + ``` | ||
| 116 | + | ||
| 117 | + ```cjs | ||
| 118 | + // loader.js | ||
| 119 | + const { isInternalThread } = require('node:worker_threads'); | ||
| 120 | + console.log(isInternalThread); // true | ||
| 121 | + ``` | ||
| 122 | + | ||
| 123 | + ```mjs | ||
| 124 | + // loader.js | ||
| 125 | + import { isInternalThread } from 'node:worker_threads'; | ||
| 126 | + console.log(isInternalThread); // true | ||
| 127 | + ``` | ||
| 128 | + | ||
| 129 | + ```cjs | ||
| 130 | + // main.js | ||
| 131 | + const { isInternalThread } = require('node:worker_threads'); | ||
| 132 | + console.log(isInternalThread); // false | ||
| 133 | + ``` | ||
| 134 | + | ||
| 135 | + ```mjs | ||
| 136 | + // main.js | ||
| 137 | + import { isInternalThread } from 'node:worker_threads'; | ||
| 138 | + console.log(isInternalThread); // false | ||
| 139 | + ``` | ||
| 140 | + | ||
| 103 | 141 | ## `worker.isMainThread` | |
| 104 | 142 | ||
| 105 | 143 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,6 +67,7 @@ const { | |||
| 67 | 67 | const { | |
| 68 | 68 | ownsProcessState, | |
| 69 | 69 | isMainThread, | |
| 70 | + isInternalThread, | ||
| 70 | 71 | resourceLimits: resourceLimitsRaw, | |
| 71 | 72 | threadId, | |
| 72 | 73 | Worker: WorkerImpl, | |
@@ -538,6 +539,7 @@ module.exports = { | |||
| 538 | 539 | ownsProcessState, | |
| 539 | 540 | kIsOnline, | |
| 540 | 541 | isMainThread, | |
| 542 | + isInternalThread, | ||
| 541 | 543 | SHARE_ENV, | |
| 542 | 544 | resourceLimits: | |
| 543 | 545 | !isMainThread ? makeResourceLimits(resourceLimitsRaw) : {}, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | + isInternalThread, | ||
| 4 | 5 | isMainThread, | |
| 5 | 6 | SHARE_ENV, | |
| 6 | 7 | resourceLimits, | |
@@ -29,6 +30,7 @@ const { | |||
| 29 | 30 | } = require('internal/buffer'); | |
| 30 | 31 | ||
| 31 | 32 | module.exports = { | |
| 33 | + isInternalThread, | ||
| 32 | 34 | isMainThread, | |
| 33 | 35 | MessagePort, | |
| 34 | 36 | MessageChannel, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,7 +54,8 @@ Worker::Worker(Environment* env, | |||
| 54 | 54 | std::shared_ptr<PerIsolateOptions> per_isolate_opts, | |
| 55 | 55 | std::vector<std::string>&& exec_argv, | |
| 56 | 56 | std::shared_ptr<KVStore> env_vars, | |
| 57 | - const SnapshotData* snapshot_data) | ||
| 57 | + const SnapshotData* snapshot_data, | ||
| 58 | + const bool is_internal) | ||
| 58 | 59 | : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER), | |
| 59 | 60 | per_isolate_opts_(per_isolate_opts), | |
| 60 | 61 | exec_argv_(exec_argv), | |
@@ -63,7 +64,8 @@ Worker::Worker(Environment* env, | |||
| 63 | 64 | name_(name), | |
| 64 | 65 | env_vars_(env_vars), | |
| 65 | 66 | embedder_preload_(env->embedder_preload()), | |
| 66 | - snapshot_data_(snapshot_data) { | ||
| 67 | + snapshot_data_(snapshot_data), | ||
| 68 | + is_internal_(is_internal) { | ||
| 67 | 69 | Debug(this, "Creating new worker instance with thread id %llu", | |
| 68 | 70 | thread_id_.id); | |
| 69 | 71 | ||
@@ -685,7 +687,8 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 685 | 687 | per_isolate_opts, | |
| 686 | 688 | std::move(exec_argv_out), | |
| 687 | 689 | env_vars, | |
| 688 | - snapshot_data); | ||
| 690 | + snapshot_data, | ||
| 691 | + is_internal); | ||
| 689 | 692 | ||
| 690 | 693 | CHECK(args[3]->IsFloat64Array()); | |
| 691 | 694 | Local<Float64Array> limit_info = args[3].As<Float64Array>(); | |
@@ -1028,6 +1031,16 @@ void CreateWorkerPerContextProperties(Local<Object> target, | |||
| 1028 | 1031 | Boolean::New(isolate, env->is_main_thread())) | |
| 1029 | 1032 | .Check(); | |
| 1030 | 1033 | ||
| 1034 | + Worker* worker = env->isolate_data()->worker_context(); | ||
| 1035 | + bool is_internal = worker != nullptr && worker->is_internal(); | ||
| 1036 | + | ||
| 1037 | + // Set the is_internal property | ||
| 1038 | + target | ||
| 1039 | + ->Set(env->context(), | ||
| 1040 | + FIXED_ONE_BYTE_STRING(isolate, "isInternalThread"), | ||
| 1041 | + Boolean::New(isolate, is_internal)) | ||
| 1042 | + .Check(); | ||
| 1043 | + | ||
| 1031 | 1044 | target | |
| 1032 | 1045 | ->Set(env->context(), | |
| 1033 | 1046 | FIXED_ONE_BYTE_STRING(isolate, "ownsProcessState"), | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,7 +34,8 @@ class Worker : public AsyncWrap { | |||
| 34 | 34 | std::shared_ptr<PerIsolateOptions> per_isolate_opts, | |
| 35 | 35 | std::vector<std::string>&& exec_argv, | |
| 36 | 36 | std::shared_ptr<KVStore> env_vars, | |
| 37 | - const SnapshotData* snapshot_data); | ||
| 37 | + const SnapshotData* snapshot_data, | ||
| 38 | + const bool is_internal); | ||
| 38 | 39 | ~Worker() override; | |
| 39 | 40 | ||
| 40 | 41 | // Run the worker. This is only called from the worker thread. | |
@@ -60,6 +61,7 @@ class Worker : public AsyncWrap { | |||
| 60 | 61 | ||
| 61 | 62 | bool is_stopped() const; | |
| 62 | 63 | const SnapshotData* snapshot_data() const { return snapshot_data_; } | |
| 64 | + bool is_internal() const { return is_internal_; } | ||
| 63 | 65 | ||
| 64 | 66 | static void New(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 65 | 67 | static void CloneParentEnvVars( | |
@@ -132,6 +134,7 @@ class Worker : public AsyncWrap { | |||
| 132 | 134 | Environment* env_ = nullptr; | |
| 133 | 135 | ||
| 134 | 136 | const SnapshotData* snapshot_data_ = nullptr; | |
| 137 | + const bool is_internal_; | ||
| 135 | 138 | friend class WorkerThreadData; | |
| 136 | 139 | }; | |
| 137 | 140 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + const { isInternalThread } = require('node:worker_threads'); | ||
| 2 | + | ||
| 3 | + console.log(`isInternalThread: ${isInternalThread}`); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + const { isInternalThread, parentPort } = require('node:worker_threads'); | ||
| 2 | + | ||
| 3 | + parentPort.postMessage(`isInternalThread: ${isInternalThread}`); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,36 @@ | |||
| 1 | + import { spawnPromisified } from '../common/index.mjs'; | ||
| 2 | + import * as fixtures from '../common/fixtures.mjs'; | ||
| 3 | + import assert from 'node:assert'; | ||
| 4 | + import { execPath } from 'node:process'; | ||
| 5 | + import { describe, it } from 'node:test'; | ||
| 6 | + import { isInternalThread, Worker } from 'node:worker_threads'; | ||
| 7 | + import * as common from '../common/index.mjs'; | ||
| 8 | + | ||
| 9 | + describe('worker_threads.isInternalThread', { concurrency: !process.env.TEST_PARALLEL }, () => { | ||
| 10 | + it('should be true inside the loader thread', async () => { | ||
| 11 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ | ||
| 12 | + '--no-warnings', | ||
| 13 | + '--experimental-loader', | ||
| 14 | + fixtures.fileURL('loader-is-internal-thread.js'), | ||
| 15 | + '--eval', | ||
| 16 | + 'setTimeout(() => {},99)', | ||
| 17 | + ]); | ||
| 18 | + | ||
| 19 | + assert.strictEqual(stderr, ''); | ||
| 20 | + assert.match(stdout, /isInternalThread: true/); | ||
| 21 | + assert.strictEqual(code, 0); | ||
| 22 | + assert.strictEqual(signal, null); | ||
| 23 | + }); | ||
| 24 | + | ||
| 25 | + it('should be false inside the main thread', async () => { | ||
| 26 | + assert.strictEqual(isInternalThread, false); | ||
| 27 | + }); | ||
| 28 | + | ||
| 29 | + it('should be false inside a regular worker thread', async () => { | ||
| 30 | + const worker = new Worker(fixtures.path('worker-is-internal-thread.js')); | ||
| 31 | + | ||
| 32 | + worker.on('message', common.mustCall((message) => { | ||
| 33 | + assert.strictEqual(message, 'isInternalThread: false'); | ||
| 34 | + })); | ||
| 35 | + }); | ||
| 36 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments