summaryrefslogtreecommitdiff
path: root/dev/KindJob.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-18 22:34:26 -0300
committerGabriel Schneider <[email protected]>2026-11-18 22:57:27 -0300
commit250d34c1b942cd6e3726e96f6d80a9cf98d6e20e (patch)
tree30d0d8bb7cd769adf3174d215d4be7e21d754fc2 /dev/KindJob.zig
parent99f5b10eccd95aa0066ea6fe6bb75f367ab56a47 (diff)
downloadpardes-250d34c1b942cd6e3726e96f6d80a9cf98d6e20e.tar.gz
pardes-250d34c1b942cd6e3726e96f6d80a9cf98d6e20e.zip
dev: host_io.zig into its type files (the reorganisation's tree, step 2e): Host, KindJob, Inbox, Lsp and Shell each a file of its own beside it, their declarations moved as they were (a struct's body is the file's, one indent less), each file importing what it names and host_io.zig naming each (`pub const Shell = @import("Shell.zig")`), so every caller reads as before; Shell's `setenv` is pub, host_io.zig's own code calling it; the tests inside the structs move with them (host_io.Shell.test.* now Shell.test.*); nothing else changes: the ledger, frames byte-identical, replicas and process-test the base's
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'dev/KindJob.zig')
-rw-r--r--dev/KindJob.zig95
1 files changed, 95 insertions, 0 deletions
diff --git a/dev/KindJob.zig b/dev/KindJob.zig
new file mode 100644
index 00000000..c24ad88d
--- /dev/null
+++ b/dev/KindJob.zig
@@ -0,0 +1,95 @@
+const std = @import("std");
+const pardes = @import("core");
+const selection_pipe = @import("core").selection_pipe;
+const KindJob = @This();
+
+gpa: std.mem.Allocator,
+pane: u8,
+serial: u32,
+token: u32,
+gen: u32,
+ops: *const pardes.Kind.Ops,
+in: []u8,
+/// What the job allocates (`pa_out`), freed with it; `out` the last.
+arena: std.heap.ArenaAllocator,
+out: []u8 = &.{},
+id: u32 = 0,
+/// A draw's: the core it was asked in.
+draw: ?*const pardes.Pardes = null,
+
+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, .arena = .init(slot.gpa) };
+ 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));
+ job.out = job.arena.allocator().alignedAlloc(u8, .@"16", n) catch &.{};
+ return if (job.out.len == n) job.out.ptr else null;
+ }
+ };
+ 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 {
+ if (j.draw != null) return pardes.Kind.landDraw(p, j);
+ 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.arena.deinit();
+ 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;
+ if (!go(io, tasks, j, ctx, sink)) here(j, p);
+}
+
+/// `j` on a worker of `io`, posting itself to `sink` done; false when
+/// none could take it.
+pub fn go(io: std.Io, tasks: *selection_pipe.Tasks, j: *KindJob, ctx: ?*anyopaque, sink: *const fn (?*anyopaque, *KindJob) void) bool {
+ 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 false;
+ const future = io.concurrent(work, .{ j, ctx, sink }) catch return false;
+ _ = tasks.add(.{ .id = j.id, .future = future });
+ return true;
+}
+
+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);
+}