Invert event loop
This commit is contained in:
+20
-5
@@ -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
|
||||
|
||||
+15
-1
@@ -10,7 +10,15 @@
|
||||
// str.h strings and the string natives
|
||||
// number.h the number natives
|
||||
// any.h any_t and JS's operators on it
|
||||
// io.h IO natives, async on io_uring (uring.h)
|
||||
// io.h IO natives, async on io_uring (uring.h), and the host's loop
|
||||
//
|
||||
// The host owns the event loop. The program exports two fns, which the host
|
||||
// calls, and a flavor of the runtime is the host. This one, for Linux, is the
|
||||
// `main` in io.h: define JSRT_NO_HOST to bring another
|
||||
//
|
||||
// jsrt_start() runs the program's top level, then its microtasks
|
||||
// jsrt_complete(h) op `h` is done: runs its callback, then the microtasks.
|
||||
// The op's result can be read only until this returns
|
||||
#pragma once
|
||||
|
||||
// io.h needs Linux's syscall() and mmap(), which -std=c11 hides. This must
|
||||
@@ -20,6 +28,12 @@
|
||||
#endif
|
||||
|
||||
#include "core.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void jsrt_start(void);
|
||||
void jsrt_complete(int32_t h);
|
||||
|
||||
#include "str.h"
|
||||
#include "number.h"
|
||||
#include "any.h"
|
||||
|
||||
Reference in New Issue
Block a user