From 9c66cdc9049b49f128336dcf5046e2bb1321e281 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 02:31:55 -0300 Subject: kinds #5, ABI 2 and the seam: src/kinds/pardes_addon.h is ABI 2 as architecture ยง16.9 has it (pa_window 24 bytes and no art bytes, pa_placement 80 with at, from and dirty_y/dirty_h and no stride, pa_word 32 with no size and PA_WORD, tick(st, now_ns, flags) with PA_FOCUSED, PA_OK and the conventions, the dump and load rules, pa_put with attrs, pa_copy and PA_POD, whose fragment gcc -Werror refused as written and is ternaries here) and fake.c and image's table speak it; tick is called at its deadline and once after open, a taken key or click, a word, a job's answer, a load and a change of PA_FOCUSED (the pane has the keyboard in some view, its window focused), nextWake folds the deadlines, and a Debug build hashes the kind's allocations across paint and panics naming it; paint runs once a frame, the slot keeps its prefix and whether it placed a picture, the tag is that prefix, the core's art words when it did and the path, and a frame that changes it owes a step to lay the tag out again; Addon load and reload are an addon effect the host performs (memfd, BLAKE3, dlopen, the head checks and init, the bytes a process holds shared and counted) answered by an addon event carrying what the library brings as data, whose update does the collisions, the reload's validate-then-swap at a new generation and the waiting panes deterministically, while unload is the update's alone and lets the library go by effect; a kind's job is Effect.job run on the host's workers (host_io.KindJob: tui and gui on Io.concurrent, macOS and tests on the loop) answered by Event.job, stale generations dropped and a library closed after its last job; each core has its own Registry (16 kinds, their words, leaders and extensions, digested and dumped; their code a local side array a replica has none of), so a test session no longer unloads the world and Restore and -l keep the process's kinds; every call into a kind whose answer the core acts on is taped as an Outcome (its answer, refusal, message, Look, text pane) and the prefix a step reads is taped too, so a replica's core, which runs no kind's code, steps alike; a pane of a kind on a replica is a Shadow drawn from the authority's frames, painted for the peer's view and sent by Screens as row hashes with the rows that changed and each raster once a key (screen_state.Shadow), its keys going to the authority; the tail, reload and the Addon word and event are Addon.zig's, the slot's record dump.zig's, a tail word's run exec.zig's; --addon-test gains focus, blur, dump, body, ctl and the window line, its jobs done as a host does them; fake.out gains its window line, loses a body row once the tag carries the art words and lands FakeJob at once; tests rewritten: the image dump test (the replica-style load keeps the record), the Petscii/Palette/Ascii tag test in ctl.zig and test/panes.zig's image tag test (a picture decoded and shown first), fs_namespace's decode test (the job performed as a host would), Kind.zig's registry test and Addon.zig's crash-mark test, the addons suite's ABI numbers; new: a job under way across a reload (T1), a Shadow's frame codec, and a replica with no code for a kind showing its pane and passing its keys; gates: core-test 958/958 and behaviour-test 249/249 on both, unit-test 1490/1491 tui and 1533/1534 gui, pane-test 171/172, snap all 105 ok, process-test 0 of 37 failed on both, addon-test ok, hxdiff 19 and hxparity 6 mismatches by the base's names, hxgolf 0, replicas, gui-golden, monkey-gui, daemon.py main and window-clients, soak local pass, perf medium within 1% of the base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/host_io.zig | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 100 insertions(+), 4 deletions(-) (limited to 'src/host_io.zig') diff --git a/src/host_io.zig b/src/host_io.zig index c00eb453..1808d266 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -66,9 +66,98 @@ pub const Host = struct { /// Carry the dump of handover `seq` to another window on a worker /// (Handover.start), which answers through the inbox's `handed`. hand_over: ?*const fn (ctx: ?*anyopaque, seq: u32, bytes: []const u8) void = null, + /// Run job `token` of the pane in slot `pane` (whose serial is + /// `serial`) on a worker (`KindJob`), answering with the `job` event; + /// absent, it runs on the loop. + job: ?*const fn (ctx: ?*anyopaque, pane: u8, serial: u32, token: u32) void = null, }; }; +/// A kind's job (Effect.job), taken off its pane: its input, its kind's +/// `job_run` and generation; run on a worker (or here), its answer the +/// core's `job` event. Its library is held until it lands (Addon.holdJob). +pub const KindJob = struct { + gpa: std.mem.Allocator, + pane: u8, + serial: u32, + token: u32, + gen: u32, + ops: *const pardes.Kind.Ops, + in: []u8, + out: []u8 = &.{}, + id: u32 = 0, + + pub fn take(p: *pardes.Pardes, pane: u8, serial: u32, token: u32) ?*KindJob { + const pn = p.panes[pane] orelse return null; + const slot = pn.addon orelse return null; + const ops = slot.ops orelse return null; + if (pn.serial != serial or ops.job_run == null) return null; + const at = for (&slot.jobs) |*j| { + if (j.*) |job| if (job.token == token) break j; + } else return null; + const j = slot.gpa.create(KindJob) catch return null; + j.* = .{ .gpa = slot.gpa, .pane = pane, .serial = serial, .token = token, .gen = slot.gen, .ops = ops, .in = at.*.?.in }; + at.* = null; + pardes.Addon.holdJob(ops, true); + return j; + } + + pub fn run(j: *KindJob) void { + const Out = struct { + fn alloc(out: [*c]pardes.Kind.c.pa_out, n: usize) callconv(.c) ?*anyopaque { + const job: *KindJob = @ptrCast(@alignCast(out.*.ctx)); + if (job.out.len > 0) return null; + job.out = job.gpa.alloc(u8, n) catch return null; + return job.out.ptr; + } + }; + var out: pardes.Kind.c.pa_out = .{ .alloc = Out.alloc, .ctx = j }; + const n = j.ops.job_run.?(j.in.ptr, @intCast(j.in.len), &out); + j.out = j.out[0..@min(n, j.out.len)]; + } + + /// Its answer to the core, and it gone. + pub fn land(j: *KindJob, p: *pardes.Pardes) void { + p.update(.{ .job = .{ .pane = j.pane, .serial = j.serial, .token = j.token, .gen = j.gen, .bytes = j.out } }); + j.drop(); + } + + pub fn drop(j: *KindJob) void { + pardes.Addon.holdJob(j.ops, false); + j.gpa.free(j.in); + j.gpa.free(j.out); + j.gpa.destroy(j); + } + + /// On a worker of `io`, posting itself to `sink` done: a host's `job`. + pub fn start(io: std.Io, p: *pardes.Pardes, tasks: *selection_pipe.Tasks, pane: u8, serial: u32, token: u32, ctx: ?*anyopaque, sink: *const fn (?*anyopaque, *KindJob) void) void { + const j = take(p, pane, serial, token) orelse return; + next_id +%= 1; + j.id = next_id | 0x8000_0000; + const work = struct { + fn work(job: *KindJob, c: ?*anyopaque, s: *const fn (?*anyopaque, *KindJob) void) void { + job.run(); + s(c, job); + } + }.work; + if (tasks.full()) return here(j, p); + const future = io.concurrent(work, .{ j, ctx, sink }) catch return here(j, p); + _ = tasks.add(.{ .id = j.id, .future = future }); + } + + var next_id: u32 = 0; + + fn here(j: *KindJob, p: *pardes.Pardes) void { + j.run(); + j.land(p); + } + + /// No workers (a test, the fallback): it runs on the loop, at once. + pub fn runHere(p: *pardes.Pardes, pane: u8, serial: u32, token: u32) void { + here(take(p, pane, serial, token) orelse return, p); + } +}; + /// PARDES_TEST_CLOCK makes a shell's `Host.now` answer a virtual clock /// instead of `std.Io.Clock.awake`, moved only when a timed wait runs out -- /// straight to the core's next wake -- so the snapshot harness sees the same @@ -82,7 +171,6 @@ pub fn testClock() ?u64 { /// loop (pdf_view.workers). `stopWorkers` is its defer. pub fn startWorkers(io: std.Io, allocs: pardes.memory.Allocators) void { pardes.image.start(io, allocs.image); - pardes.Addon.scan(); if (comptime pardes.pdf_enabled) { pardes.pdf.start(allocs.pdf); pardes.panes.Pdf.workers = io; @@ -253,6 +341,8 @@ pub const Inbox = struct { /// How a handover went (Handover.zig): `words` is where it went, or /// why it did not; gpa-owned. handed: struct { seq: u32, ok: bool, words: []u8 }, + /// A kind's job done on a worker (KindJob), never dropped. + kind_job: *KindJob, files_changed, fs_ready, /// The daemon asks for the window again (`pardes-gui --attach`). @@ -272,6 +362,7 @@ pub const Inbox = struct { }, .search => |b| b.deinit(gpa), .handed => |h| gpa.free(h.words), + .kind_job => |j| j.drop(), .adopted => |a| if (a.fd >= 0) { _ = libc.close(a.fd); }, @@ -284,7 +375,7 @@ pub const Inbox = struct { pub const output_capacity = capacity - pardes.MAX_PANES; pub const Batch = struct { - items: [capacity + selection_pipe.Tasks.capacity + 2]Msg = undefined, + items: [capacity + 2 * selection_pipe.Tasks.capacity + 2]Msg = undefined, len: usize = 0, pub fn slice(batch: *Batch) []Msg { @@ -312,7 +403,7 @@ pub const Inbox = struct { readers: [pardes.MAX_PANES]?u32 = @splat(null), waiting: usize = 0, lsp_id: ?u32 = null, - completions: [selection_pipe.Tasks.capacity + 1]Msg = undefined, + completions: [2 * selection_pipe.Tasks.capacity + 1]Msg = undefined, completion_len: usize = 0, pub fn lock(q: *Inbox) void { @@ -402,7 +493,7 @@ pub const Inbox = struct { } switch (m) { .files_changed => q.files_changed = true, - .lsp, .pipe => { + .lsp, .pipe, .kind_job => { if (m == .lsp) { if (q.lsp_id != m.lsp.id) { q.unlock(); @@ -554,6 +645,11 @@ pub const Inbox = struct { } /// A `|` job's answer (selection_pipe.start's sink). + pub fn pushJob(ctx: ?*anyopaque, j: *KindJob) void { + const q: *Inbox = @ptrCast(@alignCast(ctx.?)); + q.push(.{ .kind_job = j }); + } + pub fn pushPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void { const q: *Inbox = @ptrCast(@alignCast(ctx.?)); q.push(.{ .pipe = response }); -- cgit v1.3