Skip to content

Commit

Permalink
lib: update usage of always on Atomics API
Browse files Browse the repository at this point in the history
PR-URL: #49639
Reviewed-By: Jiawen Geng <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
targos committed Oct 10, 2023
1 parent 8568de3 commit fd21429
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 51 deletions.
4 changes: 0 additions & 4 deletions benchmark/worker/atomics-wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
5 changes: 1 addition & 4 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -193,6 +189,7 @@ rules:
- name: AggregateError
- name: Array
- name: ArrayBuffer
- name: Atomics
- name: BigInt
- name: BigInt64Array
- name: BigUint64Array
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/freeze_intrinsics.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const {
ArrayPrototype,
ArrayPrototypeForEach,
ArrayPrototypePush,
Atomics,
BigInt,
BigInt64Array,
BigInt64ArrayPrototype,
Expand Down Expand Up @@ -128,7 +129,6 @@ const {
} = primordials;

const {
Atomics,
Intl,
SharedArrayBuffer,
WebAssembly,
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const {
ArrayPrototypeForEach,
ArrayPrototypePushApply,
ArrayPrototypeSplice,
AtomicsLoad,
ObjectDefineProperty,
PromisePrototypeThen,
RegExpPrototypeExec,
SafeWeakMap,
globalThis: {
Atomics,
SharedArrayBuffer,
},
} = primordials;
Expand Down Expand Up @@ -112,15 +112,15 @@ 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 = '';
let lastCounter = -1;
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;
Expand Down
8 changes: 3 additions & 5 deletions lib/internal/modules/esm/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const {
ArrayPrototypePush,
ArrayPrototypePushApply,
AtomicsLoad,
AtomicsWait,
AtomicsWaitAsync,
Int32Array,
ObjectAssign,
ObjectDefineProperty,
Expand All @@ -15,11 +18,6 @@ const {
} = primordials;

const {
Atomics: {
load: AtomicsLoad,
wait: AtomicsWait,
waitAsync: AtomicsWaitAsync,
},
SharedArrayBuffer,
} = globalThis;

Expand Down
8 changes: 2 additions & 6 deletions lib/internal/modules/esm/worker.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
1 change: 1 addition & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ function copyPrototype(src, dest, prefix) {

// Create copies of the namespace objects
[
'Atomics',
'JSON',
'Math',
'Proxy',
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
ArrayPrototypeForEach,
ArrayPrototypeMap,
ArrayPrototypePush,
AtomicsAdd,
Float64Array,
FunctionPrototypeBind,
JSONStringify,
Expand All @@ -21,7 +22,7 @@ const {
SymbolFor,
TypedArrayPrototypeFill,
Uint32Array,
globalThis: { Atomics, SharedArrayBuffer },
globalThis: { SharedArrayBuffer },
} = primordials;

const EventEmitter = require('events');
Expand Down Expand Up @@ -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);
};
}
Expand Down
21 changes: 0 additions & 21 deletions test/parallel/test-worker-no-atomics.js

This file was deleted.

6 changes: 3 additions & 3 deletions test/parallel/test-worker-process-cwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) => {
Expand All @@ -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
Expand Down

0 comments on commit fd21429

Please sign in to comment.