summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--addons/pdf/pdf.zig11
-rw-r--r--src/Kind.zig21
-rw-r--r--src/dump.zig6
-rw-r--r--src/replica.zig37
-rw-r--r--test/behaviour/addons_pdf.zig26
-rw-r--r--test/session.zig14
6 files changed, 101 insertions, 14 deletions
diff --git a/addons/pdf/pdf.zig b/addons/pdf/pdf.zig
index 45f1e22a..d7125de5 100644
--- a/addons/pdf/pdf.zig
+++ b/addons/pdf/pdf.zig
@@ -709,7 +709,9 @@ pub const Pdf = struct {
const In = extern struct { op: Op, cookie: u32 = 0, doc: u64 = 0, page: u32 = 0, tint: Tint = .disabled, req: Request = std.mem.zeroes(Request), band_y: u32 = 0, band_h: u32 = 0, ink: u32 = 0, paper: u32 = 0, keep_clean: u32 = 0, n: u32 = 0 };
/// A job's answer, and after it the sizes (sizes), the text (text), the
/// quads (hits), or each page's count of hits and the rows (search).
- const Out = extern struct { ok: u32 = 0, busy: u32 = 0, w: u32 = 0, h: u32 = 0, band_y: u32 = 0, band_h: u32 = 0, n: u32 = 0, hits: u32 = 0, rgba: ?[*]u8 = null, clean: ?[*]u8 = null };
+ /// (`doc`: the document a render's pixels are of: one closed since,
+ /// a file changed on disk, freed them with it.)
+ const Out = extern struct { ok: u32 = 0, busy: u32 = 0, w: u32 = 0, h: u32 = 0, band_y: u32 = 0, band_h: u32 = 0, n: u32 = 0, hits: u32 = 0, doc: u64 = 0, rgba: ?[*]u8 = null, clean: ?[*]u8 = null };
/// On a worker: the job on a free lane of its document.
pub fn jobRun(bytes: []const u8, gpa: std.mem.Allocator) ?[]u8 {
@@ -780,7 +782,8 @@ pub const Pdf = struct {
const res = if (bytes.len >= @sizeOf(Out)) std.mem.bytesToValue(Out, bytes[0..@sizeOf(Out)]) else Out{};
const extra = bytes[@min(bytes.len, @sizeOf(Out))..];
const len = @as(usize, res.w) * 4 * res.band_h;
- const px: [2][]u8 = .{ if (res.rgba) |p| p[0..len] else &.{}, if (res.clean) |p| p[0..len] else &.{} };
+ const ours = res.doc == d.id;
+ const px: [2][]u8 = .{ if (res.rgba) |p| (if (ours) p[0..len] else &.{}) else &.{}, if (res.clean) |p| (if (ours) p[0..len] else &.{}) else &.{} };
for (px) |p| claim(d, p);
var kept = false; // (the pixels, a page's slot's)
defer if (!kept) for (px) |p| heap.free(p);
@@ -835,7 +838,7 @@ pub const Pdf = struct {
if (s.job.token == token and s.view != 0) break s;
} else return;
s.job.token, s.job.failed = .{ 0, res.ok == 0 and res.busy == 0 };
- if (res.ok == 0) return;
+ if (res.ok == 0 or !ours) return;
for ([_][]const u8{ s.rgba, s.clean, std.mem.sliceAsBytes(s.shown) }) |p| heap.free(p);
const v = st.views[s.view - 1].v;
st.revs += 1;
@@ -888,7 +891,7 @@ fn render(d: *Doc, ld: ?*m.pardes_pdf_document, in: *const Pdf.In, marks: []cons
};
docs_lock.unlock();
if (kept) {
- res.* = .{ .ok = 1, .w = @intCast(L.width), .h = full, .band_y = by, .band_h = bh, .rgba = px.ptr, .clean = if (clean.len > 0) clean.ptr else null };
+ res.* = .{ .ok = 1, .w = @intCast(L.width), .h = full, .band_y = by, .band_h = bh, .doc = d.id, .rgba = px.ptr, .clean = if (clean.len > 0) clean.ptr else null };
return;
}
for ([_][]u8{ px, clean }) |p| {
diff --git a/src/Kind.zig b/src/Kind.zig
index 374a2d81..a3d20f30 100644
--- a/src/Kind.zig
+++ b/src/Kind.zig
@@ -138,7 +138,7 @@ pub const Slot = struct {
// jobs' inputs, its file's hash, a placeholder's record), and what
// a frame makes (the thumb, the pointer, whether one was drawn: a
// view's, never a step's).
- .local = .{ "ops", "st", "gpa", "arena", "plain", "file", "opened", "reloads", "jobs", "tokens", "record", "thumb_at", "thumb_span", "pointer", "painted", "over", "shadow", "used" },
+ .local = .{ "ops", "st", "gpa", "arena", "plain", "file", "opened", "reloads", "jobs", "record", "thumb_at", "thumb_span", "pointer", "painted", "over", "shadow", "used" },
};
/// Its kind's code here; null in a placeholder (`live` false: its kind
/// not in the registry, its record kept for the day it comes) or a
@@ -175,7 +175,9 @@ pub const Slot = struct {
next_tick: u64 = 0,
/// PA_FOCUSED as `tick` was last told.
focused: bool = false,
- /// Its jobs' inputs, until the host takes them (Effect.job).
+ /// Its jobs' inputs, until the host takes them (Effect.job); and the
+ /// last token it gave one, which every core gives alike (dumped: a
+ /// client joined later numbers its next job as the session does).
jobs: [4]?Job = @splat(null),
tokens: u32 = 0,
/// A placeholder's: the kind's record, kept whole.
@@ -979,6 +981,11 @@ const Call = struct {
tape.diverged(why.print("addon {s}: its state after {s} is not the session's (it breaks PA_PLAIN): the session's alone now", .{ cl.slot.nameOf(), cl.name }));
}
cl.slot.state = o.state;
+ // Its last job's token the session's, on a core whose kind asked
+ // none here (a Shadow's).
+ for (o.jobs) |token| if (token != 0) {
+ cl.slot.tokens = token;
+ };
if (cl.p) |p| if (p.panes[cl.id]) |pane| for (o.jobs) |token| if (token != 0) p.emit(.{ .job = .{ .pane = @intCast(cl.id), .serial = pane.serial, .token = token } });
for (o.cancels) |token| if (token != 0) {
for (&cl.slot.jobs) |*j| if (j.*) |job| if (job.token == token) {
@@ -1101,14 +1108,16 @@ pub const api: c.pa_api = .{
cl.began += @import("FrameStats.zig").nowNs() -| t0;
};
const s = cl.slot;
- const free = for (&s.jobs) |*j| {
+ const asked = std.mem.indexOfScalar(u32, &cl.jobs, 0) orelse return 0;
+ // A client's (a plain kind's code here) runs none: its token
+ // alone, as the session's; the answer comes on the tape.
+ const free = if (tape.replaying()) null else for (&s.jobs) |*j| {
if (j.* == null) break j;
} else return 0;
- const asked = std.mem.indexOfScalar(u32, &cl.jobs, 0) orelse return 0;
- const copy = s.gpa.dupe(u8, if (in) |b| @as([*]const u8, @ptrCast(b))[0..n] else "") catch return 0;
+ const copy = if (free != null) s.gpa.dupe(u8, if (in) |b| @as([*]const u8, @ptrCast(b))[0..n] else "") catch return 0 else "";
s.tokens +%= 1;
if (s.tokens == 0) s.tokens = 1;
- free.* = .{ .token = s.tokens, .in = copy };
+ if (free) |slot| slot.* = .{ .token = s.tokens, .in = @constCast(copy) };
// Emitted as the call ends (Call.end), on every core.
cl.jobs[asked] = s.tokens;
return s.tokens;
diff --git a/src/dump.zig b/src/dump.zig
index c5e2953b..742a9574 100644
--- a/src/dump.zig
+++ b/src/dump.zig
@@ -774,6 +774,8 @@ const SlotRecord = struct {
focused: bool = false,
/// The jobs whose answers it dropped (api.cancel): a replica's drop them too.
cancelled: [4]u32 = @splat(0),
+ /// Its last job's token (Kind.Slot.tokens): a client's next is the session's.
+ tokens: u32 = 0,
/// Its tick's prefix: a replica's tag laid out as the session's.
prefix: []const u8 = "",
/// A placeholder's why (its file not here).
@@ -793,7 +795,7 @@ const SlotRecord = struct {
pub fn slotWrite(s: *const Kind.Slot, gpa: std.mem.Allocator, w: *std.Io.Writer) !void {
var place: [32]u8 = undefined;
- var r: SlotRecord = .{ .name = s.nameOf(), .version = s.version, .gen = s.gen, .path = s.path, .art = s.art, .record = s.record, .where = s.where_text.slice(), .values = s.values.slice(), .live = s.live, .next_tick = s.next_tick, .views = s.views, .focused = s.focused, .cancelled = s.cancelled, .prefix = s.prefix.slice(), .refused = s.refused.slice(), .tail = s.tail.slice(), .selection = s.selection, .state = s.state };
+ var r: SlotRecord = .{ .name = s.nameOf(), .version = s.version, .gen = s.gen, .path = s.path, .art = s.art, .record = s.record, .where = s.where_text.slice(), .values = s.values.slice(), .live = s.live, .next_tick = s.next_tick, .views = s.views, .focused = s.focused, .cancelled = s.cancelled, .tokens = s.tokens, .prefix = s.prefix.slice(), .refused = s.refused.slice(), .tail = s.tail.slice(), .selection = s.selection, .state = s.state };
var owned: ?[]u8 = null;
defer if (owned) |o| s.gpa.free(o);
if (s.ops != null) {
@@ -835,7 +837,7 @@ pub fn slotRead(out: *Kind.Slot, gpa: std.mem.Allocator, bytes: []const u8) !voi
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
const r = try walk.load(SlotRecord, arena.allocator(), bytes, null);
- out.* = .{ .gpa = gpa, .arena = .init(gpa), .version = r.version, .gen = r.gen, .art = r.art, .live = r.live, .next_tick = r.next_tick, .views = r.views, .focused = r.focused, .cancelled = r.cancelled, .state = r.state };
+ out.* = .{ .gpa = gpa, .arena = .init(gpa), .version = r.version, .gen = r.gen, .art = r.art, .live = r.live, .next_tick = r.next_tick, .views = r.views, .focused = r.focused, .cancelled = r.cancelled, .tokens = r.tokens, .state = r.state };
errdefer out.free();
@memcpy(out.name[0..@min(15, r.name.len)], r.name[0..@min(15, r.name.len)]);
out.path = try gpa.dupe(u8, r.path);
diff --git a/src/replica.zig b/src/replica.zig
index b1b0514c..d10cdf83 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -4473,6 +4473,43 @@ test "a kind's place moved in a step of another view's: a replica's key that rea
try agrees(a, r);
}
+/// A kind whose every key asks a job, for the job tokens' test.
+const Jobber = struct {
+ const pa = pardes.Kind.c;
+ fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque {
+ return pardes.Kind.api.alloc.?(4);
+ }
+ fn paint(_: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {}
+ fn press(_: ?*anyopaque, _: [*c]const pa.pa_key) callconv(.c) c_int {
+ _ = pardes.Kind.api.job.?(null, 0);
+ return 1;
+ }
+ const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Jobber", .opens = 1, .doc = "a key a job" })};
+ const ops = std.mem.zeroInit(pardes.Kind.Ops, .{
+ .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "jobber".* ++ @as([10]u8, @splat(0)), .version = 1, .exts = "", .words = &words, .nwords = 1 }),
+ .open = open,
+ .paint = paint,
+ .key = press,
+ });
+};
+
+test "a client joined from a dump numbers a kind's next job as the session does" {
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
+ defer a.deinit();
+ var words: [64]pardes.Kind.LoadedWord = undefined;
+ const jobber = pardes.Kind.loadedOf(&Jobber.ops, "/x/jobber.so", @splat(7), &words);
+ _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &jobber } });
+ _ = try a.local(.{ .command = "Jobber" });
+ for (0..3) |_| _ = try a.local(.{ .key = .{ .cp = 'x', .text = "x" } });
+ const id = a.core.view().active;
+ try std.testing.expectEqual(@as(u32, 3), a.core.panes[id].?.addon.?.tokens);
+ const joiner = try joinedNow(a, 2, 1);
+ defer joiner.deinit();
+ try std.testing.expectEqual(@as(u32, 3), joiner.core().panes[id].?.addon.?.tokens);
+ try std.testing.expectEqual(digestOf(a), joiner.digest());
+}
+
test "a replica's word that opens a kind's pane with no file waits for the authority's open: never mispredicted" {
const gpa = std.testing.allocator;
const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
diff --git a/test/behaviour/addons_pdf.zig b/test/behaviour/addons_pdf.zig
index 23768f61..af194a5b 100644
--- a/test/behaviour/addons_pdf.zig
+++ b/test/behaviour/addons_pdf.zig
@@ -256,3 +256,29 @@ test "addons: a PDF whose file changes on disk shows its new pages, under new ke
try std.testing.expect(now.len >= 1);
for (now) |pic| for (first) |old| try std.testing.expect(!std.mem.eql(u8, pic.key, old.key));
}
+
+test "addons: a PDF whose file changes on disk while a page of it renders: the pixels go with the document they are of, once" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ s.graphics();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ const a = s.arena.allocator();
+ const links = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/links.pdf", a, .unlimited);
+ const outline = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/outline.pdf", a, .unlimited);
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "doc.pdf", .data = links });
+ const n = try s.lookFile(s.at("{s}/doc.pdf", .{dir}));
+ try std.testing.expect((try shown(&s, n)).len >= 1);
+ // A page drawn again (another tint), its render done on a worker but
+ // not yet answered as the file changes and the pane opens it again.
+ s.hold_jobs = true;
+ try s.write(s.at("/pane/{d}/ctl", .{n}), "PdfTint full\n");
+ s.runJobs();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "doc.pdf", .data = outline });
+ try std.testing.expect(try s.filesChanged());
+ s.releaseJobs();
+ try s.keys("0");
+ try std.testing.expect((try shown(&s, n)).len >= 1);
+}
diff --git a/test/session.zig b/test/session.zig
index 056966b9..06cd3a32 100644
--- a/test/session.zig
+++ b/test/session.zig
@@ -43,6 +43,8 @@ pub const Session = struct {
/// meanwhile (a reload).
hold_jobs: bool = false,
held: std.ArrayList(*pardes.host_io.KindJob) = .empty,
+ /// Of `held`, those run already (`runJobs`), their answers still held.
+ ran: usize = 0,
/// The files the core asked to watch, watched as a host watches them
/// (file_watch.zig, inotify): `filesChanged` wakes them.
watch_fd: c_int = -1,
@@ -308,14 +310,22 @@ pub const Session = struct {
return true;
}
+ /// The jobs held run now, as a worker runs them, their answers held
+ /// still: what a test does next happens while they are under way.
+ pub fn runJobs(s: *Session) void {
+ for (s.held.items[s.ran..]) |j| j.run();
+ s.ran = s.held.items.len;
+ }
+
/// The jobs held, run now and answered as the host's workers would.
pub fn releaseJobs(s: *Session) void {
s.hold_jobs = false;
- for (s.held.items) |j| {
- j.run();
+ for (s.held.items, 0..) |j, i| {
+ if (i >= s.ran) j.run();
j.land(pardesOf(s));
}
s.held.clearRetainingCapacity();
+ s.ran = 0;
s.drain();
}