diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-22 23:20:38 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-22 23:20:38 -0300 |
| commit | e6dd9c5c3a60e3609e99ab2076b5a2509a014a66 (patch) | |
| tree | 59e10d535fbb5ddf1a971c391e0e6b0981a6d409 /core/Turn.zig | |
| parent | 6270f531cd89130a26ee3d5423a740db707f8888 (diff) | |
| parent | 9e5b54cfdf73728745d2aeaafac00d823114543e (diff) | |
| download | pardes-e6dd9c5c3a60e3609e99ab2076b5a2509a014a66.tar.gz pardes-e6dd9c5c3a60e3609e99ab2076b5a2509a014a66.zip | |
integration (a base for the next rounds, not yet the gathered line): the pair's round 12 on the viewport's line with a client's way back: pardes-m60-base (`pyquvrsv`: pardes-viewport + pardes-upgrade-back) and pardes-pair-12 (`mlutslkp` on `ntlpyokw` on `zsquynqy` on `wowqsyxt`, on pardes-raw-tty: a 9P request that changes the core waits for the pty output read before it (`Turn.awaitOutput`: pagercolor's cause on both paths); a pair's client takes the tests' clock; the pair rows name the side that went; the soak with a pair among its sides); no conflict; core-test 918/1078 (160 skipped), shell-test 1120/1123 (2026-11-22)
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/Turn.zig')
| -rw-r--r-- | core/Turn.zig | 78 |
1 files changed, 78 insertions, 0 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(); +} |
