From 3cddd4c4698126819ecf09e45d429ec9a4a59125 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Mon, 9 Nov 2026 15:08:42 +0100 Subject: [PATCH] fixed waitAsync() polling in quickjs-libc (#603) --- quickjs-libc.c | 5 +++-- tests/test_worker.js | 16 +++++++++++++--- tests/test_worker_module.js | 4 ++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index 3ac128e..b9e855b 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -2619,7 +2619,7 @@ static int js_os_poll(JSContext *ctx) { JSRuntime *rt = JS_GetRuntime(ctx); JSThreadState *ts = JS_GetRuntimeOpaque(rt); - int min_delay, nfds; + int min_delay, nfds, atomics_async_waker_fd_index; int64_t cur_time, delay; JSOSRWHandler *rh; struct list_head *el; @@ -2698,6 +2698,7 @@ static int js_os_poll(JSContext *ctx) } } + atomics_async_waker_fd_index = nfds; if (js_poll_add_poll_fd(ts, &nfds, ts->atomics_async_waker.read_fd, POLLIN)) return -1; @@ -2728,7 +2729,7 @@ static int js_os_poll(JSContext *ctx) } } } - if (ts->poll_fds[ts->atomics_async_waker.read_fd].revents != 0) { + if (ts->poll_fds[atomics_async_waker_fd_index].revents != 0) { js_waker_clear(&ts->atomics_async_waker); } } diff --git a/tests/test_worker.js b/tests/test_worker.js index 4b52bf8..64d4813 100644 --- a/tests/test_worker.js +++ b/tests/test_worker.js @@ -44,10 +44,20 @@ function test_worker() break; case "sab_done": { - let buf = ev.buf; /* check that the SharedArrayBuffer was modified */ - assert(buf[2], 10); - worker.postMessage({ type: "abort" }); + assert(ev.buf[2], 10); + /* start the waitAsync() test */ + let sab = new SharedArrayBuffer(4); + let buf = new Int32Array(sab); + worker.postMessage({ type: "wait_async", buf: buf }); + } + break; + case "notify": + { + let buf = ev.buf; + Atomics.store(buf, 0, 1); + /* notify the waitAsync() */ + Atomics.notify(buf, 0, 1); } break; case "done": diff --git a/tests/test_worker_module.js b/tests/test_worker_module.js index ddf8e40..df9358c 100644 --- a/tests/test_worker_module.js +++ b/tests/test_worker_module.js @@ -17,6 +17,10 @@ function handle_msg(e) { ev.buf[2] = 10; parent.postMessage({ type: "sab_done", buf: ev.buf }); break; + case "wait_async": + Atomics.waitAsync(ev.buf, 0, 0, Infinity).value.then( () => { parent.postMessage({ type: "done" }); } ); + parent.postMessage({ type: "notify", buf: ev.buf }); + break; } } -- 2.47.3