From f8bf3331ce99d98b25b0dfcc877db9c1dd06f77b Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 21:08:13 -0300 Subject: merge: the handover carrier on io.concurrent: Handover.start runs carry on the shell's Io (the double fork and exec inside it as they were), Handover.task keeps the future, finish awaits it once the answer is in, deinit cancels it; its waits are Io sleeps, so a cancel stops it there, leaving a coming-up window its dump --- src/Handover.zig | 59 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 18 deletions(-) (limited to 'src/Handover.zig') diff --git a/src/Handover.zig b/src/Handover.zig index ced9baf0..6124e49f 100644 --- a/src/Handover.zig +++ b/src/Handover.zig @@ -12,8 +12,9 @@ //! is (`adopt`) before the write is answered. //! //! The core only dumps, and closes or keeps when told (`finish`); the host -//! writes the dump, and its worker (`carry`, on a thread of its own) spawns -//! the window and waits for it, posting how it went into host_io.Inbox. +//! writes the dump, and its worker (`carry`, an io.concurrent job on the +//! shell's Io, as every background job is) spawns the window and waits for +//! it, posting how it went into host_io.Inbox. const Handover = @This(); const std = @import("std"); @@ -39,8 +40,20 @@ serials: [MAX_PANES]u32 = @splat(0), revisions: [MAX_PANES]u32 = @splat(0), /// The dump the host sends, gpa-owned while the handover flies. bytes: ?[]u8 = null, +/// The carrier (`carry`) while it flies, on the shell's Io: awaited when +/// its answer comes in (`finish`), cancelled at the session's end. +task: ?struct { future: std.Io.Future(void), io: std.Io } = null, pub fn deinit(h: *Handover, gpa: std.mem.Allocator) void { + // The session ends with the carrier still out: it stops at its next + // wait (a 9P call it is in finishes first, within its own budget). + if (h.task) |t| { + h.task = null; + var future = t.future; + pardes.turn.yield(); + future.cancel(t.io); + pardes.turn.back(); + } if (h.bytes) |b| gpa.free(b); h.bytes = null; } @@ -103,6 +116,14 @@ pub fn begin(h: *Handover, p: *Pardes, carried: dump.Carried, at: usize) void { pub fn finish(h: *Handover, p: *Pardes, seq: u32, to: []const u8, failure: ?[]const u8) void { if (!h.flying or seq != h.seq) return; h.flying = false; + // Its answer is the carrier's last word: what is left of it is freeing. + if (h.task) |t| { + h.task = null; + var future = t.future; + pardes.turn.yield(); + future.await(t.io); + pardes.turn.back(); + } h.deinit(p.gpa); var buf: [320]u8 = undefined; if (failure) |why| { @@ -266,16 +287,19 @@ pub fn dumpPath(gpa: std.mem.Allocator, seq: u32) ![:0]u8 { return std.fmt.allocPrintSentinel(gpa, "{s}/pardes-handover-{d}-{d}.zon", .{ dir, libc.getpid(), seq }, 0); } -/// Runs the handover on its own thread, detached; it removes the dump's -/// file once someone has it, or nobody will. -pub fn start(job: *Job) !void { - const thread = try std.Thread.spawn(.{}, carry, .{job}); - thread.detach(); +/// Runs the handover's carrier on `io` (io.concurrent), kept in `h.task` +/// until its answer: it owns `job`, and removes the dump's file once +/// someone has it, or nobody will. +pub fn start(h: *Handover, io: std.Io, job: *Job) !void { + h.task = .{ .future = try io.concurrent(carry, .{ io, job }), .io = io }; } -fn carry(job: *Job) void { +fn carry(io: std.Io, job: *Job) void { + // Cancelled while a new window is coming up: it may still be reading + // the dump, so the file stays for it. + var for_child = false; defer { - _ = libc.unlink(job.path); + if (!for_child) _ = libc.unlink(job.path); job.gpa.free(job.path); job.gpa.destroy(job); } @@ -284,7 +308,8 @@ fn carry(job: *Job) void { // is, before its `adopt` answers. A refusal (no room there, too many // panes) leaves the pane here: no new window instead, the user aimed. var dial_buf: [512]u8 = undefined; - if (windowUnderPointer(job, &dial_buf)) |dial| { + const under = windowUnderPointer(io, job, &dial_buf) catch return; + if (under) |dial| { var line_buf: [600]u8 = undefined; const line = std.fmt.bufPrint(&line_buf, "adopt {s}\n", .{job.path}) catch return job.post(job.post_ctx, job.seq, false, "its dump's name is too long"); const name = std.fs.path.basename(dial); @@ -308,7 +333,10 @@ fn carry(job: *Job) void { job.gpa.free(status); return job.post(job.post_ctx, job.seq, true, std.fmt.bufPrint(&words, "a new window (pardes/{d})", .{pid}) catch "a new window"); } else |_| {} - sleepMs(50); + io.sleep(.fromMilliseconds(50), .awake) catch { + for_child = true; + return; + }; } _ = libc.kill(pid, libc.SIG.TERM); job.post(job.post_ctx, job.seq, false, "the new window did not come up in 15 s"); @@ -322,14 +350,14 @@ extern "c" fn readdir(dir: *libc.DIR) ?*libc.dirent; /// window let go over learns it a moment after the release (the grab ends, /// then it is entered), so it is asked a few times. None answering with a /// pointer line at all (no other window, or an older pardes) ends it at once. -fn windowUnderPointer(job: *Job, out: *[512]u8) ?[]const u8 { +fn windowUnderPointer(io: std.Io, job: *Job, out: *[512]u8) std.Io.Cancelable!?[]const u8 { const run = std.mem.span(libc.getenv("XDG_RUNTIME_DIR") orelse return null); var reg_buf: [512:0]u8 = undefined; const reg = std.fmt.bufPrintSentinel(®_buf, "{s}/9p/pardes", .{run}, 0) catch return null; const own = job.own[0..job.own_len]; var round: u32 = 0; while (round < 10) : (round += 1) { - if (round > 0) sleepMs(40); + if (round > 0) try io.sleep(.fromMilliseconds(40), .awake); const dir = libc.opendir(reg) orelse return null; defer _ = libc.closedir(dir); var windows: usize = 0; @@ -351,11 +379,6 @@ fn windowUnderPointer(job: *Job, out: *[512]u8) ?[]const u8 { return null; } -fn sleepMs(ms: u32) void { - var ts: libc.timespec = .{ .sec = 0, .nsec = @as(isize, ms) * std.time.ns_per_ms }; - _ = libc.nanosleep(&ts, null); -} - /// This program again, `-l` the dump: a new window, in a session of its own /// (setsid) and not this one's child (forked twice), so it outlives this /// window and leaves no zombie. Answers its pid. -- cgit v1.3