summaryrefslogtreecommitdiff
path: root/core/Term.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Term.zig')
-rw-r--r--core/Term.zig72
1 files changed, 46 insertions, 26 deletions
diff --git a/core/Term.zig b/core/Term.zig
index b82017f6..d1065b49 100644
--- a/core/Term.zig
+++ b/core/Term.zig
@@ -14,11 +14,15 @@ const dump = @import("dump.zig");
const File = @import("File.zig");
const iface = plain.term;
-/// Writes into this process's emulators (a read fed, a grid reflowed, one
-/// opened, closed, loaded or made again): whether a unit's
-/// replay made one, the replica engine's one rule (core/replica.zig
-/// `confirm`) reads across that replay, on its one thread.
-pub var writes: u32 = 0;
+/// The writes of a core's emulators, which live outside its rollback copy
+/// (`Pardes.outside`): a field of the core, so copied with a speculation,
+/// digested and dumped, a function of the tape, the same on every side (one
+/// side's emulator written where the others' are not is a digest that says
+/// `outside`). Every write below takes it and counts, and so does a write a
+/// speculation owes (`resize`): over an input's apply, the count moved is
+/// "this input wrote, or owes, state outside the copy", the one question
+/// core/replica.zig `confirm` asks a unit. A load sets it from the dump.
+pub const Outside = struct { n: u64 = 0 };
/// The reloads this process made (a join's, on every side) and their time,
/// for the soak's join line (test/replicas.zig `--bytes`).
pub var reloads: struct { n: u32 = 0, ns: u64 = 0, worst_ns: u64 = 0 } = .{};
@@ -96,7 +100,6 @@ pub const State = struct {
const parts = try walk.load(iface.Parts, arena.allocator(), bytes[8..][0..n], null);
o.media(media);
out.* = .{ .impl = try o.load(gpa, bytes[8 + n ..], &parts) };
- writes +%= 1;
}
};
@@ -112,13 +115,13 @@ pub fn emulatorOf(s: *State) *@import("term").terminal.State {
/// A new terminal, `cols` by `rows`, allocated with `gpa` (its own from
/// then on). Refused in a core without one: `error.NoTerminal`, which a
/// word reports as `none` says it.
-pub fn open(gpa: std.mem.Allocator, cols: u16, rows: u16) !*State {
+pub fn open(outside: *Outside, gpa: std.mem.Allocator, cols: u16, rows: u16) !*State {
const o = ops orelse return error.NoTerminal;
const s = try gpa.create(State);
errdefer gpa.destroy(s);
o.media(media);
s.* = .{ .impl = try o.open(gpa, cols, rows) };
- writes +%= 1;
+ outside.n +%= 1;
return s;
}
@@ -137,9 +140,9 @@ fn readMedium(read: *const fn (*const anyopaque) iface.MediumError![]u8, ctx: *c
/// It and what it holds, freed, all of it with its own allocator (the
/// one it was opened with): the allocator a caller hands is not used.
-pub fn close(s: *State, _: std.mem.Allocator) void {
+pub fn close(outside: *Outside, s: *State) void {
const own = the().gpa(s.impl);
- writes +%= 1;
+ outside.n +%= 1;
the().close(s.impl);
own.destroy(s);
}
@@ -148,8 +151,8 @@ pub fn close(s: *State, _: std.mem.Allocator) void {
/// every side does it, so that all hold the same pages (ghostty prunes its
/// history a page at a time, and a snapshot does not say how its rows were
/// cut into pages). The snapshot's hash, which every side compares.
-pub fn reload(s: *State) !u64 {
- writes +%= 1;
+pub fn reload(outside: *Outside, s: *State) !u64 {
+ outside.n +%= 1;
const t0 = @import("FrameStats.zig").nowNs();
defer {
reloads.n += 1;
@@ -170,8 +173,8 @@ pub fn due(s: *const State) ?u64 {
/// Loaded from a dump without its shell: its program's modes reset, its
/// history marked off from what the next shell prints.
-pub fn revive(s: *State) void {
- writes +%= 1;
+pub fn revive(outside: *Outside, s: *State) void {
+ outside.n +%= 1;
s.written = true;
the().revive(s.impl);
}
@@ -190,8 +193,8 @@ pub fn stop() void {
/// One read of its program's output (`Told` says where the tracked rows
/// went), every view's place in its history carried through it (`resize`).
-pub fn feed(s: *State, bytes: []const u8, tracked: []const i32, rows: []i32, erased: []bool, places: []?usize) Told {
- writes +%= 1;
+pub fn feed(outside: *Outside, s: *State, bytes: []const u8, tracked: []const i32, rows: []i32, erased: []bool, places: []?usize) Told {
+ outside.n +%= 1;
s.written = true;
return the().feed(s.impl, bytes, tracked, rows, erased, places);
}
@@ -205,8 +208,11 @@ pub fn grid(s: ?*const State) Grid {
/// Its grid reflowed, every view's place in its history (Pane.scrolled_to,
/// each view's) carried through it as ghostty carries a view of its own.
-pub fn resize(s: *State, cols: u16, rows: u16, places: []?usize) void {
- writes +%= 1;
+/// A speculation owes it (counted, not made): its input is replayed when it
+/// is confirmed (core/replica.zig `confirm`), never taken as speculated.
+pub fn resize(outside: *Outside, s: *State, cols: u16, rows: u16, places: []?usize) void {
+ outside.n +%= 1;
+ if (tape.speculating()) return;
s.written = true;
the().resize(s.impl, cols, rows, places);
}
@@ -218,8 +224,10 @@ pub fn cellPixels(s: *State, w: u32, h: u32) void {
/// What it answered its program since the last call, to be written to its
/// pty.
-pub fn replies(s: *State) []const u8 {
- return the().replies(s.impl);
+pub fn replies(outside: *Outside, s: *State) []const u8 {
+ const r = the().replies(s.impl);
+ if (r.len > 0) outside.n +%= 1;
+ return r;
}
// ---- input ----
@@ -277,7 +285,8 @@ pub fn commandOutput(s: ?*State, gpa: std.mem.Allocator, max_rows: usize) !Comma
/// The shell exited in the middle of a command: its output ends at the
/// cursor.
-pub fn endOutputHere(s: *State) void {
+pub fn endOutputHere(outside: *Outside, s: *State) void {
+ outside.n +%= 1;
the().endOutputHere(s.impl);
}
@@ -399,14 +408,14 @@ pub fn hasImages(s: ?*const State) bool {
/// and pardes-image-loop's tables, kept as a test).
pub const calls = [_]struct { []const u8, []const u8, []const u8 }{
// ---- writes: outside the copy ----
- .{ "open", "dump.zig:1 panes.zig:1", "WRITE: a terminal made by a step (a shell's pane) or a load; a copy held across a pane's opening is let go (replica.zig `confirm`: serials)" },
- .{ "close", "panes.zig:1", "WRITE: let go by a step that closes its pane: a copy held across it is let go (retiring panes); a unit that closes one is never kept over a copy" },
+ .{ "open", "dump.zig:1 panes.zig:1", "WRITE: a terminal made by a step (a shell's pane) or a load, counted (`Outside`); a speculation is refused it" },
+ .{ "close", "panes.zig:1", "WRITE: let go by a step that closes its pane, counted; a speculation is refused it" },
.{ "revive", "dump.zig:1", "WRITE: a load's (a join's dump, a Restore): no copy is held" },
.{ "reload", "panes.zig:1", "WRITE: a join's step remakes each terminal written since its last reload, on every side (panes.reloadTerminals, the session's choice taped, the snapshot's hash compared)" },
- .{ "feed", "keys.zig:1 panes.zig:3", "WRITE: the pty's bytes in a unit, every side in the tape's order, every view's place (Pane.scrolled_to, in the copy) carried through it; a unit that feeds lets a copy held ahead go (`Term.writes`), so none is replayed (keys.zig's: a test's)" },
- .{ "resize", "panes.zig:1", "WRITE: a step's reflow, every view's place carried through it; a speculation owes it (`touchTerminal`): its input is taken when confirmed, never as speculated" },
+ .{ "feed", "keys.zig:1 panes.zig:3 replica.zig:1", "WRITE: the pty's bytes in a unit, every side in the tape's order, every view's place (Pane.scrolled_to, in the copy) carried through it, counted: a unit that moved the count is applied once, to the confirmed core (replica.zig `confirm`); a speculation is refused it (keys.zig's, replica.zig's: tests')" },
+ .{ "resize", "panes.zig:1", "WRITE: a step's reflow, every view's place carried through it, counted; a speculation owes it (counted, not made): its input is taken when confirmed, never as speculated" },
.{ "endOutputHere", "ninep/pty.zig:1", "WRITE: a 9P pty/run's step ends a command's output at the cursor: the session's step, replayed as such" },
- .{ "replies", "Core.zig:1", "WRITE: a step drains the reply buffer (in the digest); only the session writes it to the pty (pty_write is off the tape)" },
+ .{ "replies", "Core.zig:1", "WRITE: a step drains the reply buffer (in the digest), counted when it held any (the feed's own unit, which filled it); only the session writes it to the pty (pty_write is off the tape)" },
// ---- reads by a step: in the digest ----
.{ "grid", "Core.zig:2 ansi.zig:3 body_layer.zig:3 draw.zig:2 dump.zig:4 echo.zig:8 exec.zig:2 layout.zig:2 panes.zig:5 replica.zig:2", "READ: the size, the history's length, the cursor: in the digest (a view's place is its pane's; ansi.zig, body_layer.zig, draw.zig, echo.zig: a draw's)" },
.{ "modes", "Core.zig:5 draw.zig:1 dump.zig:2 echo.zig:3 exec.zig:3 mouse.zig:4 ninep/pty.zig:3 panes.zig:2 replica.zig:1", "READ: the program's modes (mouse, focus, bracketed paste, the alternate screen): in the digest (draw.zig, echo.zig: a draw's; panes.zig: a view scrolls no history on the alternate screen)" },
@@ -502,6 +511,17 @@ test "every call of the core's into a terminal is listed with its rule (S1): a n
try std.testing.expectEqual(@as(usize, 0), wrong);
}
+// Every WRITE above takes its core's count first (`Outside`): a call
+// cannot be written without one, so the count cannot be forgotten at a call
+// site (the compiler holds that); that a new op is listed as a WRITE is the
+// test's above and a reviewer's.
+comptime {
+ for (calls) |c| if (std.mem.startsWith(u8, c[2], "WRITE")) {
+ const params = @typeInfo(@TypeOf(@field(@This(), c[0]))).@"fn".param_types;
+ if (params.len == 0 or params[0] != *Outside) @compileError("Term." ++ c[0] ++ " writes an emulator: it takes its core's count (`*Outside`) first");
+ };
+}
+
fn listedCall(name: []const u8, file: []const u8) bool {
for (calls) |c| {
if (!std.mem.eql(u8, c[0], name)) continue;