Replace the bare-paren tuple grammar with explicit, position-unambiguous
forms, mirroring how structs work:
type `(A, B)` -> `Tuple(A, B)` (named keeps `:`)
value `(a, b)` -> `.(a, b)` (named uses `=`)
typed (new) -> `Tuple(A, B).(a, b)` (like `Point.{...}`)
failable `-> (T, !)` -> `-> T !`
`-> (T1, T2, !)`-> `-> Tuple(T1, T2) !` (channel outside Tuple)
Bare `(...)` is now grouping only, everywhere; a comma in bare parens is a
hard error with a migration hint. Grouping, function types `(A, B) -> R`,
param lists, lambdas, and match bindings are unaffected.
`Tuple(...)` is strictly a TYPE in every position (including `size_of` /
`type_info` args); a tuple VALUE comes only from `.(...)` (anonymous) or
`Tuple(...).(...)` (explicitly typed). A bare `Tuple(1, 2)` is a tuple
type with non-type elements -> rejected.
The ~110 tuple-bearing corpus files were migrated with a one-shot
AST-aware migrator (the `sx migrate` tool from the prior commit, removed
here). New examples: 0130 (new syntax), 0131 (typed construction), 1060
(named-tuple failable return). 1116 golden updated for the new hint text.
140 lines
5.4 KiB
Plaintext
140 lines
5.4 KiB
Plaintext
// std.event — the OS-neutral readiness Loop (PLAN-HTTPZ S5).
|
|
//
|
|
// One Loop multiplexes any number of fds without ever blocking on a
|
|
// single one: register interest with an opaque `udata` word, then
|
|
// `wait` yields normalized Events for whatever became ready. Idle
|
|
// registrations cost nothing — the substrate an httpz-shaped server
|
|
// worker stands on.
|
|
//
|
|
// Backend: kqueue (std/net/kqueue) on darwin. The epoll twin
|
|
// (std/net/epoll, PLAN-HTTPZ S4) slots in behind this same surface
|
|
// when the linux target lands; callers never see the backend.
|
|
//
|
|
// Interest is per direction: read and write are registered and removed
|
|
// independently (mirroring kqueue filters; the epoll backend will
|
|
// compose its event mask internally). The typical server pattern:
|
|
// read interest for a connection's whole life, write interest only
|
|
// while a partial response is pending.
|
|
//
|
|
// Deadlines: the loop deliberately has no timer registrations —
|
|
// httpz-style timeout bookkeeping (request/keepalive eviction) is
|
|
// deadline math the caller does with `deadline_in`/`expired` between
|
|
// waits, passing the nearest deadline as `wait`'s timeout.
|
|
|
|
#import "modules/std.sx";
|
|
kqb :: #import "modules/std/net/kqueue.sx";
|
|
timp :: #import "modules/std/time.sx";
|
|
|
|
EventErr :: error {
|
|
Init, // the kernel queue could not be created
|
|
Register, // an interest change was refused
|
|
Wait, // the wait itself failed (not a timeout)
|
|
}
|
|
|
|
// A normalized readiness report for one registered fd.
|
|
// readable/writable — which direction is ready;
|
|
// eof — the peer finished writing (drain pending bytes, then close);
|
|
// err — the registration itself failed asynchronously;
|
|
// user — a cross-thread wake() (see add_wake), no fd attached;
|
|
// nbytes — bytes readable / writable-buffer space (backend estimate);
|
|
// udata — the word given at registration, verbatim.
|
|
Event :: struct {
|
|
fd: i32 = -1;
|
|
udata: usize = 0;
|
|
readable: bool = false;
|
|
writable: bool = false;
|
|
eof: bool = false;
|
|
err: bool = false;
|
|
user: bool = false; // a wake() delivery, not fd readiness
|
|
nbytes: i64 = 0;
|
|
}
|
|
|
|
Loop :: struct {
|
|
kq: i32 = -1;
|
|
|
|
init :: () -> Loop !EventErr {
|
|
q := kqb.kqueue();
|
|
if q < 0 { raise error.Init; }
|
|
return Loop.{ kq = q };
|
|
}
|
|
|
|
close :: (self: *Loop) {
|
|
if self.kq >= 0 { socket.close(self.kq); }
|
|
self.kq = -1;
|
|
}
|
|
|
|
add_read :: (self: *Loop, fd: i32, udata: usize) -> !EventErr {
|
|
if !kqb.kq_apply(self.kq, kqb.kev_change(fd, kqb.EVFILT_READ, kqb.EV_ADD, udata)) { raise error.Register; }
|
|
return;
|
|
}
|
|
del_read :: (self: *Loop, fd: i32) {
|
|
kqb.kq_apply(self.kq, kqb.kev_change(fd, kqb.EVFILT_READ, kqb.EV_DELETE, 0));
|
|
}
|
|
add_write :: (self: *Loop, fd: i32, udata: usize) -> !EventErr {
|
|
if !kqb.kq_apply(self.kq, kqb.kev_change(fd, kqb.EVFILT_WRITE, kqb.EV_ADD, udata)) { raise error.Register; }
|
|
return;
|
|
}
|
|
del_write :: (self: *Loop, fd: i32) {
|
|
kqb.kq_apply(self.kq, kqb.kev_change(fd, kqb.EVFILT_WRITE, kqb.EV_DELETE, 0));
|
|
}
|
|
|
|
// Register the loop's wake channel: wake() from ANY thread makes
|
|
// wait() return an Event carrying `udata` with `.user` set. EV_CLEAR
|
|
// auto-resets, so one registration serves the loop's lifetime.
|
|
// (kqueue EVFILT_USER here; the epoll twin maps to eventfd.)
|
|
add_wake :: (self: *Loop, udata: usize) -> !EventErr {
|
|
ch : kqb.Kevent = .{ ident = 0, filter = kqb.EVFILT_USER, flags = kqb.EV_ADD | kqb.EV_CLEAR, udata = udata };
|
|
if !kqb.kq_apply(self.kq, ch) { raise error.Register; }
|
|
return;
|
|
}
|
|
|
|
// Thread-safe: kevent change submission is safe from any thread.
|
|
wake :: (self: *Loop) {
|
|
ch : kqb.Kevent = .{ ident = 0, filter = kqb.EVFILT_USER, fflags = kqb.NOTE_TRIGGER };
|
|
kqb.kq_apply(self.kq, ch);
|
|
}
|
|
|
|
// Fill `out` with ready events, waiting at most `timeout_ms`
|
|
// (negative = forever). Returns the count; 0 is a timeout.
|
|
wait :: (self: *Loop, out: []Event, timeout_ms: i64) -> i64 !EventErr {
|
|
raw : [64]kqb.Kevent = ---;
|
|
cap : i64 = 64;
|
|
if xx out.len < cap { cap = xx out.len; }
|
|
n := kqb.kq_wait(self.kq, @raw[0], xx cap, timeout_ms);
|
|
if n < 0 { raise error.Wait; }
|
|
i := 0;
|
|
while i < n {
|
|
ev := raw[i];
|
|
e : Event = .{ fd = xx ev.ident, udata = ev.udata, nbytes = ev.data };
|
|
if ev.filter == kqb.EVFILT_READ { e.readable = true; }
|
|
if ev.filter == kqb.EVFILT_WRITE { e.writable = true; }
|
|
if ev.filter == kqb.EVFILT_USER { e.user = true; }
|
|
if (ev.flags & kqb.EV_EOF) != 0 { e.eof = true; }
|
|
if (ev.flags & kqb.EV_ERROR) != 0 { e.err = true; }
|
|
out[i] = e;
|
|
i += 1;
|
|
}
|
|
return xx n;
|
|
}
|
|
}
|
|
|
|
// ── deadline helpers (monotonic, std.time) ───────────────────────────
|
|
|
|
// The absolute monotonic instant `ms` from now.
|
|
deadline_in :: (ms: i64) -> i64 {
|
|
return timp.mono_ms() + ms;
|
|
}
|
|
|
|
// True once `deadline` has passed.
|
|
expired :: (deadline: i64) -> bool {
|
|
return timp.mono_ms() >= deadline;
|
|
}
|
|
|
|
// Milliseconds until `deadline`, floored at 0 — the value to hand
|
|
// `wait` so the loop wakes exactly when the nearest deadline fires.
|
|
remaining_ms :: (deadline: i64) -> i64 {
|
|
left := deadline - timp.mono_ms();
|
|
if left < 0 { return 0; }
|
|
return left;
|
|
}
|