]> git.kaiwu.me - quickjs.git/commitdiff
fixed waitAsync() polling in quickjs-libc (#603)
authorFabrice Bellard <fabrice@bellard.org>
Mon, 9 Nov 2026 14:08:42 +0000 (15:08 +0100)
committerFabrice Bellard <fabrice@bellard.org>
Mon, 9 Nov 2026 14:08:42 +0000 (15:08 +0100)
quickjs-libc.c
tests/test_worker.js
tests/test_worker_module.js

index 3ac128e2366ccdb6a81b528cab5207bc280fa48f..b9e855b9866d290c81bc4fe0cb8a5c0f50762a92 100644 (file)
@@ -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);
         }
     }
index 4b52bf82cd5d6f83cd93bf6b740c2c1750fa7084..64d481357a5c2560b1d18b6707262a213cafae3c 100644 (file)
@@ -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":
index ddf8e40074b1f599a2369d28a4bfc6bc9479740f..df9358cec58ceaafc700ddf471a9bb703dc30977 100644 (file)
@@ -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;
     }
 }