0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 13:09:21 +01:00

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');
}
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],

View File

@ -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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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.
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