summaryrefslogtreecommitdiff
path: root/dev/file_watch.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-20 01:45:46 -0300
committerGabriel Schneider <[email protected]>2026-11-20 02:33:46 -0300
commit3eb6c09bc97c2e60a17e1663108f4b22e3d1308c (patch)
treebe4f3e4b73d299abcefb1abb52f1e6e8f31ba1c1 /dev/file_watch.zig
parent11f55f018a5eb0699d58627a4f2ea39c2b7f5e89 (diff)
downloadpardes-3eb6c09bc97c2e60a17e1663108f4b22e3d1308c.tar.gz
pardes-3eb6c09bc97c2e60a17e1663108f4b22e3d1308c.zip
dev, runtime: a session's file watches once (file_watch.Watches, a working name), the session loop's beside its ptys and queue: the kernel's descriptor, the table (each pane's mark and the theme's slot), the reader that posts `files_changed` to the one queue (the Inbox) and the Host's three file calls (Watches.Calls fills both frontends' write_file, watch_file, watch_theme); the terminal's reader (an Io task posting to vaxis's queue, its files_changed message) and the window's (a thread with a stop pipe, polling a non-blocking descriptor) are deleted for one Io task on a blocking descriptor ended by its cancel, as the ptys' readers are; a Restore lets go of the loop's table, so the frontend call `watched` is gone (ten calls to nine); what a user sees: nothing (the queue keeps one change flag however many reads posted it; a reader that cannot start leaves the session running unwatched, as the terminal's did, where the window's start failed); tui core-test 857/1007 (150 skipped; +1, the device's test), shell-test 1047/1050, term 17/17, pane 183/184, kitty 45/45, behaviour-test and --replay 275/275, process-test 0 of 144 failed (ttytaken once in the run, passed on its retry), soaks --clients 5 and --bytes --clients 5 diverged 0, unequal 0, quiet checks 597/0 673/0 779/0 and 920/0 723/0 755/0, unlisted 0, corpus scene 22 of 22, no leak; gui unit-test 1643/1647 with only the perf check's BaselineHarnessMismatch; daemon.py plain, --far, --games, --window-clients, --upgrade both ways pass; the macOS library (Linux, aarch64-macos) and FreeBSD build (load 9-18)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'dev/file_watch.zig')
-rw-r--r--dev/file_watch.zig122
1 files changed, 122 insertions, 0 deletions
diff --git a/dev/file_watch.zig b/dev/file_watch.zig
index 84f37c81..58229d90 100644
--- a/dev/file_watch.zig
+++ b/dev/file_watch.zig
@@ -664,6 +664,88 @@ pub fn reloadTheme(
return false;
}
+/// A session's file watches: the kernel's descriptor, the table (each
+/// pane's mark and the theme's slot) and the reader that posts a change
+/// to the session's queue (Inbox) as `files_changed`. One a session, the
+/// session loop's (runtime/session.zig): the Host's three file calls are
+/// answered here whatever the frontend (`Calls`), and a frontend taking
+/// `files_changed` off the queue hands it back (`reload`).
+pub const Watches = struct {
+ fd: c_int = -1,
+ table: Table = @splat(null),
+ io: std.Io,
+ gpa: std.mem.Allocator,
+ queue: *Inbox,
+ reader: ?std.Io.Future(void) = null,
+
+ const Inbox = @import("Inbox.zig");
+
+ pub fn open(io: std.Io, gpa: std.mem.Allocator, queue: *Inbox) Watches {
+ return .{ .fd = init(false), .io = io, .gpa = gpa, .queue = queue };
+ }
+
+ /// The reader, from the loop's start on (none: nothing is watched for
+ /// a change, the marks still made).
+ pub fn start(w: *Watches) void {
+ if (w.fd >= 0) w.reader = w.io.concurrent(read, .{ w.io, w.fd, w.queue }) catch null;
+ }
+
+ pub fn deinit(w: *Watches) void {
+ stop(w.fd);
+ if (w.reader) |*r| r.cancel(w.io);
+ w.reader = null;
+ if (w.fd >= 0) _ = libc.close(w.fd);
+ w.fd = -1;
+ }
+
+ /// The marks reconciled after a wake; a second pass asked of the
+ /// queue when one wants it (`reloadPane`).
+ pub fn reload(w: *Watches, core: *pardes.Pardes) void {
+ if (reloadChanged(core, w.io, w.gpa, &w.table)) w.queue.push(.files_changed);
+ }
+
+ /// Blocked in a read of the descriptor (a kqueue's `wait`, where there
+ /// is nothing to read): a change posted, whatever it was (`reload`
+ /// reads every mark); canceled (`deinit`), it ends.
+ fn read(io: std.Io, fd: c_int, queue: *Inbox) void {
+ if (comptime builtin.os.tag != .linux and builtin.os.tag != .freebsd) {
+ while (wait(fd)) queue.push(.files_changed);
+ return;
+ }
+ const file: std.Io.File = .{ .handle = fd, .flags = .{ .nonblocking = false } };
+ var buf: [4096]u8 = undefined;
+ while (true) {
+ const n = file.readStreaming(io, &.{&buf}) catch return;
+ if (n == 0) return;
+ queue.push(.files_changed);
+ }
+ }
+
+ /// The Host's file calls, for a host whose `ctx` is a `*Ctx` with the
+ /// fields `watches: *Watches` and `core: *Pardes`.
+ pub fn Calls(comptime Ctx: type) type {
+ return struct {
+ fn of(ctx: ?*anyopaque) *Ctx {
+ return @ptrCast(@alignCast(ctx.?));
+ }
+ pub fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void {
+ const s = of(ctx);
+ @import("host_io.zig").writeFile(s.core, &s.watches.table, id.index(), path, bytes);
+ }
+ pub fn watchFile(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, on: bool, mode: pardes.WatchMode) void {
+ const s = of(ctx);
+ const w = s.watches;
+ if (applyEffect(s.core, w.io, w.fd, &w.table, id.index(), on, mode)) w.queue.push(.files_changed);
+ }
+ pub fn watchTheme(ctx: ?*anyopaque, generation: u32, on: bool) void {
+ const s = of(ctx);
+ const w = s.watches;
+ if (applyThemeEffect(s.core, w.gpa, w.fd, &w.table, generation, on)) w.queue.push(.files_changed);
+ }
+ };
+ }
+};
+
pub fn identify(io: std.Io, path: []const u8) !Identity {
const native = filesystem.localPath(path) orelse return error.NonLocalPath;
pardes.turn.yield();
@@ -961,3 +1043,43 @@ test "a watched file deleted wakes the watch at once, and its pane says so" {
_ = reloadChanged(core, io, gpa, &watches);
try std.testing.expect(pane.file.?.disk_gone);
}
+
+test "a session's watches: a marked file's change posted to the queue, the reader ended at the end" {
+ if (comptime !supported or !filesystem.platform_has_fs) return;
+ const io = std.testing.io;
+ const gpa = std.testing.allocator;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "before\n" });
+ var path_buf: [256]u8 = undefined;
+ const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/watched", .{tmp.sub_path});
+ const core = try pardes.Pardes.init(gpa, .{ .tty_only = true });
+ defer core.deinit();
+ while (core.nextEffect()) |_| {}
+ const id: u8 = @intCast(core.freeSlot().?);
+ const pane = try pardes.panes.File.open(core, id, path, 0);
+ var queue: Watches.Inbox = .{ .io = io, .gpa = gpa, .lsp_allocator = gpa };
+ defer queue.deinit();
+ var w: Watches = .open(io, gpa, &queue);
+ defer w.deinit();
+ if (w.fd < 0) return error.NoWatcher;
+ defer unwatchAll(w.fd, &w.table);
+ while (core.nextEffect()) |effect| switch (effect) {
+ .watch => |watch| if (watch.pane.index() == id and watch.on) {
+ _ = applyEffect(core, io, w.fd, &w.table, id, watch.on, watch.mode);
+ },
+ else => {},
+ };
+ w.start();
+ try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "after\n" });
+ var batch: Watches.Inbox.Batch = .{};
+ for (0..500) |_| {
+ queue.takeInto(&batch);
+ if (batch.len > 0) break;
+ try io.sleep(.fromMilliseconds(10), .awake);
+ }
+ try std.testing.expectEqual(@as(usize, 1), batch.len);
+ try std.testing.expect(batch.items[0] == .files_changed);
+ w.reload(core);
+ try std.testing.expectEqualStrings("after\n", pane.file.?.content);
+}