diff options
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/Inbox.zig | 21 | ||||
| -rw-r--r-- | dev/Ptys.zig | 34 | ||||
| -rw-r--r-- | dev/host_io.zig | 2 | ||||
| -rw-r--r-- | dev/tui/tui.zig | 8 |
4 files changed, 58 insertions, 7 deletions
diff --git a/dev/Inbox.zig b/dev/Inbox.zig index 347ad278..d733b971 100644 --- a/dev/Inbox.zig +++ b/dev/Inbox.zig @@ -86,6 +86,11 @@ len: usize = 0, closed: bool = false, files_changed: bool = false, readers: [pardes.MAX_PANES]?u32 = @splat(null), +/// Each pane's reads read out of its pty and not yet applied (`applied`): +/// a command's exit is told only once none is (Ptys.unapplied), since +/// the poll that asks whether its output is in (CommandWatch.due) cannot +/// see a read the reader holds or the queue does. +unapplied: [pardes.MAX_PANES]std.atomic.Value(u32) = @splat(.init(0)), waiting: usize = 0, lsp_id: ?u32 = null, completions: [2 * selection_pipe.Tasks.capacity + 1]Msg = undefined, @@ -110,6 +115,12 @@ pub fn acceptReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void { q.readers[pane.index()] = gen; } +/// One of `pane`'s reads applied, or dropped unapplied. +pub fn applied(q: *Inbox, pane: pardes.Pane.Id) void { + const n = &q.unapplied[pane.index()]; + if (n.load(.acquire) > 0) _ = n.fetchSub(1, .acq_rel); +} + pub fn cancelReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void { q.lock(); defer q.unlock(); @@ -123,7 +134,10 @@ pub fn cancelReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void { .eof => |e| e.pane == pane and e.gen == gen, else => false, }; - if (matches) q.removeAt(i).deinit(q.gpa, q.lsp_allocator) else i += 1; + if (matches) { + if (msg == .output) q.applied(pane); + q.removeAt(i).deinit(q.gpa, q.lsp_allocator); + } else i += 1; } q.space.broadcast(q.io); } @@ -302,11 +316,14 @@ fn readPty(q: *Inbox, pty: std.Io.File, pane: pardes.Pane.Id, gen: u32) void { }, }; if (n == 0) continue; + // Counted at once: out of the pty, the poll no longer sees it. + _ = q.unapplied[pane.index()].fetchAdd(1, .acq_rel); const bytes = q.gpa.dupe(u8, buf[0..n]) catch |err| { + q.applied(pane); failure = err; break; }; - if (!q.pushOutput(pane, gen, bytes)) return; + if (!q.pushOutput(pane, gen, bytes)) return q.applied(pane); } q.push(.{ .eof = .{ .pane = pane, .gen = gen, .failure = failure } }); } diff --git a/dev/Ptys.zig b/dev/Ptys.zig index 16a31939..583e3c38 100644 --- a/dev/Ptys.zig +++ b/dev/Ptys.zig @@ -180,6 +180,7 @@ pub fn close(p: *Ptys, id: pardes.Pane.Id) void { /// A reader's read, off the queue (freed here). pub fn output(p: *Ptys, core: *pardes.Pardes, o: @FieldType(Inbox.Msg, "output")) void { defer p.queue.gpa.free(o.bytes); + defer p.queue.applied(o.pane); if (p.gens[o.pane.index()] == o.gen) core.update(.{ .output = .{ .pane = o.pane, .bytes = o.bytes } }); } @@ -213,6 +214,11 @@ pub fn takeExits(p: *Ptys, core: *pardes.Pardes) void { host_io.takeExits(core, &p.slots, p, closeWatched); } +/// Whether pane `id` has reads not yet applied (host_io.takeExits). +pub fn unapplied(p: *const Ptys, id: usize) bool { + return p.queue.unapplied[id].load(.acquire) > 0; +} + fn closeWatched(p: *Ptys, id: usize) void { p.close(.at(id)); } @@ -401,3 +407,31 @@ test "a post of an older generation is passed over" { try std.testing.expect(std.mem.indexOf(u8, text, "old session") == null); try std.testing.expect(std.mem.indexOf(u8, text, "current session") != null); } + +test "a command's exit is told after its output a reader read, however long that waits in the queue" { + const gpa = std.testing.allocator; + const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + defer core.deinit(); + var queue: Inbox = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; + defer queue.deinit(); + var p = testing(&queue); + defer p.deinit(); + // A command done (reaped), its pty held open by what it left running + // (the pipe's write end): no end of file, and no output in the kernel + // once the reader has read it. + var fds: [2]c_int = undefined; + if (libc.pipe(&fds) != 0) return error.PipeFailed; + defer _ = libc.close(fds[1]); + p.slots[0] = .{ .fd = fds[0], .pid = 0, .cmd = .{ .watched = true, .fd = fds[0], .exited = true, .status = 0 } }; + p.slots[0].?.reader = try queue.startReader(.{ .handle = fds[0], .flags = .{ .nonblocking = false } }, .at(0), p.gens[0]); + try std.testing.expect(host_io.writeFd(fds[1], "LAST")); + const until = std.Io.Clock.awake.now(std.testing.io).toMilliseconds() + 2_000; + while (queue.len == 0 and std.Io.Clock.awake.now(std.testing.io).toMilliseconds() < until) try std.testing.io.sleep(.fromMilliseconds(1), .awake); + try std.testing.expectEqual(@as(usize, 1), queue.len); + p.takeExits(core); + try std.testing.expect(!p.slots[0].?.cmd.told); + var batch = queue.take(); + for (batch.slice()) |msg| p.output(core, msg.output); + p.takeExits(core); + try std.testing.expect(p.slots[0].?.cmd.told); +} diff --git a/dev/host_io.zig b/dev/host_io.zig index 0efb37fd..a22bf5ce 100644 --- a/dev/host_io.zig +++ b/dev/host_io.zig @@ -1105,6 +1105,8 @@ pub fn takeExits(core: *pardes.Pardes, ptys: anytype, ctx: anytype, comptime clo for (ptys, 0..) |*slot, id| { const pt = slotPty(slot) orelse continue; if (!pt.cmd.watched or !pt.cmd.due()) continue; + // Its output read and not yet applied (a host whose reader counts). + if (comptime @hasDecl(@TypeOf(ctx.*), "unapplied")) if (!pt.cmd.eof and ctx.unapplied(id)) continue; pt.cmd.told = true; const eof = pt.cmd.eof; core.update(.{ .exited = .{ .pane = .at(id), .status = pt.cmd.status } }); diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index ecc52df0..86956eb5 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -1463,11 +1463,9 @@ const Shell = struct { // is held for the next wait: `split`). s.takeQueue(); // An exit's wake (a .nop) or output an exit waited behind: told - // here, inside the step, so the frame after it shows it; not while - // reads taken off the queue wait their steps (`takeQueue`): one of - // them may be the command's own last output, which its exit would - // be drawn above (CommandWatch.due cannot see it). - if (s.batch_at == s.batch.len) s.takeExits(); + // here, inside the step, so the frame after it shows it (a read + // still to be applied holds it back: Ptys.unapplied). + s.takeExits(); s.reloadWatched(); } |
