diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Turn.zig | 78 | ||||
| -rw-r--r-- | core/root.zig | 1 |
2 files changed, 79 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(); +} 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. |
