summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-22 21:06:02 -0300
committerGabriel Schneider <[email protected]>2026-11-22 21:20:44 -0300
commit1017496cc9c7e95a39b93928653e7ff338069152 (patch)
tree47a771929d6d12bb76b9b4211590d85eab0f0376
parent34e2d82a322bd3767a8ec62b3a4c0894399e8c04 (diff)
downloadpardes-1017496cc9c7e95a39b93928653e7ff338069152.tar.gz
pardes-1017496cc9c7e95a39b93928653e7ff338069152.zip
9P: a request that changes the core waits for the output already read out of the ptys before it, as a terminal shows a program's output before what it led to: `cmd | pardes -` typed in a pane writes its pager over 9P after the shell echoed the line and started the command, and the session, which feeds its queue a read a step (dev/tui/tui.zig `takeQueue`), could still hold that echo when the write was served at its next rest (runtime/9p_io.zig `onServe`); the pager's layout then gave the shell's pane its rows as the stale emulator had its cursor (one short), and the emulator's resize cleared the command line as a prompt the shell would draw again (ghostty's Screen.clearPromptForRedraw: the cursor still after `133;B`, the `133;C` not yet fed), which bash, running the command, never did; process-test's `pagercolor` failed so as a pair 7 of 20 pinned to one CPU (a client's keys reach the shell a step each, so its echo comes a key a read: 55 reads queued in a failing run, the write served with seven still to feed) and on the own path 1 of 20 (its keys a burst a step: a read or two); now core/Turn.zig counts the reads (`reads`, by each pty reader as it reads, dev/Inbox.zig `readPty`, beside `unapplied`) and those applied or dropped (`applied`, `outputApplied` from `Inbox.applied`), and `awaitOutput` waits, the turn given up, until every read made before the request is applied, only while the editor waits in its loop (`listening`, set by the new `listen` in the tui's `waitOnce` and the window's `waitInput`, cleared by `wake`): resting in a call of its own (a save through its own mount, core/fs.zig `writeOut`) it applies nothing and the request goes first as before; `onServe` waits so for every request that `needsQuiet` and asks `quiet` after; a user could hit it typing or pasting `cmd | pardes -` in a pane of a session with a client attached, or under load on the own path (the pager open, the command line wiped from the shell's screen); pagercolor pinned to CPU 5 after: as a pair 20 of 20 first time, own path 20 of 20 (before: 13 of 20, 19 of 20); a core test of the rule (`Turn` now in core's test imports); core-test 918/1075 (157 skipped), shell-test 1116/1119 (3 skipped)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
-rw-r--r--core/Turn.zig78
-rw-r--r--core/root.zig1
-rw-r--r--dev/Inbox.zig8
-rw-r--r--dev/gui/gui.zig2
-rw-r--r--dev/tui/tui.zig2
-rw-r--r--runtime/9p_io.zig7
6 files changed, 93 insertions, 5 deletions
diff --git a/core/Turn.zig b/core/Turn.zig
index 249c6795..d0e49d44 100644
--- a/core/Turn.zig
+++ b/core/Turn.zig
@@ -43,6 +43,15 @@ restores: u64 = 0,
/// Bumped each time a language server's answer (or its lack) is taken
/// in (`lspResponse`): a write that asked one waits for this.
lsp_answers: u64 = 0,
+/// Reads out of the ptys (a pane's reader, as it reads) and those applied
+/// or dropped since (`outputApplied`): a write waits for the output read
+/// before it (`awaitOutput`).
+reads: std.atomic.Value(u64) = .init(0),
+applied: std.atomic.Value(u64) = .init(0),
+/// The editor gave the turn up to wait for input (`listen`), not for a
+/// call of its own (`rest`, `yield`: a save through its own mount, whose
+/// write it would never answer applying output meanwhile).
+listening: bool = false,
/// All of the above, and `out` reaching zero.
settled: std.Io.Condition = .init,
/// Every `yield`, ever: a replica's step makes none (each syscall of its
@@ -69,12 +78,22 @@ pub fn rest(t: *Turn) void {
t.release();
}
+/// The editor is about to wait for input, the output its readers read
+/// among it (its loop's wait): `rest`, and a write may wait for that
+/// output meanwhile (`awaitOutput`).
+pub fn listen(t: *Turn) void {
+ if (t.io == null) return;
+ t.listening = true;
+ t.release();
+}
+
/// The editor takes the turn back -- once every step that is out in a
/// syscall has returned, since a step of the editor's may change what
/// those still point into.
pub fn wake(t: *Turn) void {
const io = t.io orelse return;
t.mutex.lockUncancelable(io);
+ t.listening = false;
while (t.out != 0) t.settled.waitUncancelable(io, &t.mutex);
}
@@ -123,6 +142,32 @@ pub fn bump(t: *Turn, counter: *u64) void {
t.settled.broadcast(io);
}
+/// A pty's read applied, or dropped: a write waiting on it told.
+pub fn outputApplied(t: *Turn) void {
+ _ = t.applied.fetchAdd(1, .acq_rel);
+ if (t.io) |io| t.settled.broadcast(io);
+}
+
+/// With the turn: waits, turn given up meanwhile, until every read out of
+/// a pty before now is applied, as a terminal shows a program's output
+/// before what it led to: `cmd | pardes -` typed in a pane writes its
+/// pager after the shell echoed the line and started the command, and
+/// the editor, a read a step, could still hold that echo (a client's keys
+/// come a step each, so a shell's echo comes a key a read): the pager's
+/// layout then saw the shell's line not yet ended, and its emulator's
+/// resize cleared that line as a prompt the shell would draw again
+/// (ghostty's Screen.clearPromptForRedraw). Only while the editor waits
+/// in its loop (`listening`): resting in a call of its own it applies
+/// nothing, and the request goes first, as before. A read its reader
+/// drops off the turn wakes nobody: seen at the next recheck.
+pub fn awaitOutput(t: *Turn) void {
+ const mark = t.reads.load(.acquire);
+ while (t.listening and t.applied.load(.acquire) < mark) t.settled.waitTimeout(t.io.?, &t.mutex, .{ .duration = .{ .raw = .fromMilliseconds(25), .clock = .awake } }) catch |err| switch (err) {
+ error.Timeout => {},
+ error.Canceled => return,
+ };
+}
+
/// With the turn: waits, turn given up meanwhile, until `counter` has
/// moved past `seen`; stopping ends the wait early.
pub fn awaitPast(t: *Turn, counter: *const u64, seen: u64) void {
@@ -139,3 +184,36 @@ fn release(t: *Turn) void {
t.mutex.unlock(t.io.?);
if (woken) if (t.wake_parked) |f| f(t.wake_ctx);
}
+
+test "a write waits for the output read before it while the editor listens, not while it rests in a call of its own" {
+ const io = std.testing.io;
+ var t: Turn = .{};
+ t.start(io);
+ _ = t.reads.fetchAdd(2, .acq_rel);
+ // A save through its own mount: the editor rests in it, applies
+ // nothing, and the write it waits on is served at once.
+ t.rest();
+ _ = t.take();
+ t.awaitOutput();
+ try std.testing.expectEqual(@as(u64, 0), t.applied.load(.acquire));
+ t.give();
+ // Its loop's wait: the write waits for both reads, applied meanwhile.
+ t.wake();
+ t.listen();
+ const editor = try std.Thread.spawn(.{}, struct {
+ fn apply(turn: *Turn) void {
+ for (0..2) |_| {
+ turn.wake();
+ turn.outputApplied();
+ turn.listen();
+ }
+ }
+ }.apply, .{&t});
+ _ = t.take();
+ t.awaitOutput();
+ try std.testing.expectEqual(@as(u64, 2), t.applied.load(.acquire));
+ t.give();
+ editor.join();
+ t.wake();
+ t.stop();
+}
diff --git a/core/root.zig b/core/root.zig
index 3bca5e03..a476f90f 100644
--- a/core/root.zig
+++ b/core/root.zig
@@ -443,6 +443,7 @@ test {
_ = @import("Handover.zig");
_ = @import("walk.zig");
_ = @import("views.zig");
+ _ = Turn;
// A window's build links the window's C (font.c over FreeType and
// HarfBuzz) into each of its programs, and their allocator (ui_malloc
// and the rest) is gui.zig's.
diff --git a/dev/Inbox.zig b/dev/Inbox.zig
index d733b971..1df516cb 100644
--- a/dev/Inbox.zig
+++ b/dev/Inbox.zig
@@ -118,7 +118,9 @@ pub fn acceptReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void {
/// One of `pane`'s reads applied, or dropped unapplied.
pub fn applied(q: *Inbox, pane: pardes.Pane.Id) void {
const n = &q.unapplied[pane.index()];
- if (n.load(.acquire) > 0) _ = n.fetchSub(1, .acq_rel);
+ if (n.load(.acquire) == 0) return;
+ _ = n.fetchSub(1, .acq_rel);
+ pardes.turn.outputApplied();
}
pub fn cancelReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void {
@@ -316,8 +318,10 @@ fn readPty(q: *Inbox, pty: std.Io.File, pane: pardes.Pane.Id, gen: u32) void {
},
};
if (n == 0) continue;
- // Counted at once: out of the pty, the poll no longer sees it.
+ // Counted at once: out of the pty, the poll no longer sees it,
+ // nor a write after it (Turn.awaitOutput).
_ = q.unapplied[pane.index()].fetchAdd(1, .acq_rel);
+ _ = pardes.turn.reads.fetchAdd(1, .acq_rel);
const bytes = q.gpa.dupe(u8, buf[0..n]) catch |err| {
q.applied(pane);
failure = err;
diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig
index 3193d966..b00073fa 100644
--- a/dev/gui/gui.zig
+++ b/dev/gui/gui.zig
@@ -2696,7 +2696,7 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void {
s.held = null;
if (held) |h| sev = h;
// The wait is the 9P connections' turn with the core.
- pardes.turn.rest();
+ pardes.turn.listen();
const got = held != null or c.SDL_WaitEventTimeout(&sev, ms);
pardes.turn.wake();
// A virtual clock: while a render-time animation moves, each loop
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig
index 5d75722f..ec5a3309 100644
--- a/dev/tui/tui.zig
+++ b/dev/tui/tui.zig
@@ -1494,7 +1494,7 @@ const Shell = struct {
s.wake_to = s.core.nextWake() orelse if (s.moving()) s.core.clock.now_ns + pardes.frame_ns else 0;
requestWake(s.io, wait);
}
- pardes.turn.rest();
+ pardes.turn.listen();
const got = s.loop.nextEvent() catch {
pardes.turn.wake();
if (s.client) |c| c.link.quit = true;
diff --git a/runtime/9p_io.zig b/runtime/9p_io.zig
index 1079b102..50011690 100644
--- a/runtime/9p_io.zig
+++ b/runtime/9p_io.zig
@@ -183,8 +183,13 @@ pub const Listener = struct {
const refused = pardes.ctlfs.Reply.fail(req.tag, pardes.ctlfs.E.IO);
return conn.reply(&refused, "");
}
- const quiet = pardes.turn.take();
+ _ = pardes.turn.take();
defer pardes.turn.give();
+ // What changes the core comes after the output already read out of
+ // the ptys (a pane's `cmd | pardes -`: its pager after the line's
+ // echo); quiet asked after that wait, which gave the turn up.
+ if (pardes.ctlfs.needsQuiet(l.core, req)) pardes.turn.awaitOutput();
+ const quiet = pardes.turn.out == 0;
// A connection a Restore is cutting (`reset`) is the old editor's
// client: its fids name the old editor's panes and opens.
if (l.cutting(conn)) {