Invert event loop

This commit is contained in:
2026-10-08 08:53:11 +00:00
parent c4d0bab3d9
commit 2f21347088
12 changed files with 142 additions and 33 deletions
+20 -5
View File
@@ -2,9 +2,10 @@
// io_uring (see uring.h)
//
// A `__native_io_queue_<op>` queues an op and gives its handle. It makes no
// syscall. `__native_io_wait` puts queued ops into the SQ, submits them,
// waits until one op is done, and gives its handle. The getters then read
// that op's result, until the next wait
// syscall, and no native waits. The host's loop (`main` below) puts queued
// ops into the SQ, submits them, waits until one op is done, and gives its
// handle to `jsrt_complete`. The getters read that op's result while
// `jsrt_complete` runs
//
// At most `cq_entries` ops are in flight, so the CQ cannot overflow. The rest
// stay queued until a later wait. So a queue never fails, and the program
@@ -392,8 +393,8 @@ int32_t __native_io_queue_cancel(int32_t target) {
}
// The handle of an op that is done, or -1 if no op is queued or in flight.
// The op that the last wait gave is freed now
int32_t __native_io_wait(void) {
// The op that the last call gave is freed now
static int32_t jsrt_io_next(void) {
if (jsrt_io.given >= 0) {
jsrt_io_release(jsrt_io.given);
jsrt_io.given = -1;
@@ -452,4 +453,18 @@ double __native_io_result_number(int32_t h) {
return op->res;
}
#ifndef JSRT_NO_HOST
// The Linux flavor's host: run the program, then give it each op as it
// completes, until none is left
int main(void) {
jsrt_start();
for (int32_t h; (h = jsrt_io_next()) >= 0;) {
jsrt_complete(h);
}
return 0;
}
#endif
#endif // JSRT_IMPLEMENTATION