diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-23 17:11:12 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-23 17:11:12 -0300 |
| commit | 19c83c90ebed4817acd7a12d4d225f5af0957bc1 (patch) | |
| tree | e6ae660d2768063eb0c3ff0548c23077927de520 /src/macos.zig | |
| parent | ef0de36bf473f22d37cc885054bf4eed98bf275f (diff) | |
| download | pardes-19c83c90ebed4817acd7a12d4d225f5af0957bc1.tar.gz pardes-19c83c90ebed4817acd7a12d4d225f5af0957bc1.zip | |
One inbox for every host: the GUI's queue moves to host_io.Inbox, macOS and the detached server use it
The inbox takes a wake callback instead of an SDL flag and owns starting a
pty reader and an LSP query and delivering their answers, status and pipe
results. macOS gains the GUI's rule that only a pane's current reader and
the current query get in; its reap now cancels the reader on the inbox
before its future, since a reader blocked on a full queue cannot be
canceled. The detached server's completion mailbox goes; its socket stays
the loop's wakeup. Two old macOS ABI test compile errors fixed, so its unit
tests run on Linux.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/macos.zig')
| -rw-r--r-- | src/macos.zig | 425 |
1 files changed, 74 insertions, 351 deletions
diff --git a/src/macos.zig b/src/macos.zig index 22d2db71..9378359c 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -128,7 +128,7 @@ const Pty = struct { file: std.Io.File, pid: posix.pid_t, gen: u32, - reader: std.Io.Future(anyerror!void), + reader: std.Io.Future(void), /// A command pane's child, watched to its exit (host_io.watchExit); /// the pty stays open until both its end of file and its exit. cmd: host_io.CommandWatch = .{}, @@ -309,22 +309,26 @@ test "mac queued results never cancel a newer LSP request" { .runtime = .{}, .config_arena = .init(gpa), .prompt_rcs = .{}, + .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = std.testing.io }, }; defer st.config_arena.deinit(); - defer st.inbox.close(gpa, std.testing.io); + defer st.inbox.close(); core.lspRequest(0, .status, ""); const old_id = core.lsp_wait.?.id; core.lspRequest(0, .status, ""); const current_id = core.lsp_wait.?.id; - st.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } }; - st.inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = old_id, .rows = try gpa.dupe(u8, "stale result\n") } }); - try std.testing.expect(drainInbox(&st)); - try std.testing.expect(st.lsp_task != null); - try std.testing.expectEqual(current_id, st.lsp_task.?.id); + st.inbox.lsp_id = current_id; + st.inbox.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } }; + // The inbox turns the superseded answer away. + st.inbox.push(.{ .lsp = .{ .id = old_id, .rows = try gpa.dupe(u8, "stale result\n") } }); + try std.testing.expectEqual(@as(usize, 0), st.inbox.completion_len); + _ = drainInbox(&st); + try std.testing.expect(st.inbox.lsp_task != null); + try std.testing.expectEqual(current_id, st.inbox.lsp_task.?.id); try std.testing.expectEqual(current_id, core.lsp_wait.?.id); - st.inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = current_id, .rows = try gpa.dupe(u8, "") } }); + st.inbox.push(.{ .lsp = .{ .id = current_id, .rows = try gpa.dupe(u8, "") } }); try std.testing.expect(drainInbox(&st)); - try std.testing.expect(st.lsp_task == null); + try std.testing.expect(st.inbox.lsp_task == null); try std.testing.expect(core.lsp_wait == null); } @@ -348,9 +352,10 @@ test "mac worker setup failures finish matching LSP and pipe requests" { .runtime = .{}, .config_arena = .init(gpa), .prompt_rcs = .{}, + .inbox = .{ .gpa = failing.allocator(), .lsp_allocator = failing.allocator(), .io = failing_io }, }; defer st.config_arena.deinit(); - defer st.inbox.close(gpa, std.testing.io); + defer st.inbox.close(); core.lspRequest(core.active, .status, ""); const req: host_io.Lsp.Request = .{ .id = core.lsp_wait.?.id, @@ -372,7 +377,7 @@ test "mac worker setup failures finish matching LSP and pipe requests" { pipeRequest(&st, pipe_id); try std.testing.expect(core.pipe.wait == null); try std.testing.expectEqualStrings("abc", pane.file.?.content); - try std.testing.expect(st.lsp_task == null); + try std.testing.expect(st.inbox.lsp_task == null); try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len); } } @@ -400,264 +405,8 @@ test "mac host watcher covers file and directory vnode events, debounce, and gen ) != null); } -const Msg = union(enum) { - output: struct { pane: u8, gen: u32, bytes: []u8 }, - eof: struct { pane: u8, gen: u32 }, - lsp_done: struct { id: u32, rows: ?[]u8 }, - lsp_status: []u8, - pipe: selection_pipe.Response, - - fn free(m: Msg, gpa: std.mem.Allocator) void { - switch (m) { - .output => |o| gpa.free(o.bytes), - .eof => {}, - .lsp_done => |d| if (d.rows) |rows| gpa.free(rows), - .lsp_status => |t| gpa.free(t), - .pipe => |r| { - var response = r; - response.deinit(gpa); - }, - } - } -}; - -const inbox_capacity = 512; -const inbox_output_limit = inbox_capacity - pardes.MAX_PANES - selection_pipe.Tasks.capacity - 2; - -const MessageBatch = struct { - items: [inbox_capacity]Msg = undefined, - len: usize = 0, - - fn slice(batch: *MessageBatch) []Msg { - return batch.items[0..batch.len]; - } -}; - -const Inbox = struct { - mutex: std.Io.Mutex = .init, - space: std.Io.Condition = .init, - items: [inbox_capacity]Msg = undefined, - len: usize = 0, - closed: bool = false, - wake_pending: std.atomic.Value(bool) = .init(false), - - fn removeAt(q: *Inbox, offset: usize) Msg { - const removed = q.items[offset]; - std.mem.copyForwards(Msg, q.items[offset .. q.len - 1], q.items[offset + 1 .. q.len]); - q.len -= 1; - return removed; - } - - fn pushOutput(q: *Inbox, gpa: std.mem.Allocator, io: std.Io, output: @FieldType(Msg, "output")) std.Io.Cancelable!void { - errdefer gpa.free(output.bytes); - q.mutex.lockUncancelable(io); - defer q.mutex.unlock(io); - while (q.len >= inbox_output_limit and !q.closed) try q.space.wait(io, &q.mutex); - if (q.closed) return error.Canceled; - q.items[q.len] = .{ .output = output }; - q.len += 1; - } - - fn push(q: *Inbox, gpa: std.mem.Allocator, io: std.Io, m: Msg) void { - std.debug.assert(m != .output); - q.mutex.lockUncancelable(io); - defer q.mutex.unlock(io); - if (q.closed) { - m.free(gpa); - return; - } - if (m != .pipe) { - var offset: usize = 0; - while (offset < q.len) : (offset += 1) { - const old = q.items[offset]; - const replaced = switch (m) { - .eof => |end| old == .eof and old.eof.pane == end.pane, - .lsp_done => old == .lsp_done, - .lsp_status => old == .lsp_status, - .output, .pipe => unreachable, - }; - if (replaced) { - q.removeAt(offset).free(gpa); - break; - } - } - } - std.debug.assert(q.len < q.items.len); - q.items[q.len] = m; - q.len += 1; - } - - fn take(q: *Inbox, io: std.Io) MessageBatch { - q.mutex.lockUncancelable(io); - defer q.mutex.unlock(io); - var batch: MessageBatch = .{ .len = q.len }; - @memcpy(batch.items[0..q.len], q.items[0..q.len]); - q.len = 0; - q.space.broadcast(io); - return batch; - } - - fn close(q: *Inbox, gpa: std.mem.Allocator, io: std.Io) void { - q.mutex.lockUncancelable(io); - defer q.mutex.unlock(io); - q.closed = true; - for (q.items[0..q.len]) |msg| msg.free(gpa); - q.len = 0; - q.space.broadcast(io); - } -}; - -test "mac inbox coalesces completions at the tail without reordering terminal output" { - const gpa = std.testing.allocator; - var inbox: Inbox = .{}; - defer inbox.close(gpa, std.testing.io); - inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = std.math.maxInt(u32), .rows = try gpa.dupe(u8, "old result") } }); - inbox.push(gpa, std.testing.io, .{ .lsp_status = try gpa.dupe(u8, "old status") }); - try inbox.pushOutput(gpa, std.testing.io, .{ .pane = 0, .gen = std.math.maxInt(u32), .bytes = try gpa.dupe(u8, "old output") }); - inbox.push(gpa, std.testing.io, .{ .eof = .{ .pane = 0, .gen = std.math.maxInt(u32) } }); - try inbox.pushOutput(gpa, std.testing.io, .{ .pane = 0, .gen = 0, .bytes = try gpa.dupe(u8, "new output") }); - inbox.push(gpa, std.testing.io, .{ .eof = .{ .pane = 0, .gen = 0 } }); - inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = 0, .rows = try gpa.dupe(u8, "new result") } }); - inbox.push(gpa, std.testing.io, .{ .lsp_status = try gpa.dupe(u8, "new status") }); - var batch = inbox.take(std.testing.io); - defer for (batch.slice()) |msg| msg.free(gpa); - try std.testing.expectEqual(@as(usize, 5), batch.len); - try std.testing.expectEqualStrings("old output", batch.items[0].output.bytes); - try std.testing.expectEqualStrings("new output", batch.items[1].output.bytes); - try std.testing.expectEqual(@as(u32, 0), batch.items[2].eof.gen); - try std.testing.expectEqual(@as(u32, 0), batch.items[3].lsp_done.id); - try std.testing.expectEqualStrings("new result", batch.items[3].lsp_done.rows.?); - try std.testing.expectEqualStrings("new status", batch.items[4].lsp_status); - inbox.close(gpa, std.testing.io); - inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = 1, .rows = try gpa.dupe(u8, "closed") } }); - try std.testing.expectEqual(@as(usize, 0), inbox.len); -} - -test "mac inbox admits every retained task completion when output fills the queue" { - const gpa = std.testing.allocator; - var inbox: Inbox = .{}; - defer inbox.close(gpa, std.testing.io); - var tasks: selection_pipe.Tasks = .{}; - defer tasks.cancelAll(std.testing.io); - for (0..inbox_output_limit) |_| try inbox.pushOutput(gpa, std.testing.io, .{ - .pane = 0, - .gen = 1, - .bytes = try gpa.dupe(u8, "output"), - }); - for (0..pardes.MAX_PANES) |pane| inbox.push(gpa, std.testing.io, .{ .eof = .{ .pane = @intCast(pane), .gen = 1 } }); - inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = 1, .rows = try gpa.dupe(u8, "result") } }); - for (0..selection_pipe.Tasks.capacity) |index| { - const id: u32 = @intCast(index); - try std.testing.expect(tasks.add(.{ .id = id, .future = .{ .any_future = null, .result = {} } })); - if (index == 0) { - const output = try gpa.dupe(u8, "filtered"); - const outputs = gpa.dupe([]u8, &.{output}) catch |err| { - gpa.free(output); - return err; - }; - inbox.push(gpa, std.testing.io, .{ .pipe = .{ .id = id, .success = true, .outputs = outputs } }); - } else inbox.push(gpa, std.testing.io, .{ .pipe = .{ - .id = id, - .success = false, - .outputs = &.{}, - .failure = .{ .kind = .exit, .code = 1, .stderr = try gpa.dupe(u8, "no match") }, - } }); - } - inbox.push(gpa, std.testing.io, .{ .lsp_status = try gpa.dupe(u8, "current status") }); - try std.testing.expectEqual(inbox_capacity, inbox.len); - var batch = inbox.take(std.testing.io); - defer for (batch.slice()) |msg| msg.free(gpa); - var eof_count: usize = 0; - var lsp_count: usize = 0; - var pipe_count: usize = 0; - var output_count: usize = 0; - for (batch.slice()) |msg| switch (msg) { - .eof => eof_count += 1, - .lsp_done => lsp_count += 1, - .pipe => |result| { - pipe_count += 1; - tasks.finish(std.testing.io, result.id); - }, - .output => |output| { - output_count += 1; - try std.testing.expectEqualStrings("output", output.bytes); - }, - .lsp_status => |status| try std.testing.expectEqualStrings("current status", status), - }; - try std.testing.expectEqual(pardes.MAX_PANES, eof_count); - try std.testing.expectEqual(@as(usize, 1), lsp_count); - try std.testing.expectEqual(selection_pipe.Tasks.capacity, pipe_count); - try std.testing.expectEqual(inbox_output_limit, output_count); - try std.testing.expectEqual(@as(usize, 0), tasks.len); -} - -test "mac inbox preserves output under backpressure and wakes on take cancel and close" { - const gpa = std.testing.allocator; - const io = std.testing.io; - const Producer = struct { - fn run(q: *Inbox, done: *std.Io.Event) !void { - defer done.set(std.testing.io); - defer q.push(std.testing.allocator, std.testing.io, .{ .eof = .{ .pane = 0, .gen = 1 } }); - try q.pushOutput(std.testing.allocator, std.testing.io, .{ - .pane = 0, - .gen = 1, - .bytes = try std.testing.allocator.dupe(u8, "tail\x1b[0m"), - }); - try q.pushOutput(std.testing.allocator, std.testing.io, .{ - .pane = 0, - .gen = 1, - .bytes = try std.testing.allocator.dupe(u8, "é😀"), - }); - } - }; - for ([_]enum { take, cancel, close }{ .take, .cancel, .close }) |action| { - var inbox: Inbox = .{}; - defer inbox.close(gpa, io); - for (0..inbox_output_limit) |_| try inbox.pushOutput(gpa, io, .{ - .pane = 0, - .gen = 1, - .bytes = try gpa.dupe(u8, "before"), - }); - var done: std.Io.Event = .unset; - var future = try io.concurrent(Producer.run, .{ &inbox, &done }); - defer future.cancel(io) catch {}; - for (0..1000) |_| { - if (inbox.space.state.load(.acquire).waiters != 0) break; - try io.sleep(.fromMilliseconds(1), .awake); - } - try std.testing.expectEqual(@as(u16, 1), inbox.space.state.load(.acquire).waiters); - switch (action) { - .take => { - var before = inbox.take(io); - defer for (before.slice()) |msg| msg.free(gpa); - try std.testing.expectEqual(inbox_output_limit, before.len); - for (before.slice()) |msg| try std.testing.expectEqualStrings("before", msg.output.bytes); - try done.waitTimeout(io, .{ .duration = .{ .raw = .fromSeconds(2), .clock = .awake } }); - try future.await(io); - var after = inbox.take(io); - defer for (after.slice()) |msg| msg.free(gpa); - try std.testing.expectEqual(@as(usize, 3), after.len); - try std.testing.expectEqualStrings("tail\x1b[0m", after.items[0].output.bytes); - try std.testing.expectEqualStrings("é😀", after.items[1].output.bytes); - try std.testing.expect(after.items[2] == .eof); - }, - .cancel => { - try std.testing.expectError(error.Canceled, future.cancel(io)); - var batch = inbox.take(io); - defer for (batch.slice()) |msg| msg.free(gpa); - try std.testing.expectEqual(inbox_output_limit + 1, batch.len); - for (batch.slice()[0..inbox_output_limit]) |msg| try std.testing.expectEqualStrings("before", msg.output.bytes); - try std.testing.expect(batch.items[inbox_output_limit] == .eof); - }, - .close => { - inbox.close(gpa, io); - try done.waitTimeout(io, .{ .duration = .{ .raw = .fromSeconds(2), .clock = .awake } }); - try std.testing.expectError(error.Canceled, future.await(io)); - try std.testing.expectEqual(@as(usize, 0), inbox.len); - }, - } - } -} +const Inbox = host_io.Inbox; +const Msg = Inbox.Msg; const State = struct { ninep: ?*ninep_io.Listener = null, @@ -681,8 +430,9 @@ const State = struct { panel_tracks: [pardes.MAX_PANES * 2]PanelTrack = undefined, panel_tracks_len: usize = 0, ptys: [pardes.MAX_PANES]?Pty = @splat(null), - inbox: Inbox = .{}, - lsp_task: ?host_io.Lsp.Task = null, + inbox: Inbox = undefined, + /// A wakeup is on its way to the runtime: the next ones are not sent. + wake_pending: std.atomic.Value(bool) = .init(false), pipe_tasks: selection_pipe.Tasks = .{}, file_watches: FileWatches = .{}, gens: [pardes.MAX_PANES]u32 = @splat(0), @@ -795,6 +545,7 @@ fn initCore(runtime: ?*const Runtime, cols_arg: u16, rows_arg: u16) !void { .runtime = if (runtime) |r| r.* else .{}, }; const st = &state.?; + st.inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = io, .wake_fn = wakeHost, .wake_ctx = st }; st.ninep = ninep_io.start(io, gpa, core); core.host = hostFor(st); @@ -807,7 +558,7 @@ fn initCore(runtime: ?*const Runtime, cols_arg: u16, rows_arg: u16) !void { if (st.ninep) |listener| listener.watch(st, wakeNinep) catch |err| core.reportError(0, "9p wake", err); for (&st.ptys, 0..) |*slot, id| if (slot.*) |*pt| startReader(st, pt, @intCast(id)); - pardes.lsp.setStatusSink(st, lspStatusSink); + pardes.lsp.setStatusSink(&st.inbox, Inbox.lspStatus); host_io.setExitWake(st, wakeForExit); // From here the core is touched only inside the entry points above, // each of which takes the turn for its own duration; between them the @@ -829,9 +580,9 @@ export fn pardes_deinit() void { } pardes.lsp.setStatusSink(null, null); host_io.setExitWake(null, null); - if (st.lsp_task) |*t| { + if (st.inbox.lsp_task) |*t| { t.future.cancel(st.io); - st.lsp_task = null; + st.inbox.lsp_task = null; } st.pipe_tasks.cancelAll(st.io); for (0..pardes.MAX_PANES) |pane| if (st.file_watches.entries[pane] != null) { @@ -844,7 +595,7 @@ export fn pardes_deinit() void { hostWatchFile(theme_watch_pane, generation, null, 0); } for (0..pardes.MAX_PANES) |pane| reap(st, @intCast(pane)); - st.inbox.close(st.gpa, st.io); + st.inbox.close(); st.file_watches.deinit(st.gpa); if (st.cells.len > 0) st.gpa.free(st.cells); for (st.layer_cells) |cells| st.gpa.free(cells); @@ -1295,12 +1046,12 @@ export fn pardes_watch_changed(pane: u8, generation: u32) void { } fn drainInbox(st: *State) bool { - var batch = st.inbox.take(st.io); + var batch = st.inbox.take(); // After this batch's output, which an exit is told behind. defer takeExits(st); var did = batch.len > 0; for (batch.slice()) |msg| { - defer msg.free(st.gpa); + defer msg.deinit(st.gpa, st.gpa); switch (msg) { .output => |o| { if (st.gens[o.pane] != o.gen) continue; @@ -1330,11 +1081,11 @@ fn drainInbox(st: *State) bool { reap(st, e.pane); st.core.update(.{ .eof = .{ .pane = e.pane } }); }, - .lsp_done => |d| { + .lsp => |d| { st.core.update(.{ .lsp_resp = .{ .id = d.id, .rows = d.rows } }); - if (st.lsp_task) |*t| if (t.id == d.id) { + if (st.inbox.lsp_task) |*t| if (t.id == d.id) { t.future.await(st.io); - st.lsp_task = null; + st.inbox.lsp_task = null; }; }, .lsp_status => |text| { @@ -1350,6 +1101,8 @@ fn drainInbox(st: *State) bool { } }); st.pipe_tasks.finish(st.io, value.id); }, + // The SDL shell's: this one's file watches and 9P are its own. + .files_changed, .fs_ready => {}, } } for (0..pardes.MAX_PANES) |pane| { @@ -1367,7 +1120,7 @@ export fn pardes_tick() bool { pardes.turn.wake(); defer pardes.turn.rest(); const st = &(state orelse return false); - st.inbox.wake_pending.store(false, .release); + st.wake_pending.store(false, .release); var did = drainInbox(st); if (st.ninep) |listener| { const drained = listener.tick(); @@ -1396,17 +1149,17 @@ fn restoreCore(st: *State) bool { st.core.reportError(st.core.active, "Restore", err); return false; }; - if (st.lsp_task) |*task| { + if (st.inbox.lsp_task) |*task| { task.future.cancel(st.io); - st.lsp_task = null; + st.inbox.lsp_task = null; } st.pipe_tasks.cancelAll(st.io); for (0..pardes.MAX_PANES) |pane| { reap(st, @intCast(pane)); st.gens[pane] +%= 1; } - var stale = st.inbox.take(st.io); - for (stale.slice()) |msg| msg.free(st.gpa); + var stale = st.inbox.take(); + for (stale.slice()) |msg| msg.deinit(st.gpa, st.gpa); for (0..watch_slot_count) |pane| if (st.file_watches.entries[pane] != null) { const id: u8 = @intCast(pane); const generation = st.file_watches.stop(st.gpa, id); @@ -1447,13 +1200,14 @@ test "mac Restore keeps host state and rejects callbacks from the old core" { .runtime = .{}, .config_arena = .init(gpa), .prompt_rcs = .{}, + .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = std.testing.io }, .frame_len = 7, .rotate_coasting = true, }; defer st.core.deinit(); defer st.config_arena.deinit(); defer st.file_watches.deinit(gpa); - defer st.inbox.close(gpa, std.testing.io); + defer st.inbox.close(); const marker = try st.config_arena.allocator().dupe(u8, "host configuration"); _ = try core.setTestFile("saved body\n"); const old_serial = core.panes[0].?.serial; @@ -1472,19 +1226,21 @@ test "mac Restore keeps host state and rejects callbacks from the old core" { core.lspRequest(0, .status, ""); const old_request = core.lsp_wait.?.id; - st.lsp_task = .{ .id = old_request, .future = .{ .any_future = null, .result = {} } }; - st.inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = old_request, .rows = try gpa.dupe(u8, "old result") } }); + st.inbox.lsp_id = old_request; + st.inbox.lsp_task = .{ .id = old_request, .future = .{ .any_future = null, .result = {} } }; + st.inbox.push(.{ .lsp = .{ .id = old_request, .rows = try gpa.dupe(u8, "old result") } }); for (0..selection_pipe.Tasks.capacity) |index| { const id: u32 = @intCast(index); try std.testing.expect(st.pipe_tasks.add(.{ .id = id, .future = .{ .any_future = null, .result = {} } })); - st.inbox.push(gpa, std.testing.io, .{ .pipe = .{ + st.inbox.push(.{ .pipe = .{ .id = id, .success = false, .outputs = &.{}, .failure = .{ .kind = .exit, .code = 1, .stderr = try gpa.dupe(u8, "old filter failure") }, } }); } - try st.inbox.pushOutput(gpa, std.testing.io, .{ .pane = 0, .gen = 23, .bytes = try gpa.dupe(u8, "old PTY output") }); + st.inbox.acceptReader(0, 23); + _ = st.inbox.pushOutput(0, 23, try gpa.dupe(u8, "old PTY output")); core.update(.{ .command = try std.fmt.bufPrint(&command, "Restore .zig-cache/tmp/{s}/restore.zon", .{tmp.sub_path}) }); try std.testing.expect(restoreCore(&st)); try std.testing.expect(st.core != core); @@ -1498,17 +1254,18 @@ test "mac Restore keeps host state and rejects callbacks from the old core" { try std.testing.expect(st.file_watches.generations[0] > old_watch); try std.testing.expectEqual(@as(usize, 0), st.inbox.len); try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len); - try std.testing.expect(st.lsp_task == null); + try std.testing.expect(st.inbox.lsp_task == null); try std.testing.expect(!drainInbox(&st)); try std.testing.expect(!st.cwd_stale[0]); st.core.lspRequest(0, .status, ""); const new_request = st.core.lsp_wait.?.id; try std.testing.expect(new_request > old_request); - st.lsp_task = .{ .id = new_request, .future = .{ .any_future = null, .result = {} } }; - st.inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = new_request, .rows = &.{} } }); + st.inbox.lsp_id = new_request; + st.inbox.lsp_task = .{ .id = new_request, .future = .{ .any_future = null, .result = {} } }; + st.inbox.push(.{ .lsp = .{ .id = new_request, .rows = &.{} } }); try std.testing.expect(drainInbox(&st)); - try std.testing.expect(st.lsp_task == null); + try std.testing.expect(st.inbox.lsp_task == null); try std.testing.expect(st.core.lsp_wait == null); const pane = st.core.panes[0].?; @@ -1524,7 +1281,7 @@ test "mac Restore keeps host state and rejects callbacks from the old core" { gpa.free(output); return err; }; - st.inbox.push(gpa, std.testing.io, .{ .pipe = .{ .id = pipe_id, .success = true, .outputs = outputs } }); + st.inbox.push(.{ .pipe = .{ .id = pipe_id, .success = true, .outputs = outputs } }); try std.testing.expect(drainInbox(&st)); try std.testing.expect(st.core.pipe.wait == null); try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len); @@ -1544,19 +1301,17 @@ test "mac Restore cancels a PTY reader waiting for output capacity" { .runtime = .{}, .config_arena = .init(gpa), .prompt_rcs = .{}, + .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = io }, }; defer st.core.deinit(); defer st.config_arena.deinit(); defer st.file_watches.deinit(gpa); - defer st.inbox.close(gpa, io); + defer st.inbox.close(); try dump.dumpState(st.core); try tmp.dir.writeFile(io, .{ .sub_path = "restore.zon", .data = st.core.dump_out.? }); while (st.core.nextEffect()) |_| {} - for (0..inbox_output_limit) |_| try st.inbox.pushOutput(gpa, io, .{ - .pane = 0, - .gen = 1, - .bytes = try gpa.dupe(u8, "retained output"), - }); + st.inbox.acceptReader(1, 1); + for (0..Inbox.output_capacity) |_| _ = st.inbox.pushOutput(1, 1, try gpa.dupe(u8, "retained output")); const child = try host_io.forkShell(null, 0, &st.prompt_rcs, "/bin/sh", "", 12, 40, null); st.gens[0] = 1; st.ptys[0] = .{ .file = child.file, .pid = child.pid, .gen = 1, .reader = .{ .any_future = null, .result = {} } }; @@ -1580,9 +1335,10 @@ test "mac Restore cancels a PTY reader waiting for output capacity" { try std.testing.expect(st.ptys[0] == null); try std.testing.expectEqual(@as(usize, 0), st.inbox.len); try std.testing.expectEqual(@as(u16, 0), st.inbox.space.state.load(.acquire).waiters); - try st.inbox.pushOutput(gpa, io, .{ .pane = 0, .gen = st.gens[0], .bytes = try gpa.dupe(u8, "fresh output") }); - var batch = st.inbox.take(io); - defer for (batch.slice()) |msg| msg.free(gpa); + st.inbox.acceptReader(0, st.gens[0]); + _ = st.inbox.pushOutput(0, st.gens[0], try gpa.dupe(u8, "fresh output")); + var batch = st.inbox.take(); + defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); try std.testing.expectEqual(@as(usize, 1), batch.len); try std.testing.expectEqualStrings("fresh output", batch.items[0].output.bytes); } @@ -2374,31 +2130,12 @@ fn hostFor(st: *State) pardes.Host { fn lspRequest(ctx: ?*anyopaque, req: host_io.Lsp.Request) void { const st = hostState(ctx); - host_io.Lsp.start(st.io, st.gpa, st.core, &st.lsp_task, req, st, deliverLspRows); + st.inbox.startLsp(st.core, req); } fn pipeRequest(ctx: ?*anyopaque, id: u32) void { const st = hostState(ctx); - selection_pipe.start(st.io, st.gpa, st.core, &st.pipe_tasks, id, st, deliverPipe); -} - -fn deliverPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void { - const st: *State = @ptrCast(@alignCast(ctx orelse return)); - st.inbox.push(st.gpa, st.io, .{ .pipe = response }); - wake(st); -} - -fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { - const st: *State = @ptrCast(@alignCast(ctx orelse return)); - st.inbox.push(st.gpa, st.io, .{ .lsp_done = .{ .id = id, .rows = rows } }); - wake(st); -} - -fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void { - const st: *State = @ptrCast(@alignCast(ctx orelse return)); - const copy = st.gpa.dupe(u8, text) catch return; - st.inbox.push(st.gpa, st.io, .{ .lsp_status = copy }); - wake(st); + selection_pipe.start(st.io, st.gpa, st.core, &st.pipe_tasks, id, &st.inbox, Inbox.pushPipe); } fn hostState(ctx: ?*anyopaque) *State { @@ -2525,7 +2262,7 @@ fn openLink(_: ?*anyopaque, url: []const u8) void { } fn startReader(st: *State, pt: *Pty, id: u8) void { - pt.reader = st.io.concurrent(readPty, .{ st, st.io, pt.file, id, pt.gen }) catch |err| { + pt.reader = st.inbox.startReader(pt.file, id, pt.gen) catch |err| { log.err("pane {d} has no reader ({t}); it will not show output", .{ id, err }); return; }; @@ -2542,29 +2279,20 @@ fn closePty(ctx: ?*anyopaque, pane: u8) void { fn reap(st: *State, pane: u8) void { var pt = st.ptys[pane] orelse return; st.ptys[pane] = null; - pt.reader.cancel(st.io) catch {}; + // The inbox first: a reader waiting for space there wakes and returns. + st.inbox.cancelReader(pane, pt.gen); + pt.reader.cancel(st.io); _ = libc.close(pt.file.handle); host_io.retireShell(pt.pid); } -fn readPty(st: *State, io: std.Io, pty: std.Io.File, id: u8, gen: u32) anyerror!void { - // One buffer, read into directly: a Reader around it only added a second - // (and in a safe build, filled both as `undefined` on every start). - var buf: [0x10000]u8 = undefined; - while (true) { - const n = pty.readStreaming(io, &.{&buf}) catch break; - if (n == 0) break; - const bytes = st.gpa.dupe(u8, buf[0..n]) catch break; - st.inbox.pushOutput(st.gpa, io, .{ .pane = id, .gen = gen, .bytes = bytes }) catch break; - wake(st); - } - st.inbox.push(st.gpa, st.io, .{ .eof = .{ .pane = id, .gen = gen } }); - wake(st); +fn wakeHost(ctx: ?*anyopaque) void { + wake(@ptrCast(@alignCast(ctx.?))); } fn wake(st: *State) void { const cb = st.runtime.wakeup orelse return; - if (st.inbox.wake_pending.swap(true, .acq_rel)) return; + if (st.wake_pending.swap(true, .acq_rel)) return; cb(st.runtime.userdata); } @@ -2750,9 +2478,9 @@ fn takeRotationNotches(lag: *f32, degrees: f32) i32 { fn expectSameAbi(comptime C: type, comptime Z: type) !void { const c_fn = @typeInfo(C).@"fn"; const z_fn = @typeInfo(Z).@"fn"; - try std.testing.expectEqual(c_fn.params.len, z_fn.params.len); - inline for (c_fn.params, z_fn.params) |cp, zp| - try std.testing.expectEqual(@sizeOf(cp.type.?), @sizeOf(zp.type.?)); + try std.testing.expectEqual(c_fn.param_types.len, z_fn.param_types.len); + inline for (c_fn.param_types, z_fn.param_types) |cp, zp| + try std.testing.expectEqual(@sizeOf(cp.?), @sizeOf(zp.?)); try std.testing.expectEqual(@sizeOf(c_fn.return_type.?), @sizeOf(z_fn.return_type.?)); } @@ -2844,13 +2572,8 @@ test "pardes.h matches the Zig boundary" { inline for (@typeInfo(PanelBox).@"struct".field_names) |name| try expectEqual(@offsetOf(c.pardes_panel_box_s, name), @offsetOf(PanelBox, name)); try expectEqual(@sizeOf(c.pardes_panel_track_s), @sizeOf(PanelTrack)); - inline for (@typeInfo(PanelTrack).@"struct".field_names) |name| { - const c_name = comptime if (std.mem.eql(u8, name, "motion")) - "reserved0" - else - name; - try expectEqual(@offsetOf(c.pardes_panel_track_s, c_name), @offsetOf(PanelTrack, name)); - } + inline for (@typeInfo(PanelTrack).@"struct".field_names) |name| + try expectEqual(@offsetOf(c.pardes_panel_track_s, name), @offsetOf(PanelTrack, name)); try expectEqual(@sizeOf(c.pardes_image_s), @sizeOf(Image)); inline for (@typeInfo(Image).@"struct".field_names) |name| try expectEqual(@offsetOf(c.pardes_image_s, name), @offsetOf(Image, name)); |
