lib: update usage of always on Atomics API

PR-URL: https://github.com/nodejs/node/pull/49639
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Michaël Zasso 2023-04-16 15:28:41 +02:00
parent 8568de3da6
commit fd21429ef5
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600
10 changed files with 18 additions and 51 deletions

View File

@ -4,10 +4,6 @@ if (typeof SharedArrayBuffer === 'undefined') {
throw new Error('SharedArrayBuffers must be enabled to run this benchmark'); 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 common = require('../common.js');
const bench = common.createBenchmark(main, { const bench = common.createBenchmark(main, {
n: [1e7], n: [1e7],

View File

@ -33,10 +33,6 @@ rules:
message: Use `const { AbortController } = require('internal/abort_controller');` instead of the global. message: Use `const { AbortController } = require('internal/abort_controller');` instead of the global.
- name: AbortSignal - name: AbortSignal
message: Use `const { AbortSignal } = require('internal/abort_controller');` instead of the global. 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 - name: Blob
message: Use `const { Blob } = require('buffer');` instead of the global. message: Use `const { Blob } = require('buffer');` instead of the global.
- name: BroadcastChannel - name: BroadcastChannel
@ -193,6 +189,7 @@ rules:
- name: AggregateError - name: AggregateError
- name: Array - name: Array
- name: ArrayBuffer - name: ArrayBuffer
- name: Atomics
- name: BigInt - name: BigInt
- name: BigInt64Array - name: BigInt64Array
- name: BigUint64Array - name: BigUint64Array

View File

@ -31,6 +31,7 @@ const {
ArrayPrototype, ArrayPrototype,
ArrayPrototypeForEach, ArrayPrototypeForEach,
ArrayPrototypePush, ArrayPrototypePush,
Atomics,
BigInt, BigInt,
BigInt64Array, BigInt64Array,
BigInt64ArrayPrototype, BigInt64ArrayPrototype,
@ -128,7 +129,6 @@ const {
} = primordials; } = primordials;
const { const {
Atomics,
Intl, Intl,
SharedArrayBuffer, SharedArrayBuffer,
WebAssembly, WebAssembly,

View File

@ -7,12 +7,12 @@ const {
ArrayPrototypeForEach, ArrayPrototypeForEach,
ArrayPrototypePushApply, ArrayPrototypePushApply,
ArrayPrototypeSplice, ArrayPrototypeSplice,
AtomicsLoad,
ObjectDefineProperty, ObjectDefineProperty,
PromisePrototypeThen, PromisePrototypeThen,
RegExpPrototypeExec, RegExpPrototypeExec,
SafeWeakMap, SafeWeakMap,
globalThis: { globalThis: {
Atomics,
SharedArrayBuffer, SharedArrayBuffer,
}, },
} = primordials; } = primordials;
@ -112,7 +112,7 @@ port.on('message', (message) => {
require('internal/worker').assignEnvironmentData(environmentData); 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, // The counter is only passed to the workers created by the main thread,
// not to workers created by other workers. // not to workers created by other workers.
let cachedCwd = ''; let cachedCwd = '';
@ -120,7 +120,7 @@ port.on('message', (message) => {
const originalCwd = process.cwd; const originalCwd = process.cwd;
process.cwd = function() { process.cwd = function() {
const currentCounter = Atomics.load(cwdCounter, 0); const currentCounter = AtomicsLoad(cwdCounter, 0);
if (currentCounter === lastCounter) if (currentCounter === lastCounter)
return cachedCwd; return cachedCwd;
lastCounter = currentCounter; lastCounter = currentCounter;

View File

@ -3,6 +3,9 @@
const { const {
ArrayPrototypePush, ArrayPrototypePush,
ArrayPrototypePushApply, ArrayPrototypePushApply,
AtomicsLoad,
AtomicsWait,
AtomicsWaitAsync,
Int32Array, Int32Array,
ObjectAssign, ObjectAssign,
ObjectDefineProperty, ObjectDefineProperty,
@ -15,11 +18,6 @@ const {
} = primordials; } = primordials;
const { const {
Atomics: {
load: AtomicsLoad,
wait: AtomicsWait,
waitAsync: AtomicsWaitAsync,
},
SharedArrayBuffer, SharedArrayBuffer,
} = globalThis; } = globalThis;

View File

@ -1,18 +1,14 @@
'use strict'; 'use strict';
const { const {
AtomicsAdd,
AtomicsNotify,
DataViewPrototypeGetBuffer, DataViewPrototypeGetBuffer,
Int32Array, Int32Array,
PromisePrototypeThen, PromisePrototypeThen,
ReflectApply, ReflectApply,
SafeSet, SafeSet,
TypedArrayPrototypeGetBuffer, TypedArrayPrototypeGetBuffer,
globalThis: {
Atomics: {
add: AtomicsAdd,
notify: AtomicsNotify,
},
},
} = primordials; } = primordials;
const assert = require('internal/assert'); const assert = require('internal/assert');
const { clearImmediate, setImmediate } = require('timers'); const { clearImmediate, setImmediate } = require('timers');

View File

@ -172,6 +172,7 @@ function copyPrototype(src, dest, prefix) {
// Create copies of the namespace objects // Create copies of the namespace objects
[ [
'Atomics',
'JSON', 'JSON',
'Math', 'Math',
'Proxy', 'Proxy',

View File

@ -4,6 +4,7 @@ const {
ArrayPrototypeForEach, ArrayPrototypeForEach,
ArrayPrototypeMap, ArrayPrototypeMap,
ArrayPrototypePush, ArrayPrototypePush,
AtomicsAdd,
Float64Array, Float64Array,
FunctionPrototypeBind, FunctionPrototypeBind,
JSONStringify, JSONStringify,
@ -21,7 +22,7 @@ const {
SymbolFor, SymbolFor,
TypedArrayPrototypeFill, TypedArrayPrototypeFill,
Uint32Array, Uint32Array,
globalThis: { Atomics, SharedArrayBuffer }, globalThis: { SharedArrayBuffer },
} = primordials; } = primordials;
const EventEmitter = require('events'); const EventEmitter = require('events');
@ -101,12 +102,11 @@ let cwdCounter;
const environmentData = new SafeMap(); const environmentData = new SafeMap();
// SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer. // SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
// Atomics can be disabled with --no-harmony-atomics. if (isMainThread && SharedArrayBuffer !== undefined) {
if (isMainThread && SharedArrayBuffer !== undefined && Atomics !== undefined) {
cwdCounter = new Uint32Array(new SharedArrayBuffer(4)); cwdCounter = new Uint32Array(new SharedArrayBuffer(4));
const originalChdir = process.chdir; const originalChdir = process.chdir;
process.chdir = function(path) { process.chdir = function(path) {
Atomics.add(cwdCounter, 0, 1); AtomicsAdd(cwdCounter, 0, 1);
originalChdir(path); originalChdir(path);
}; };
} }

View File

@ -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);
}

View File

@ -16,7 +16,7 @@ if (!process.env.HAS_STARTED_WORKER) {
// Normalize the current working dir to also work with the root folder. // Normalize the current working dir to also work with the root folder.
process.chdir(__dirname); process.chdir(__dirname);
assert(!process.cwd.toString().includes('Atomics.load')); assert.doesNotMatch(process.cwd.toString(), /AtomicsLoad/);
// 1. Start the first worker. // 1. Start the first worker.
const w = new Worker(__filename); 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 // 2. Save the current cwd and verify that `process.cwd` includes the
// Atomics.load call and spawn a new worker. // Atomics.load call and spawn a new worker.
const cwd = process.cwd(); const cwd = process.cwd();
assert(process.cwd.toString().includes('Atomics.load')); assert.match(process.cwd.toString(), /AtomicsLoad/);
const w = new Worker(__filename); const w = new Worker(__filename);
w.once('message', common.mustCall((message) => { w.once('message', common.mustCall((message) => {
@ -56,7 +56,7 @@ if (!process.env.HAS_STARTED_WORKER) {
const cwd = process.cwd(); const cwd = process.cwd();
// Send the current cwd to the parent. // Send the current cwd to the parent.
parentPort.postMessage(cwd); parentPort.postMessage(cwd);
assert(process.cwd.toString().includes('Atomics.load')); assert.match(process.cwd.toString(), /AtomicsLoad/);
parentPort.once('message', common.mustCall((message) => { parentPort.once('message', common.mustCall((message) => {
// 7. Verify that the current cwd is identical to the received one but // 7. Verify that the current cwd is identical to the received one but