From fd21429ef5f6c2c06ce9e108a8548a1703577714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sun, 16 Apr 2023 15:28:41 +0200 Subject: [PATCH] lib: update usage of always on Atomics API PR-URL: https://github.com/nodejs/node/pull/49639 Reviewed-By: Jiawen Geng Reviewed-By: Rafael Gonzaga Reviewed-By: Antoine du Hamel --- benchmark/worker/atomics-wait.js | 4 ---- lib/.eslintrc.yaml | 5 +---- lib/internal/freeze_intrinsics.js | 2 +- lib/internal/main/worker_thread.js | 6 +++--- lib/internal/modules/esm/hooks.js | 8 +++----- lib/internal/modules/esm/worker.js | 8 ++------ lib/internal/per_context/primordials.js | 1 + lib/internal/worker.js | 8 ++++---- test/parallel/test-worker-no-atomics.js | 21 --------------------- test/parallel/test-worker-process-cwd.js | 6 +++--- 10 files changed, 18 insertions(+), 51 deletions(-) delete mode 100644 test/parallel/test-worker-no-atomics.js diff --git a/benchmark/worker/atomics-wait.js b/benchmark/worker/atomics-wait.js index 2e6e01f881c66a..53d71ff9224a11 100644 --- a/benchmark/worker/atomics-wait.js +++ b/benchmark/worker/atomics-wait.js @@ -4,10 +4,6 @@ if (typeof SharedArrayBuffer === 'undefined') { throw new Error('SharedArrayBuffers must be enabled to run this benchmark'); } -if (typeof Atomics === 'undefined') { - throw new Error('Atomics must be enabled to run this benchmark'); -} - const common = require('../common.js'); const bench = common.createBenchmark(main, { n: [1e7], diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index 0d9443ed413912..5b7fa7d3882d7d 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -33,10 +33,6 @@ rules: message: Use `const { AbortController } = require('internal/abort_controller');` instead of the global. - name: AbortSignal message: Use `const { AbortSignal } = require('internal/abort_controller');` instead of the global. - # Atomics is not available in primordials because it can be - # disabled with --no-harmony-atomics CLI flag. - - name: Atomics - message: Use `const { Atomics } = globalThis;` instead of the global. - name: Blob message: Use `const { Blob } = require('buffer');` instead of the global. - name: BroadcastChannel @@ -193,6 +189,7 @@ rules: - name: AggregateError - name: Array - name: ArrayBuffer + - name: Atomics - name: BigInt - name: BigInt64Array - name: BigUint64Array diff --git a/lib/internal/freeze_intrinsics.js b/lib/internal/freeze_intrinsics.js index 793c19df1e9138..2017b8f66e84c0 100644 --- a/lib/internal/freeze_intrinsics.js +++ b/lib/internal/freeze_intrinsics.js @@ -31,6 +31,7 @@ const { ArrayPrototype, ArrayPrototypeForEach, ArrayPrototypePush, + Atomics, BigInt, BigInt64Array, BigInt64ArrayPrototype, @@ -128,7 +129,6 @@ const { } = primordials; const { - Atomics, Intl, SharedArrayBuffer, WebAssembly, diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js index 12ae4a9b23212d..0dda4c760e4acb 100644 --- a/lib/internal/main/worker_thread.js +++ b/lib/internal/main/worker_thread.js @@ -7,12 +7,12 @@ const { ArrayPrototypeForEach, ArrayPrototypePushApply, ArrayPrototypeSplice, + AtomicsLoad, ObjectDefineProperty, PromisePrototypeThen, RegExpPrototypeExec, SafeWeakMap, globalThis: { - Atomics, SharedArrayBuffer, }, } = primordials; @@ -112,7 +112,7 @@ port.on('message', (message) => { require('internal/worker').assignEnvironmentData(environmentData); - if (SharedArrayBuffer !== undefined && Atomics !== undefined) { + if (SharedArrayBuffer !== undefined) { // The counter is only passed to the workers created by the main thread, // not to workers created by other workers. let cachedCwd = ''; @@ -120,7 +120,7 @@ port.on('message', (message) => { const originalCwd = process.cwd; process.cwd = function() { - const currentCounter = Atomics.load(cwdCounter, 0); + const currentCounter = AtomicsLoad(cwdCounter, 0); if (currentCounter === lastCounter) return cachedCwd; lastCounter = currentCounter; diff --git a/lib/internal/modules/esm/hooks.js b/lib/internal/modules/esm/hooks.js index 8dd81ca52c5318..7e1a06c12e0a74 100644 --- a/lib/internal/modules/esm/hooks.js +++ b/lib/internal/modules/esm/hooks.js @@ -3,6 +3,9 @@ const { ArrayPrototypePush, ArrayPrototypePushApply, + AtomicsLoad, + AtomicsWait, + AtomicsWaitAsync, Int32Array, ObjectAssign, ObjectDefineProperty, @@ -15,11 +18,6 @@ const { } = primordials; const { - Atomics: { - load: AtomicsLoad, - wait: AtomicsWait, - waitAsync: AtomicsWaitAsync, - }, SharedArrayBuffer, } = globalThis; diff --git a/lib/internal/modules/esm/worker.js b/lib/internal/modules/esm/worker.js index 433cb9446a2897..311d77fb099384 100644 --- a/lib/internal/modules/esm/worker.js +++ b/lib/internal/modules/esm/worker.js @@ -1,18 +1,14 @@ 'use strict'; const { + AtomicsAdd, + AtomicsNotify, DataViewPrototypeGetBuffer, Int32Array, PromisePrototypeThen, ReflectApply, SafeSet, TypedArrayPrototypeGetBuffer, - globalThis: { - Atomics: { - add: AtomicsAdd, - notify: AtomicsNotify, - }, - }, } = primordials; const assert = require('internal/assert'); const { clearImmediate, setImmediate } = require('timers'); diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 64438ddd5219f1..c4f2b12aff2cba 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -172,6 +172,7 @@ function copyPrototype(src, dest, prefix) { // Create copies of the namespace objects [ + 'Atomics', 'JSON', 'Math', 'Proxy', diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 401bc43550ea7f..aa65b229c43b07 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -4,6 +4,7 @@ const { ArrayPrototypeForEach, ArrayPrototypeMap, ArrayPrototypePush, + AtomicsAdd, Float64Array, FunctionPrototypeBind, JSONStringify, @@ -21,7 +22,7 @@ const { SymbolFor, TypedArrayPrototypeFill, Uint32Array, - globalThis: { Atomics, SharedArrayBuffer }, + globalThis: { SharedArrayBuffer }, } = primordials; const EventEmitter = require('events'); @@ -101,12 +102,11 @@ let cwdCounter; const environmentData = new SafeMap(); // SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer. -// Atomics can be disabled with --no-harmony-atomics. -if (isMainThread && SharedArrayBuffer !== undefined && Atomics !== undefined) { +if (isMainThread && SharedArrayBuffer !== undefined) { cwdCounter = new Uint32Array(new SharedArrayBuffer(4)); const originalChdir = process.chdir; process.chdir = function(path) { - Atomics.add(cwdCounter, 0, 1); + AtomicsAdd(cwdCounter, 0, 1); originalChdir(path); }; } diff --git a/test/parallel/test-worker-no-atomics.js b/test/parallel/test-worker-no-atomics.js deleted file mode 100644 index 3dd5a47c127d7c..00000000000000 --- a/test/parallel/test-worker-no-atomics.js +++ /dev/null @@ -1,21 +0,0 @@ -// Flags: --no-harmony-atomics - -'use strict'; - -const common = require('../common'); -const assert = require('assert'); -const { Worker } = require('worker_threads'); - -// Regression test for https://github.com/nodejs/node/issues/39717. - -// Do not use isMainThread so that this test itself can be run inside a Worker. -if (!process.env.HAS_STARTED_WORKER) { - process.env.HAS_STARTED_WORKER = 1; - const w = new Worker(__filename); - - w.on('exit', common.mustCall((status) => { - assert.strictEqual(status, 2); - })); -} else { - process.exit(2); -} diff --git a/test/parallel/test-worker-process-cwd.js b/test/parallel/test-worker-process-cwd.js index e24515cce777a3..1a581a37025078 100644 --- a/test/parallel/test-worker-process-cwd.js +++ b/test/parallel/test-worker-process-cwd.js @@ -16,7 +16,7 @@ if (!process.env.HAS_STARTED_WORKER) { // Normalize the current working dir to also work with the root folder. process.chdir(__dirname); - assert(!process.cwd.toString().includes('Atomics.load')); + assert.doesNotMatch(process.cwd.toString(), /AtomicsLoad/); // 1. Start the first worker. const w = new Worker(__filename); @@ -32,7 +32,7 @@ if (!process.env.HAS_STARTED_WORKER) { // 2. Save the current cwd and verify that `process.cwd` includes the // Atomics.load call and spawn a new worker. const cwd = process.cwd(); - assert(process.cwd.toString().includes('Atomics.load')); + assert.match(process.cwd.toString(), /AtomicsLoad/); const w = new Worker(__filename); w.once('message', common.mustCall((message) => { @@ -56,7 +56,7 @@ if (!process.env.HAS_STARTED_WORKER) { const cwd = process.cwd(); // Send the current cwd to the parent. parentPort.postMessage(cwd); - assert(process.cwd.toString().includes('Atomics.load')); + assert.match(process.cwd.toString(), /AtomicsLoad/); parentPort.once('message', common.mustCall((message) => { // 7. Verify that the current cwd is identical to the received one but