Skip to content

Commit fd0e233

Browse files
authored
Separate out Atomics.waitAsync polyfill. NFC (#20474)
Split out from #20404. Rather than adding artificial dependencies on `emscripten_atomic_wait_async` we can use the same technique used by `$polyfillSetImmediate` in `library_eventloop.js`.
1 parent a8e9dd5 commit fd0e233

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

emcc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2504,7 +2504,7 @@ def phase_linker_setup(options, state, newargs):
25042504
# set location of Wasm Worker bootstrap JS file
25052505
if settings.WASM_WORKERS == 1:
25062506
settings.WASM_WORKER_FILE = unsuffixed(os.path.basename(target)) + '.ww.js'
2507-
settings.JS_LIBRARIES.append((0, shared.path_from_root('src', 'library_wasm_worker.js')))
2507+
settings.JS_LIBRARIES.append((0, 'library_wasm_worker.js'))
25082508

25092509
# Set min browser versions based on certain settings such as WASM_BIGINT,
25102510
# PTHREADS, AUDIO_WORKLET

src/library_wasm_worker.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ if (ENVIRONMENT_IS_WASM_WORKER) {
257257
// https://github.com/tc39/proposal-atomics-wait-async/blob/master/PROPOSAL.md
258258
// This polyfill performs polling with setTimeout() to observe a change in the
259259
// target memory location.
260-
emscripten_atomic_wait_async__postset: `if (!Atomics.waitAsync || (typeof navigator !== 'undefined' && navigator.userAgent && jstoi_q((navigator.userAgent.match(/Chrom(e|ium)\\/([0-9]+)\\./)||[])[2]) < 91)) {
260+
$polyfillWaitAsync__deps: ['$jstoi_q'],
261+
$polyfillWaitAsync__postset: `if (!Atomics.waitAsync || (typeof navigator !== 'undefined' && navigator.userAgent && jstoi_q((navigator.userAgent.match(/Chrom(e|ium)\\/([0-9]+)\\./)||[])[2]) < 91)) {
261262
let __Atomics_waitAsyncAddresses = [/*[i32a, index, value, maxWaitMilliseconds, promiseResolve]*/];
262263
function __Atomics_pollWaitAsyncAddresses() {
263264
let now = performance.now();
@@ -292,20 +293,19 @@ Atomics.waitAsync = (i32a, index, value, maxWaitMilliseconds) => {
292293
return { async: true, value: promise };
293294
};
294295
}`,
295-
296-
// These dependencies are artificial, issued so that we still get the
297-
// waitAsync polyfill emitted if code only calls
298-
// emscripten_lock/semaphore_async_acquire() but not
299-
// emscripten_atomic_wait_async() directly.
300-
emscripten_lock_async_acquire__deps: ['emscripten_atomic_wait_async'],
301-
emscripten_semaphore_async_acquire__deps: ['emscripten_atomic_wait_async'],
302-
303296
#endif
304297

305-
$liveAtomicWaitAsyncs: '{}',
306-
$liveAtomicWaitAsyncCounter: '0',
298+
$polyfillWaitAsync__internal: true,
299+
$polyfillWaitAsync: () => {
300+
// nop, used for its postset to ensure `Atomics.waitAsync()` polyfill is
301+
// included exactly once and only included when needed.
302+
// Any function using Atomics.waitAsync should depend on this.
303+
},
304+
305+
$liveAtomicWaitAsyncs: {},
306+
$liveAtomicWaitAsyncCounter: 0,
307307

308-
emscripten_atomic_wait_async__deps: ['$atomicWaitStates', '$liveAtomicWaitAsyncs', '$liveAtomicWaitAsyncCounter', '$jstoi_q'],
308+
emscripten_atomic_wait_async__deps: ['$atomicWaitStates', '$liveAtomicWaitAsyncs', '$liveAtomicWaitAsyncCounter', '$polyfillWaitAsync'],
309309
emscripten_atomic_wait_async: (addr, val, asyncWaitFinished, userData, maxWaitMilliseconds) => {
310310
let wait = Atomics.waitAsync(HEAP32, {{{ getHeapOffset('addr', 'i32') }}}, val, maxWaitMilliseconds);
311311
if (!wait.async) return atomicWaitStates.indexOf(wait.value);
@@ -385,6 +385,7 @@ Atomics.waitAsync = (i32a, index, value, maxWaitMilliseconds) => {
385385
return Atomics.isLockFree(width);
386386
},
387387

388+
emscripten_lock_async_acquire__deps: ['$polyfillWaitAsync'],
388389
emscripten_lock_async_acquire: (lock, asyncWaitFinished, userData, maxWaitMilliseconds) => {
389390
let dispatch = (val, ret) => {
390391
setTimeout(() => {
@@ -406,6 +407,7 @@ Atomics.waitAsync = (i32a, index, value, maxWaitMilliseconds) => {
406407
tryAcquireLock();
407408
},
408409

410+
emscripten_semaphore_async_acquire__deps: ['$polyfillWaitAsync'],
409411
emscripten_semaphore_async_acquire: (sem, num, asyncWaitFinished, userData, maxWaitMilliseconds) => {
410412
let dispatch = (idx, ret) => {
411413
setTimeout(() => {

0 commit comments

Comments
 (0)