diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-19 20:47:38 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-19 22:47:08 -0300 |
| commit | 91076bdb892fe898a8b07325867a1d29c1d65862 (patch) | |
| tree | 20d7254ebf9809a905f344f7d46442e77c210b73 /dev/tui/tui.zig | |
| parent | 0db34fbf28e70c8c6a3c6025c0888fc5ec75201c (diff) | |
| download | pardes-91076bdb892fe898a8b07325867a1d29c1d65862.tar.gz pardes-91076bdb892fe898a8b07325867a1d29c1d65862.zip | |
dev, runtime: a session's ptys once (dev/Ptys.zig, a working name), the session loop's with the one queue they post to (the Inbox, now the loop's): spawn, take an adopted shell, write, resize, signal, close, the three questions Kill asks and Adopt (Ptys.Calls fills both frontends' Host tables), the readers on the Inbox in both frontends (the terminal's own reader, its two-reads back-pressure and its pty_read/pty_eof messages deleted; it still takes one read a step, the rest kept for the next wait, so a frame shows a command's last line before its exit scrolls it away, as process-test's headersweep watches), end of file and a command's exit, the cwd poll, an Upgrade's pause/record/readOn (a window whose Upgrade failed no longer trips acceptReader's assert), an upgraded session's shells and a Restore's stop (the loop's: the frontend call resumePtys is gone, eleven calls to ten); where the copies differed the window's behaviour stays (the daemon told at a shell's end of file, a reader that cannot start said in its pane, the session's end hangs each shell up and waits out its grace) but the tty's for where a new shell is asked (only one given no directory) and the window's reconcile of ptys against panes is deleted (the core asks every retired terminal pane's pty closed); tui core-test 848/991 (143 skipped), shell-test 1031/1034 (4 Ptys tests, 3 of them moved from gui.zig), term 15/15, pane 183/184, kitty 45/45, behaviour-test and --replay 275/275, the frame corpus 77/77 Debug; on this change's first form (all of a wait's output in one step) the soak 0 diverged, 0 unequal, quiet checks 618/0 835/0 771/0, unlisted 0, corpus scene 22/22, no leak, and gui unit-test 1626/1630 with only the perf check's BaselineHarnessMismatch; the rest of the gate was stopped with the disk under 10 GB (report pardes-services.md)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'dev/tui/tui.zig')
| -rw-r--r-- | dev/tui/tui.zig | 362 |
1 files changed, 89 insertions, 273 deletions
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 1f6b9a49..2c6cf2ee 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -116,8 +116,6 @@ const Msg = union(enum) { /// A SIGHUP: the session's own terminal hung up. hangup, key_press: vaxis.Key, - pty_read: struct { id: usize, gen: u32, bytes: []u8 }, - pty_eof: struct { id: usize, gen: u32 }, winsize: vaxis.Winsize, mouse: vaxis.Mouse, focus_in, @@ -379,15 +377,6 @@ const TestReader = struct { const PipeTasks = selection_pipe.Tasks; -const Pty = struct { - file: std.Io.File, - pid: posix.pid_t, - reader: std.Io.Future(error{OutOfMemory}!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 = .{}, -}; - /// A watched child exited: wake the loop, which takes it (`takeExits`). fn wakeForExit(ctx: ?*anyopaque) void { const loop: *Loop = @ptrCast(@alignCast(ctx.?)); @@ -776,15 +765,18 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, var tty: Tty = undefined; var c = try Client.init(&tty, &start); defer c.link.deinit(gpa); - // A client has no clients of its own. + // A client has no clients of its own, and spawns nothing: its pty + // table an empty one over an empty queue. var no_peers: replica.Peers = .{}; + var no_inbox: host_io.Inbox = .{ .io = io, .gpa = gpa, .lsp_allocator = start.allocs.lsp }; + defer no_inbox.deinit(); + var no_ptys: host_io.Ptys = .{ .queue = &no_inbox, .prompt_rcs = undefined }; var sh: Shell = .{ .io = io, .gpa = gpa, .lsp_gpa = start.allocs.lsp, .core = start.r.core(), .frames = &start.frames, - .prompt_rcs = undefined, .loop = &loop, .frame = &frame_arena, .paste_buf = &paste_buf, @@ -793,6 +785,7 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, .screen = &screen, .client = &c, .peers = &no_peers, + .ptys = &no_ptys, }; tty = .{ .sh = &sh, .screen = &screen, .loop = &loop, .joins = &start }; @@ -961,7 +954,6 @@ const Own = struct { .lsp_gpa = l.allocs.lsp, .core = l.core, .frames = &l.frames, - .prompt_rcs = &l.prompt_rcs, .loop = o.loop, .frame = &o.frame_arena, .paste_buf = o.paste_buf, @@ -969,9 +961,12 @@ const Own = struct { .fs = l.fs, .test_clock = host_io.testClock(), .peers = &l.peers, + .ptys = &l.ptys, }; const sh = &o.sh; sh.screen = o.screen; + l.inbox.wake_fn = Shell.wakeForQueue; + l.inbox.wake_ctx = sh; l.peers.wake_ctx = o.loop; l.peers.wake = wakeForReplica; pardes.lsp.setStatusSink(sh, lspStatusSink); @@ -997,12 +992,6 @@ const Own = struct { pardes.Kind.drawer = null; host_io.setExitWake(null, null); pardes.lsp.setStatusSink(null, null); - for (&sh.ptys) |*slot| if (slot.*) |*pt| { - pt.reader.cancel(io) catch {}; - _ = libc.close(pt.file.handle); - slot.* = null; - }; - for (sh.adoptions.list[0..sh.adoptions.len]) |a| _ = libc.close(a.fd); if (sh.lsp_task) |*t| { t.future.cancel(io); sh.lsp_task = null; @@ -1021,10 +1010,6 @@ const Own = struct { sh.inotify_fd = -1; } while (loop.tryEvent() catch null) |ev| switch (ev) { - .pty_read => |pr| { - tookRead(io, pr.id); - gpa.free(pr.bytes); - }, .paste => |b| gpa.free(@constCast(b)), .lsp_done => |d| if (d.rows) |rows| sh.lsp_gpa.free(rows), .lsp_status => |text| sh.lsp_gpa.free(text), @@ -1046,10 +1031,6 @@ const Own = struct { o.frame_arena.deinit(); } - pub fn resumePtys(o: *Own, rec: *const upgrade.Record) void { - o.sh.resumePtys(rec); - } - pub fn start(o: *Own, l: *Session) !void { const io = l.io; const sh = &o.sh; @@ -1064,9 +1045,6 @@ const Own = struct { if (!screen.on) l.watch(); sh.threads_ok = true; - for (&sh.ptys, 0..) |*slot, id| if (slot.*) |*pt| { - pt.reader = try io.concurrent(readPty, .{ io, l.gpa, pt.file, id, sh.gens[id], loop }); - }; if (sh.inotify_fd >= 0) sh.watch_task = io.concurrent(watchFiles, .{ io, sh.inotify_fd, loop }) catch null; } @@ -1098,8 +1076,6 @@ const Own = struct { sh.lsp_task = null; } sh.pipe_tasks.cancelAll(io); - for (&sh.gens) |*generation| generation.* +%= 1; - for (0..pardes.MAX_PANES) |pane| Shell.closePty(sh, .at(pane)); // One view, the terminal's (host_io.restoreCore). const live = o.screen; stopInput(io, &live.thread); @@ -1167,7 +1143,6 @@ const Shell = struct { /// Its runner's frame timings (Pardes.frames): written as it draws and /// takes input, never through the core. frames: *pardes.FrameStats, - prompt_rcs: *const host_io.Shell.PromptFiles, loop: *Loop, /// The terminal (none in tests), and the view it shows: a second /// terminal on the session is a replica client (`pardes --attach`). @@ -1176,12 +1151,16 @@ const Shell = struct { frame: *std.heap.ArenaAllocator, paste_buf: *std.Io.Writer.Allocating, inotify_fd: c_int, - /// Shells a dead session left (Adopt), each waiting for the spawn of - /// the terminal pane made for it. - adoptions: host_io.Adoptions = .{}, fs: ?*ninep_io.Listener = null, - ptys: [pardes.MAX_PANES]?Pty = @splat(null), - gens: [pardes.MAX_PANES]u32 = @splat(0), + /// The session's ptys (runtime/session.zig's); a client's, an empty + /// table over an empty queue. + ptys: *host_io.Ptys, + /// What `takeQueue` takes off the ptys' queue, and whether the loop + /// was woken for it since it last did (one wake, not one a read ahead + /// of the terminal's keys in the loop's queue). + batch: host_io.Inbox.Batch = .{}, + batch_at: usize = 0, + queue_woken: std.atomic.Value(bool) = .init(false), watches: file_watch.Table = @splat(null), watch_task: ?std.Io.Future(void) = null, lsp_task: ?host_io.Lsp.Task = null, @@ -1207,8 +1186,6 @@ const Shell = struct { focused_ns: u64 = 0, /// Upgrade was asked from this pane, to be done between steps (`upgradeNow`). upgrade_asked: ?u8 = null, - /// Its pty readers stopped for it: their end of file is not a shell's. - upgrading: bool = false, /// The session's replica clients (core/replica.zig), on sockets: the /// session loop's (runtime/session.zig); a client's, none. peers: *replica.Peers, @@ -1272,17 +1249,17 @@ const Shell = struct { .poll_frame = pollFrame, .watched = watched, .detach = detach, - .adopt = adopt, + .adopt = PtyCalls.adopt, .upgrade = upgradeAsked, .newer_build = newerBuild, - .spawn = spawn, - .pty_write = ptyWrite, - .pty_resize = ptyResize, - .pty_signal = ptySignal, - .close_pty = closePty, - .tty_taken = ttyTaken, - .fg_name = fgName, - .kill_job = killJob, + .spawn = PtyCalls.spawn, + .pty_write = PtyCalls.write, + .pty_resize = PtyCalls.resize, + .pty_signal = PtyCalls.signal, + .close_pty = PtyCalls.close, + .tty_taken = PtyCalls.ttyTaken, + .fg_name = PtyCalls.fgName, + .kill_job = PtyCalls.killJob, .write_file = writeFile, .write_dump = writeDump, .watch_file = watchFile, @@ -1386,11 +1363,7 @@ const Shell = struct { if (stub >= 0) daemon.tellStub(stub, "r{s}", .{why}); } - fn adopt(ctx: ?*anyopaque, id: pardes.Pane.Id) void { - const pane = id.index(); - const s = of(ctx); - s.adoptions.ask(s.core, pane); - } + const PtyCalls = host_io.Ptys.Calls(Shell); /// A client's own build is not the session's to say (its core's host /// is the session's, through the tape). @@ -1425,18 +1398,19 @@ const Shell = struct { .ok => {}, } // Nothing more read from a pty or a terminal, and what was read - // goes in; a reader's cancel posts an end of file, passed over. - s.upgrading = true; - defer s.upgrading = false; - for (&s.ptys) |*slot| if (slot.*) |*pt| pt.reader.cancel(s.io) catch {}; + // goes in: taken, the readers stopped (one waiting for room has it + // then), and what they posted meanwhile taken. + s.takeAll(); + s.ptys.pause(); if (s.screen) |sc| stopInput(s.io, &sc.thread); while (s.loop.tryEvent() catch null) |ev| _ = s.apply(ev); + s.takeAll(); core.settle(); // Its replica clients told, their views gone before the dump: each // dials the session again once it is back. s.peers.refuseAll(core, "the session is upgrading to another build of pardes"); var rec: upgrade.Record = .{ .link = daemon.link, .stub = daemon.stub, .exe = path }; - for (s.ptys, 0..) |slot, id| if (slot) |pt| rec.addPty(id, pt.pid, pt.file.handle); + s.ptys.record(&rec); if (s.screen) |sc| if (sc.on) rec.addTty(.{ .view = s.own, .fd = sc.tty.fd.handle, @@ -1460,9 +1434,7 @@ const Shell = struct { break :failed upgrade.execAs(s.gpa, path, &rec, &at_buf, s.fs, &why); }; // As it was: its ptys read on, its terminals' keys as they were. - for (&s.ptys, 0..) |*slot, id| if (slot.*) |*pt| { - pt.reader = s.io.concurrent(readPty, .{ s.io, s.gpa, pt.file, id, s.gens[id], s.loop }) catch pt.reader; - }; + s.ptys.readOn(core); if (s.screen) |sc| if (sc.on) { if (popped) sc.vx.enableDetectedFeatures(sc.tty.writer()) catch {}; startInput(s.loop, sc) catch {}; @@ -1470,20 +1442,6 @@ const Shell = struct { core.reportFailure(pane, failed); } - /// An upgraded session's shells, running on: each its pane's pty again, - /// or, when its dump would not load (`fresh`), adopted as new panes. - fn resumePtys(s: *Shell, rec: *const upgrade.Record) void { - for (rec.ptys[0..rec.nptys]) |k| { - const pane = if (rec.fresh) null else s.core.panes[k.pane]; - if (pane == null or !pane.?.isTerminal()) { - s.adoptions.adoptShell(s.core, k.pid, k.fd); - continue; - } - const file: std.Io.File = .{ .handle = k.fd, .flags = .{ .nonblocking = false } }; - s.ptys[k.pane] = .{ .file = file, .pid = k.pid, .reader = .{ .any_future = null, .result = {} }, .cmd = .{ .watched = pane.?.command != null and host_io.watchExit(k.pid), .fd = k.fd } }; - } - } - /// An upgraded session's terminal, its view's again (`primary`, run's, /// entered already); every other view but a replica client's, kept for /// its rejoin (views.away), closes. A terminal another build kept for a @@ -1621,6 +1579,10 @@ const Shell = struct { batch += 1; } if (motion) |m| _ = s.apply(m); + // The ptys' output, after the terminal's input of this wait (which + // is never queued behind it; with clients, an input after output + // 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. s.takeExits(); @@ -1635,10 +1597,6 @@ const Shell = struct { fn split(s: *Shell, ev: Msg) bool { if (!s.peers.recording) return false; switch (ev) { - .pty_read => { - s.took_output = true; - return false; - }, .key_press => if (s.in_paste) return false, .mouse, .paste, .paste_end, .winsize, .focus_in, .focus_out => {}, else => return false, @@ -1709,7 +1667,7 @@ const Shell = struct { return true; }, .attach => |a| s.attachTerminal(a.tty, a.stub, null), - .adopted => |a| s.adoptions.adoptShell(s.core, a.pid, a.fd), + .adopted => |a| s.ptys.adoptions.adoptShell(s.core, a.pid, a.fd), .replica => |j| { s.peers.handed(j.fd, j.view); s.input_seen = true; @@ -1745,38 +1703,6 @@ const Shell = struct { s.feed(.{ .focus = false }); }, .winsize => |ws| if (!s.detached()) s.resized(s.screen, ws), - .pty_read => |pr| { - tookRead(s.io, pr.id); - s.output_seen = true; - if (pr.id == core.view().active) s.focused_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); - if (s.gens[pr.id] == pr.gen) - core.update(.{ .output = .{ .pane = .at(pr.id), .bytes = pr.bytes } }); - s.gpa.free(pr.bytes); - // An exit waiting behind this output may be told now. - s.takeExits(); - return true; - }, - .pty_eof => |e| if (s.gens[e.id] == e.gen and !s.upgrading) { - if (s.ptys[e.id]) |*pt| { - pt.reader.await(s.io) catch {}; // reader just finished; join it or its future leaks - // A command's pty stays open until its child has exited: - // closing it would hang up one that runs on without it. - // Its exit, if it came first, is told now its output is in. - if (pt.cmd.watched) { - host_io.commandEof(core, &s.ptys, e.id, s, closeWatched); - return true; - } - _ = libc.close(pt.file.handle); - // Unwatched, a command's exit is read here, as its end, - // and a shell's, for a run waiting on it and the log. - const command = if (core.panes[e.id]) |pn| pn.command != null else false; - const status = host_io.exitStatus(pt.pid, 100); - if (status == null) host_io.retireShell(pt.pid); - s.ptys[e.id] = null; - if (command or status != null) core.update(.{ .exited = .{ .pane = .at(e.id), .status = status } }); - } - core.update(.{ .eof = .{ .pane = .at(e.id) } }); - }, .key_press => |key| if (s.in_paste) { const bytes = vaxis_input.pasteBytes(key); const room = max_paste_bytes -| s.paste_buf.written().len; @@ -1885,10 +1811,7 @@ const Shell = struct { if (s.fs) |f| if (f.tick().pending) { _ = s.loop.tryPostEvent(.fs_ready) catch {}; }; - for (&s.ptys, 0..) |*slot, id| if (slot.*) |pt| { - var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.ttyCwd(pt.pid, pt.file.handle, s.core.settings.shell.effective.get(), &lbuf)) |cwd| s.core.setCwd(.at(id), cwd); - }; + s.ptys.pollCwds(s.core); const sc = s.term() orelse return; s.core.view().frontend.native_image_patches = sc.feed.shm.load(.acquire); @@ -2030,120 +1953,59 @@ const Shell = struct { tracy.frameMark(); } - fn spawn(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8) void { - const pane = id.index(); - const s = of(ctx); - if (s.adoptions.take(id)) |a| return s.takeShell(pane, a); - // The new shell starts before a running one goes: one that cannot - // start leaves the running one be (pty/ctl's exec). - const running = s.ptys[pane] != null; - const child = host_io.forkShell(s.core, pane, s.prompt_rcs, s.core.shellBin(), cwd, s.core.view().screen_h, s.core.view().screen_w) catch |err| { - if (running) return s.core.restartFailed(id, err); - return s.core.shellFailed(id, err); - }; - closePty(ctx, id); // a shell still in the slot goes, reaped - s.gens[pane] +%= 1; - const command = if (s.core.panes[pane]) |pn| pn.command != null else false; - s.ptys[pane] = .{ .file = child.file, .pid = child.pid, .reader = .{ .any_future = null, .result = {} }, .cmd = .{ .watched = command and host_io.watchExit(child.pid), .fd = child.file.handle } }; - var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - // Only a shell given no directory is asked where it is: one given a - // directory is named by it already (newShell), and asked now, before - // its chdir, it would answer this process's, a rename to undo. - if (cwd.len == 0) if (host_io.shellCwd(child.pid, &lbuf)) |wd| s.core.setCwd(id, wd); - if (s.threads_ok) { - if (s.ptys[pane]) |*pt| { - pt.reader = s.io.concurrent(readPty, .{ s.io, s.gpa, pt.file, @as(usize, pane), s.gens[pane], s.loop }) catch pt.reader; - } - } - } - - /// An adopted shell, running already, as pane `pane`'s: told the size - /// twice (a size it never had, then the pane's), which makes it redraw. - fn takeShell(s: *Shell, pane: u8, a: host_io.Adoption) void { - closePty(s, .at(pane)); - s.gens[pane] +%= 1; - const file: std.Io.File = .{ .handle = a.fd, .flags = .{ .nonblocking = false } }; - s.ptys[pane] = .{ .file = file, .pid = a.pid, .reader = .{ .any_future = null, .result = {} }, .cmd = .{} }; - var exe_buf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - s.core.acknowledgeShell(.at(pane), host_io.processExe(a.pid, &exe_buf) orelse "sh", host_io.marksPrompts(a.pid)); - var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.shellCwd(a.pid, &lbuf)) |wd| s.core.setCwd(.at(pane), wd); - const tiny: posix.winsize = .{ .row = 1, .col = 1, .xpixel = 0, .ypixel = 0 }; - _ = posix.system.ioctl(a.fd, TIOCSWINSZ, @intFromPtr(&tiny)); - if (s.core.panes[pane]) |pn| ptyResize(s, .at(pane), @max(pn.cols, 1), @max(pn.rows, 1)); - if (s.threads_ok) if (s.ptys[pane]) |*pt| { - pt.reader = s.io.concurrent(readPty, .{ s.io, s.gpa, pt.file, @as(usize, pane), s.gens[pane], s.loop }) catch pt.reader; - }; + /// The command panes' exits, told once their output is in (host_io). + fn takeExits(s: *Shell) void { + s.ptys.takeExits(s.core); } - fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { - const pane = id.index(); + /// A post to the session's queue (Inbox.wake, any thread): the loop + /// woken, once until it takes them. + fn wakeForQueue(ctx: ?*anyopaque) void { const s = of(ctx); - if (s.ptys[pane]) |pt| _ = host_io.writeFd(pt.file.handle, bytes); + if (s.queue_woken.swap(true, .acq_rel)) return; + // A full queue is taken soon anyway, and the post with it. + if (!(s.loop.tryPostEvent(.nop) catch false)) s.queue_woken.store(false, .release); } - fn ptyResize(ctx: ?*anyopaque, id: pardes.Pane.Id, cols: u16, rows: u16) void { - const pane = id.index(); - const s = of(ctx); - if (s.ptys[pane]) |pt| { - const ws = host_io.ptyWinsize(s.core, cols, rows); - _ = posix.system.ioctl(pt.file.handle, TIOCSWINSZ, @intFromPtr(&ws)); + /// What the session's queue holds: its ptys' output (`waitInput` + /// paces it; the focused pane's is its echo) and their ends of file. + /// A read a step, as the terminal's own queue gave them: the frame + /// after one shows it before what came after it (a command's last line + /// before its exit scrolls it away), the rest kept for the next wait. + fn takeQueue(s: *Shell) void { + while (true) { + if (s.batch_at == s.batch.len) { + s.queue_woken.store(false, .release); + s.ptys.queue.takeInto(&s.batch); + s.batch_at = 0; + if (s.batch.len == 0) return; + } + const m = s.batch.items[s.batch_at]; + s.batch_at += 1; + switch (m) { + .output => |o| { + s.output_seen = true; + s.mail_only = false; + if (s.peers.recording) s.took_output = true; + if (o.pane.index() == s.core.view().active) s.focused_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + s.ptys.output(s.core, o); + // The next wait takes the rest (a wake its own: the + // one a post made may be taken already). + _ = s.loop.tryPostEvent(.nop) catch {}; + return; + }, + .eof => |e| s.ptys.eof(s.core, e), + else => m.deinit(s.gpa, s.lsp_gpa), + } } } - fn ptySignal(ctx: ?*anyopaque, id: pardes.Pane.Id, sig: pardes.PtySignal) void { - const pane = id.index(); - const s = of(ctx); - // A command whose exit is recorded is reaped: its group may be - // another's by now. - if (s.ptys[pane]) |pt| if (!pt.cmd.exited) host_io.signalTty(pt.pid, pt.file.handle, sig, pt.cmd.watched); - } - - /// The pane is gone: hang its pty up, which the kernel passes on to the - /// shell as SIGHUP, and retire the shell. Output still in flight - /// carries the old generation and is dropped. - fn closePty(ctx: ?*anyopaque, id: pardes.Pane.Id) void { - const pane = id.index(); - const s = of(ctx); - var pt = s.ptys[pane] orelse return; - s.ptys[pane] = null; - s.gens[pane] +%= 1; - pt.reader.cancel(s.io) catch {}; - _ = libc.close(pt.file.handle); - daemon.closed(pt.pid); - host_io.retireShell(pt.pid); - } - - fn ttyTaken(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { - const pane = id.index(); - const s = of(ctx); - const pt = s.ptys[pane] orelse return false; - return host_io.ttyTaken(pt.pid, pt.file.handle); - } - - fn fgName(ctx: ?*anyopaque, id: pardes.Pane.Id, buf: []u8) ?[]const u8 { - const pane = id.index(); - const s = of(ctx); - const pt = s.ptys[pane] orelse return null; - return host_io.foregroundName(pt.pid, pt.file.handle, buf); - } - - /// The command panes' exits, told once their output is in (host_io). - fn takeExits(s: *@This()) void { - host_io.takeExits(s.core, &s.ptys, s, closeWatched); - } - - fn closeWatched(s: *@This(), id: usize) void { - const pt = &(s.ptys[id] orelse return); - _ = libc.close(pt.file.handle); - s.ptys[id] = null; - } - - fn killJob(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { - const pane = id.index(); - const s = of(ctx); - const pt = s.ptys[pane] orelse return false; - return host_io.killJob(pt.pid, pt.file.handle); + /// Everything queued taken (an Upgrade's). + fn takeAll(s: *Shell) void { + while (true) { + s.takeQueue(); + if (s.batch.len == 0) return; + } } fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void { @@ -2355,44 +2217,6 @@ fn wakeFs(ctx: ?*anyopaque) void { _ = loop.tryPostEvent(.fs_ready) catch {}; } -/// Each pane's reads posted and not yet taken in. Two at most: a flood the -/// loop cannot keep up with waits in its pty, not in the queue ahead of a -/// key and its echo (hundreds of 4 KB reads queued were half a second). -var reads_queued: [pardes.MAX_PANES]std.atomic.Value(u32) = @splat(.init(0)); - -/// A read of pane `id` taken in (or dropped): its reader may read on. Never -/// below zero, for an event no reader posted (a test's). -fn tookRead(io: std.Io, id: usize) void { - var queued = reads_queued[id].load(.acquire); - while (queued > 0) queued = reads_queued[id].cmpxchgWeak(queued, queued - 1, .acq_rel, .acquire) orelse { - if (queued == 2) io.futexWake(u32, &reads_queued[id].raw, 1); - return; - }; -} - -fn readPty(io: std.Io, gpa: std.mem.Allocator, pty: std.Io.File, id: usize, gen: u32, loop: *Loop) error{OutOfMemory}!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; - read: while (true) { - while (true) { - const queued = reads_queued[id].load(.acquire); - if (queued < 2) break; - io.futexWait(u32, &reads_queued[id].raw, queued) catch break :read; - } - const n = pty.readStreaming(io, &.{&buf}) catch break; - if (n == 0) break; - const bytes = try gpa.dupe(u8, buf[0..n]); - _ = reads_queued[id].fetchAdd(1, .acq_rel); - loop.postEvent(.{ .pty_read = .{ .id = id, .gen = gen, .bytes = bytes } }) catch { - tookRead(io, id); - gpa.free(bytes); - break; - }; - } - _ = loop.tryPostEvent(.{ .pty_eof = .{ .id = id, .gen = gen } }) catch {}; -} - const max_paste_bytes: usize = pardes.memory.limits.max_paste_bytes; fn mouseEvent(m: vaxis.Mouse) ?pardes.Event { @@ -2999,7 +2823,7 @@ test "on a terminal with no kitty graphics a program's 64x64 image over 8x4 cell try std.testing.expectEqual(@as(usize, 32), painted); } -test "TTY queued results reject old PTY generations and LSP request IDs" { +test "TTY queued results reject an old LSP request ID" { const gpa = std.testing.allocator; const core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); defer core.deinit(); @@ -3010,21 +2834,13 @@ test "TTY queued results reject old PTY generations and LSP request IDs" { .lsp_gpa = gpa, .core = core, .frames = core.frames, - .prompt_rcs = undefined, .loop = undefined, .frame = undefined, .paste_buf = undefined, .inotify_fd = -1, .peers = &no_peers, + .ptys = undefined, }; - shell.gens[0] = 2; - _ = shell.apply(.{ .pty_read = .{ .id = 0, .gen = 1, .bytes = try gpa.dupe(u8, "old session\n") } }); - _ = shell.apply(.{ .pty_read = .{ .id = 0, .gen = 2, .bytes = try gpa.dupe(u8, "current session\n") } }); - const text = try pardes.Term.scrollback(core.panes[0].?.terminal, gpa); - defer gpa.free(text); - try std.testing.expect(std.mem.indexOf(u8, text, "old session") == null); - try std.testing.expect(std.mem.indexOf(u8, text, "current session") != null); - core.lspRequest(0, .status, ""); const old_id = core.lsp_ask.wait.?.id; core.lspRequest(0, .status, ""); @@ -3058,13 +2874,13 @@ test "TTY worker setup failures finish matching LSP and pipe requests" { .lsp_gpa = failing.allocator(), .core = core, .frames = core.frames, - .prompt_rcs = undefined, .loop = undefined, .frame = undefined, .paste_buf = undefined, .inotify_fd = -1, .threads_ok = true, .peers = &no_peers, + .ptys = undefined, }; core.lspRequest(core.view().active, .status, ""); const req: host_io.Lsp.Request = .{ |
