diff options
Diffstat (limited to 'src/host_io.zig')
| -rw-r--r-- | src/host_io.zig | 104 |
1 files changed, 100 insertions, 4 deletions
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 }); |
