Skip to content

Commit fd21429

Browse files
committed
lib: update usage of always on Atomics API
PR-URL: #49639 Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 8568de3 commit fd21429

File tree

10 files changed

+18
-51
lines changed

10 files changed

+18
-51
lines changed

benchmark/worker/atomics-wait.js

-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ if (typeof SharedArrayBuffer === 'undefined') {
44
throw new Error('SharedArrayBuffers must be enabled to run this benchmark');
55
}
66

7-
if (typeof Atomics === 'undefined') {
8-
throw new Error('Atomics must be enabled to run this benchmark');
9-
}
10-
117
const common = require('../common.js');
128
const bench = common.createBenchmark(main, {
139
n: [1e7],

lib/.eslintrc.yaml

+1-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ rules:
3333
message: Use `const { AbortController } = require('internal/abort_controller');` instead of the global.
3434
- name: AbortSignal
3535
message: Use `const { AbortSignal } = require('internal/abort_controller');` instead of the global.
36-
# Atomics is not available in primordials because it can be
37-
# disabled with --no-harmony-atomics CLI flag.
38-
- name: Atomics
39-
message: Use `const { Atomics } = globalThis;` instead of the global.
4036
- name: Blob
4137
message: Use `const { Blob } = require('buffer');` instead of the global.
4238
- name: BroadcastChannel
@@ -193,6 +189,7 @@ rules:
193189
- name: AggregateError
194190
- name: Array
195191
- name: ArrayBuffer
192+
- name: Atomics
196193
- name: BigInt
197194
- name: BigInt64Array
198195
- name: BigUint64Array

lib/internal/freeze_intrinsics.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const {
3131
ArrayPrototype,
3232
ArrayPrototypeForEach,
3333
ArrayPrototypePush,
34+
Atomics,
3435
BigInt,
3536
BigInt64Array,
3637
BigInt64ArrayPrototype,
@@ -128,7 +129,6 @@ const {
128129
} = primordials;
129130

130131
const {
131-
Atomics,
132132
Intl,
133133
SharedArrayBuffer,
134134
WebAssembly,

lib/internal/main/worker_thread.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const {
77
ArrayPrototypeForEach,
88
ArrayPrototypePushApply,
99
ArrayPrototypeSplice,
10+
AtomicsLoad,
1011
ObjectDefineProperty,
1112
PromisePrototypeThen,
1213
RegExpPrototypeExec,
1314
SafeWeakMap,
1415
globalThis: {
15-
Atomics,
1616
SharedArrayBuffer,
1717
},
1818
} = primordials;
@@ -112,15 +112,15 @@ port.on('message', (message) => {
112112

113113
require('internal/worker').assignEnvironmentData(environmentData);
114114

115-
if (SharedArrayBuffer !== undefined && Atomics !== undefined) {
115+
if (SharedArrayBuffer !== undefined) {
116116
// The counter is only passed to the workers created by the main thread,
117117
// not to workers created by other workers.
118118
let cachedCwd = '';
119119
let lastCounter = -1;
120120
const originalCwd = process.cwd;
121121

122122
process.cwd = function() {
123-
const currentCounter = Atomics.load(cwdCounter, 0);
123+
const currentCounter = AtomicsLoad(cwdCounter, 0);
124124
if (currentCounter === lastCounter)
125125
return cachedCwd;
126126
lastCounter = currentCounter;

lib/internal/modules/esm/hooks.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
const {
44
ArrayPrototypePush,
55
ArrayPrototypePushApply,
6+
AtomicsLoad,
7+
AtomicsWait,
8+
AtomicsWaitAsync,
69
Int32Array,
710
ObjectAssign,
811
ObjectDefineProperty,
@@ -15,11 +18,6 @@ const {
1518
} = primordials;
1619

1720
const {
18-
Atomics: {
19-
load: AtomicsLoad,
20-
wait: AtomicsWait,
21-
waitAsync: AtomicsWaitAsync,
22-
},
2321
SharedArrayBuffer,
2422
} = globalThis;
2523

lib/internal/modules/esm/worker.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
'use strict';
22

33
const {
4+
AtomicsAdd,
5+
AtomicsNotify,
46
DataViewPrototypeGetBuffer,
57
Int32Array,
68
PromisePrototypeThen,
79
ReflectApply,
810
SafeSet,
911
TypedArrayPrototypeGetBuffer,
10-
globalThis: {
11-
Atomics: {
12-
add: AtomicsAdd,
13-
notify: AtomicsNotify,
14-
},
15-
},
1612
} = primordials;
1713
const assert = require('internal/assert');
1814
const { clearImmediate, setImmediate } = require('timers');

lib/internal/per_context/primordials.js

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ function copyPrototype(src, dest, prefix) {
172172

173173
// Create copies of the namespace objects
174174
[
175+
'Atomics',
175176
'JSON',
176177
'Math',
177178
'Proxy',

lib/internal/worker.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
ArrayPrototypeForEach,
55
ArrayPrototypeMap,
66
ArrayPrototypePush,
7+
AtomicsAdd,
78
Float64Array,
89
FunctionPrototypeBind,
910
JSONStringify,
@@ -21,7 +22,7 @@ const {
2122
SymbolFor,
2223
TypedArrayPrototypeFill,
2324
Uint32Array,
24-
globalThis: { Atomics, SharedArrayBuffer },
25+
globalThis: { SharedArrayBuffer },
2526
} = primordials;
2627

2728
const EventEmitter = require('events');
@@ -101,12 +102,11 @@ let cwdCounter;
101102
const environmentData = new SafeMap();
102103

103104
// SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
104-
// Atomics can be disabled with --no-harmony-atomics.
105-
if (isMainThread && SharedArrayBuffer !== undefined && Atomics !== undefined) {
105+
if (isMainThread && SharedArrayBuffer !== undefined) {
106106
cwdCounter = new Uint32Array(new SharedArrayBuffer(4));
107107
const originalChdir = process.chdir;
108108
process.chdir = function(path) {
109-
Atomics.add(cwdCounter, 0, 1);
109+
AtomicsAdd(cwdCounter, 0, 1);
110110
originalChdir(path);
111111
};
112112
}

test/parallel/test-worker-no-atomics.js

-21
This file was deleted.

test/parallel/test-worker-process-cwd.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if (!process.env.HAS_STARTED_WORKER) {
1616
// Normalize the current working dir to also work with the root folder.
1717
process.chdir(__dirname);
1818

19-
assert(!process.cwd.toString().includes('Atomics.load'));
19+
assert.doesNotMatch(process.cwd.toString(), /AtomicsLoad/);
2020

2121
// 1. Start the first worker.
2222
const w = new Worker(__filename);
@@ -32,7 +32,7 @@ if (!process.env.HAS_STARTED_WORKER) {
3232
// 2. Save the current cwd and verify that `process.cwd` includes the
3333
// Atomics.load call and spawn a new worker.
3434
const cwd = process.cwd();
35-
assert(process.cwd.toString().includes('Atomics.load'));
35+
assert.match(process.cwd.toString(), /AtomicsLoad/);
3636

3737
const w = new Worker(__filename);
3838
w.once('message', common.mustCall((message) => {
@@ -56,7 +56,7 @@ if (!process.env.HAS_STARTED_WORKER) {
5656
const cwd = process.cwd();
5757
// Send the current cwd to the parent.
5858
parentPort.postMessage(cwd);
59-
assert(process.cwd.toString().includes('Atomics.load'));
59+
assert.match(process.cwd.toString(), /AtomicsLoad/);
6060

6161
parentPort.once('message', common.mustCall((message) => {
6262
// 7. Verify that the current cwd is identical to the received one but

0 commit comments

Comments
 (0)