summaryrefslogtreecommitdiff
path: root/src/host_io.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host_io.zig')
-rw-r--r--src/host_io.zig43
1 files changed, 26 insertions, 17 deletions
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;
- }
-}