diff options
Diffstat (limited to 'dev/Ptys.zig')
| -rw-r--r-- | dev/Ptys.zig | 34 |
1 files changed, 34 insertions, 0 deletions
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); +} |
