diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/gui/gui.zig | 54 | ||||
| -rw-r--r-- | src/host_io.zig | 43 |
2 files changed, 34 insertions, 63 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 1bc8b959..edf8347a 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -836,9 +836,6 @@ const Pty = struct { cmd: host_io.CommandWatch = .{}, }; - -const RetiredShell = struct { pid: libc.pid_t = 0, kill_at: i64 = 0 }; - const Queue = host_io.Inbox; /// Wakes a blocking SDL_WaitEventTimeout from another thread: a push to @@ -1000,7 +997,6 @@ test "GUI PTY reader delivers a real shell tail before EOF and joins on close" { shell.ptys = &ptys; shell.gens = &gens; shell.queue = &queue; - shell.retired_shells = @splat(.{}); defer shell.shutdownPtys(); const rcs: host_io.Shell.PromptFiles = .{}; const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80, null); @@ -1052,7 +1048,6 @@ test "GUI PTY Restore joins a real reader waiting for queue space" { shell.ptys = &ptys; shell.gens = &gens; shell.queue = &queue; - shell.retired_shells = @splat(.{}); defer shell.shutdownPtys(); const rcs: host_io.Shell.PromptFiles = .{}; const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80, null); @@ -1081,7 +1076,7 @@ test "GUI PTY Restore joins a real reader waiting for queue space" { try std.testing.expectEqualStrings("fresh", batch.items[0].output.bytes); } -test "GUI PTY deletion joins an idle reader and retains only its owned child for reaping" { +test "GUI PTY deletion joins an idle reader and its shell is reaped by exit" { const gpa = std.testing.allocator; const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); defer core.deinit(); @@ -1095,7 +1090,6 @@ test "GUI PTY deletion joins an idle reader and retains only its owned child for shell.ptys = &ptys; shell.gens = &gens; shell.queue = &queue; - shell.retired_shells = @splat(.{}); defer shell.shutdownPtys(); const rcs: host_io.Shell.PromptFiles = .{}; const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80, null); @@ -1107,7 +1101,6 @@ test "GUI PTY deletion joins an idle reader and retains only its owned child for try std.testing.expectEqual(@as(usize, 0), queue.waiting); try std.testing.expectEqual(@as(usize, 0), queue.take().len); try std.testing.expectEqual(@as(c_int, -1), libc.fcntl(child.file.handle, libc.F.GETFD)); - for (shell.retired_shells) |retired| try std.testing.expect(retired.pid == 0 or retired.pid == child.pid); shell.shutdownPtys(); try std.testing.expectEqual(@as(libc.pid_t, -1), libc.waitpid(child.pid, null, libc.W.NOHANG)); try std.testing.expectEqual(libc.E.CHILD, libc.errno(-1)); @@ -1158,7 +1151,6 @@ test "GUI completion failure survives backlog and leaves the document unchanged" shell.lsp_allocator = gpa; shell.gens = &gens; shell.ptys = &ptys; - shell.retired_shells = @splat(.{}); shell.drainQueue(); try std.testing.expect(core.lsp_wait == null); try std.testing.expectEqualStrings("abc", pane.file.?.content); @@ -3733,7 +3725,6 @@ const Shell = struct { presented: bool = false, skipped_presents: u8 = 0, surface: ?*const pardes.Surface = null, - retired_shells: [pardes.MAX_PANES]RetiredShell = @splat(.{}), /// PARDES_TEST_CLOCK's virtual time (host_io.testClock), in test mode. test_clock: ?u64 = null, /// The display rate a virtual clock stands in for (PARDES_TEST_HZ). @@ -3764,11 +3755,6 @@ const Shell = struct { took_input: bool = false, held: ?c.SDL_Event = null, - fn reap(s: *Shell) void { - const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); - for (&s.retired_shells) |*child| host_io.reapRetired(&child.pid, &child.kill_at, now); - } - fn closePaneShell(ctx: ?*anyopaque, pane: u8) void { shellOf(ctx).closePty(pane); } @@ -3784,41 +3770,21 @@ const Shell = struct { } _ = libc.close(pt.fd); daemon.closed(pt.pid); - var pid = pt.pid; + host_io.retireShell(pt.pid); s.ptys[pane] = null; - const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); - var kill_at: i64 = 0; - host_io.reapRetired(&pid, &kill_at, now); - if (pid == 0) return; - _ = libc.kill(pid, libc.SIG.HUP); - for (&s.retired_shells) |*child| if (child.pid == 0) { - child.* = .{ .pid = pid, .kill_at = now + 100 }; - return; - }; - // No room to wait for it here: a slot kept for it would refuse the - // next shell spawned into this pane until it was reaped. - host_io.retireShell(pid); } fn stopPtys(s: *Shell) void { for (0..s.ptys.len) |pane| s.closePty(@intCast(pane)); for (s.gens) |*gen| gen.* +%= 1; - s.reap(); } fn shutdownPtys(s: *Shell) void { s.stopPtys(); - for (s.retired_shells) |child| if (child.pid > 0) { - _ = libc.kill(child.pid, libc.SIG.KILL); - }; - for (&s.retired_shells) |*child| if (child.pid > 0) { - while (libc.waitpid(child.pid, null, 0) < 0 and libc.errno(-1) == .INTR) {} - child.* = .{}; - }; + host_io.waitRetired(); } fn reconcilePtys(s: *Shell) void { - s.reap(); for (s.ptys, 0..) |slot, pane| if (slot) |pt| { const current = s.core.panes[pane]; if (current == null or current.?.serial != pt.serial or !current.?.isTerminal()) @@ -4251,14 +4217,11 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // shell's queue raises for a terminal's output, a completion, a file // system request. What the shell moves or times itself polls every // frame: a smooth scroll, a gamepad's sticks, the test stdin feed, - // a post chain that moves on its own (ShaderAnimation), - // a retired shell waiting to be killed, a present to retry. A - // minimized window sleeps through the core's animation too: nothing - // shows it, and it is caught up at once when the window is back. - const retiring = for (s.retired_shells) |child| { - if (child.kill_at != 0) break true; - } else false; - const polls = g.scroll_lag != 0 or g.scroll_delta != 0 or g.scroll_notch != 0 or g.glide.moving() or g.gamepad != null or s.test_mode or core.needs_frame or retiring or + // a post chain that moves on its own (ShaderAnimation), a present + // to retry. A minimized window sleeps through the core's animation + // too: nothing shows it, and it is caught up at once when the window + // is back. + const polls = g.scroll_lag != 0 or g.scroll_delta != 0 or g.scroll_notch != 0 or g.glide.moving() or g.gamepad != null or s.test_mode or core.needs_frame or g.post.animating(core.settings.shader_animation); const minimized = !s.test_mode and c.SDL_GetWindowFlags(g.window) & (c.SDL_WINDOW_MINIMIZED | c.SDL_WINDOW_OCCLUDED) != 0; // A latency trace is fed on stdin, whose poll below paces the loop: @@ -4722,7 +4685,6 @@ fn guiNow(ctx: ?*anyopaque) u64 { fn spawnPane(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { const s = shellOf(ctx); - s.reap(); if (s.adoptions.take(pane)) |a| return takeShell(s, pane, a); // The new shell starts before a running one goes: one that cannot // start leaves the running one be (pty/ctl's exec). diff --git a/src/host_io.zig b/src/host_io.zig index 6ee38b6b..df016fd0 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -2689,17 +2689,39 @@ const darwin = struct { /// reaped, killed if it is still there 100 ms on. A thread does the waiting /// so that no host loop needs a timer for it (macOS's has none); a thread /// that cannot start kills and reaps it here. -/// ponytail: a shell still inside its 100 ms when the editor exits is left -/// to init; the gui and detached hosts keep their own retired lists, which -/// this could replace. +/// ponytail: only the window waits for them at exit (waitRetired); the tui +/// and macOS leave a shell still inside its 100 ms to init, a waitRetired at +/// their exit if that matters. pub fn retireShell(pid: libc.pid_t) void { if (pid <= 0) return; _ = libc.kill(pid, libc.SIG.HUP); if (libc.waitpid(pid, null, libc.W.NOHANG) != 0) return; - const thread = std.Thread.spawn(.{}, reapShell, .{ pid, 100 }) catch return reapShell(pid, 0); + _ = retiring.fetchAdd(1, .monotonic); + const thread = std.Thread.spawn(.{}, reapRetiring, .{pid}) catch { + _ = retiring.fetchSub(1, .monotonic); + return reapShell(pid, 0); + }; thread.detach(); } +/// retireShell's threads still waiting on a shell. +var retiring: std.atomic.Value(u32) = .init(0); + +fn reapRetiring(pid: libc.pid_t) void { + defer _ = retiring.fetchSub(1, .release); + reapShell(pid, 100); +} + +/// An exit's sweep: every shell retireShell still waits on is seen out, gone +/// on its hangup or killed at the end of its 100 ms, and reaped, rather than +/// left to init. +pub fn waitRetired() void { + while (retiring.load(.acquire) != 0) { + const ts: libc.timespec = .{ .sec = 0, .nsec = std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } +} + // ---- a command pane's child, watched to its exit ---- // // A command pane's command is done when its process exits, not when its pty @@ -3545,16 +3567,3 @@ pub fn dumpThemes(core: *pardes.Pardes, io: std.Io, gpa: std.mem.Allocator, pane var mbuf: [256]u8 = undefined; core.setMessage(pane, pardes.Messages.Message.stamp(&mbuf, "dumped themes", out_dir)); } - -/// A retired shell reaped once it has exited, killed once its grace is up. -pub fn reapRetired(pid: *std.c.pid_t, kill_at: *i64, now: i64) void { - if (pid.* <= 0) return; - const result = std.c.waitpid(pid.*, null, std.c.W.NOHANG); - if (result > 0 or (result < 0 and std.c.errno(result) == .CHILD)) { - pid.* = 0; - kill_at.* = 0; - } else if (kill_at.* != 0 and now >= kill_at.*) { - _ = std.c.kill(pid.*, std.c.SIG.KILL); - kill_at.* = 0; - } -} |
