summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Handover.zig59
-rw-r--r--src/gui/gui.zig2
2 files changed, 42 insertions, 19 deletions
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(&reg_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.
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 5d3cbafd..9c3a1256 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -4806,7 +4806,7 @@ fn handOver(ctx: ?*anyopaque, seq: u32, bytes: []const u8) void {
std.fmt.bufPrint(&job.geometry, "{d}x{d}", .{ w, h })) catch "";
job.geometry_len = geometry.len;
}
- Handover.start(job) catch |err| {
+ s.core.handover.start(s.io, job) catch |err| {
_ = libc.unlink(path);
s.gpa.free(path);
s.gpa.destroy(job);