mirror of https://github.com/nodejs/node.git
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');
|
||||
}
|
||||
|
||||
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],
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -31,6 +31,7 @@ const {
|
|||
ArrayPrototype,
|
||||
ArrayPrototypeForEach,
|
||||
ArrayPrototypePush,
|
||||
Atomics,
|
||||
BigInt,
|
||||
BigInt64Array,
|
||||
BigInt64ArrayPrototype,
|
||||
|
@ -128,7 +129,6 @@ const {
|
|||
} = primordials;
|
||||
|
||||
const {
|
||||
Atomics,
|
||||
Intl,
|
||||
SharedArrayBuffer,
|
||||
WebAssembly,
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -172,6 +172,7 @@ function copyPrototype(src, dest, prefix) {
|
|||
|
||||
// Create copies of the namespace objects
|
||||
[
|
||||
'Atomics',
|
||||
'JSON',
|
||||
'Math',
|
||||
'Proxy',
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue