mirror of
https://github.com/nodejs/node.git
synced 2025-04-28 21:46:48 +00: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:
parent
8568de3da6
commit
fd21429ef5
@ -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],
|
||||||
|
@ -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
|
||||||
|
@ -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,
|
||||||
|
@ -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;
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
@ -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');
|
||||||
|
@ -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',
|
||||||
|
@ -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);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user