summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dev/Inbox.zig20
-rw-r--r--dev/file_watch.zig122
-rw-r--r--dev/gui/gui.zig135
-rw-r--r--dev/tui/tui.zig163
-rw-r--r--runtime/session.zig36
-rw-r--r--test/replicas.zig4
6 files changed, 198 insertions, 282 deletions
diff --git a/dev/Inbox.zig b/dev/Inbox.zig
index 4df02013..aac1f7a1 100644
--- a/dev/Inbox.zig
+++ b/dev/Inbox.zig
@@ -262,6 +262,9 @@ pub fn close(q: *Inbox) void {
}
pub fn deinit(q: *Inbox) void {
+ // The query in flight ends first: what it still delivers is dropped.
+ if (q.lsp_task) |*t| t.future.cancel(q.io);
+ q.lsp_task = null;
q.close();
std.debug.assert(q.waiting == 0);
}
@@ -308,6 +311,23 @@ pub fn startLsp(q: *Inbox, core: *pardes.Pardes, e: Lsp.Request) void {
Lsp.start(q.io, q.lsp_allocator, core, &q.lsp_task, e, q, pushLspRows);
}
+/// A language server's answer or status line taken off the queue, to the
+/// core (whichever frontend took it).
+pub fn landLsp(q: *Inbox, core: *pardes.Pardes, m: Msg) void {
+ switch (m) {
+ .lsp => |l| {
+ core.update(.{ .lsp_resp = .{ .id = l.id, .rows = l.rows } });
+ if (l.rows) |rows| q.lsp_allocator.free(rows);
+ },
+ .lsp_status => |text| {
+ var mbuf: [256]u8 = undefined;
+ core.setStatus(.at(core.view().active), pardes.Messages.Message.stamp(&mbuf, "lsp", text));
+ q.lsp_allocator.free(text);
+ },
+ else => unreachable,
+ }
+}
+
fn pushLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void {
const q: *Inbox = @ptrCast(@alignCast(ctx orelse return));
q.push(.{ .lsp = .{ .id = id, .rows = rows } });
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);
+}
diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig
index 2aaa8bcc..ab33abe9 100644
--- a/dev/gui/gui.zig
+++ b/dev/gui/gui.zig
@@ -382,32 +382,6 @@ fn sdlWake(_: ?*anyopaque) void {
const PipeTasks = selection_pipe.Tasks;
const output_capacity = Queue.output_capacity;
-fn stopPipe() ![2]c_int {
- var fds: [2]c_int = undefined;
- if (libc.pipe(&fds) != 0) return error.PipeFailed;
- errdefer for (fds) |fd| {
- _ = libc.close(fd);
- };
- for (fds) |fd| if (libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1)) < 0) return error.PipeFailed;
- return fds;
-}
-
-fn watchThread(fd: c_int, stop: c_int, q: *Queue) void {
- while (true) {
- var fds = [_]libc.pollfd{
- .{ .fd = stop, .events = libc.POLL.IN, .revents = 0 },
- .{ .fd = fd, .events = libc.POLL.IN, .revents = 0 },
- };
- if (libc.poll(&fds, fds.len, -1) < 0) {
- if (libc.errno(-1) == .INTR) continue;
- return;
- }
- if (fds[0].revents != 0) return;
- if ((fds[1].revents & (libc.POLL.ERR | libc.POLL.HUP | libc.POLL.NVAL)) != 0) return;
- if (fds[1].revents != 0 and file_watch.drain(fd)) q.push(.files_changed);
- }
-}
-
const Gui = struct {
addon_gpu: ?SketchGpu = null,
addon_hash: [pardes.MAX_PANES]u64 = @splat(0),
@@ -1018,10 +992,6 @@ const Own = struct {
test_mode: bool,
detached: bool,
pipe_tasks: PipeTasks = .{},
- inotify_fd: c_int = -1,
- watch_reader: ?std.Thread = null,
- watch_stop: [2]c_int = .{ -1, -1 },
- watches: file_watch.Table = @splat(null),
shell: Shell = undefined,
const Session = session_loop.Loop(Own);
@@ -1046,7 +1016,6 @@ const Own = struct {
observeGuiFont(g, core);
syncTaglineFont(g, core);
l.inbox.wake_fn = sdlWake;
- o.inotify_fd = file_watch.init(true);
o.shell = .{
.core = core,
.gui = g,
@@ -1056,8 +1025,7 @@ const Own = struct {
.ptys = &l.ptys,
.queue = &l.inbox,
.pipe_tasks = &o.pipe_tasks,
- .inotify_fd = o.inotify_fd,
- .watches = &o.watches,
+ .watches = &l.watches,
.fs = l.fs,
.test_mode = o.test_mode,
.test_clock = if (o.test_mode) host_io.testClock() else null,
@@ -1080,33 +1048,15 @@ const Own = struct {
const io = l.io;
o.shell.fs = null;
host_io.setExitWake(null, null);
- pardes.lsp.setStatusSink(null, null);
pardes.Kind.drawer = null;
o.shell.job_tasks.cancelAll(io);
- if (o.inotify_fd >= 0) {
- if (o.watch_reader) |thread| {
- _ = host_io.writeFd(o.watch_stop[1], "x");
- thread.join();
- }
- for (o.watch_stop) |fd| if (fd >= 0) {
- _ = libc.close(fd);
- };
- file_watch.stop(o.inotify_fd);
- _ = libc.close(o.inotify_fd);
- o.inotify_fd = -1;
- }
o.pipe_tasks.cancelAll(io);
- if (l.inbox.lsp_task) |*task| task.future.cancel(io);
// Its worker may be waiting on the queue for room.
l.core.stream.cancelTask();
o.g.hit_core = null;
}
pub fn start(o: *Own, l: *Session) !void {
- if (o.inotify_fd >= 0) {
- o.watch_stop = try stopPipe();
- o.watch_reader = try std.Thread.spawn(.{}, watchThread, .{ o.inotify_fd, o.watch_stop[0], &l.inbox });
- }
if (l.fs) |f| try f.watch(&l.inbox, wakeFs);
_ = c.SDL_StartTextInput(o.g.window);
@@ -1115,7 +1065,6 @@ const Own = struct {
if (o.test_mode) setStdinRaw() catch {};
o.shell.threads_ok = true;
- pardes.lsp.setStatusSink(&l.inbox, Queue.lspStatus);
host_io.setExitWake(&l.inbox, Queue.wakeForExit);
if (o.detached) {
o.shell.closeTheWindow();
@@ -1134,13 +1083,8 @@ const Own = struct {
deliverNote(&o.shell, n);
}
- pub fn watched(o: *Own) session_loop.Watched {
- return .{ .fd = o.inotify_fd, .table = &o.watches };
- }
-
pub fn restoring(o: *Own, nc: *pardes.Pardes) void {
o.pipe_tasks.cancelAll(o.shell.io);
- o.shell.queue.discardCompletions();
// One view, the window's (host_io.restoreCore).
o.shell.gview = 0;
o.g.cells.clearImages();
@@ -1472,7 +1416,7 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_
defer queue.deinit();
var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = undefined };
var pipe_tasks: PipeTasks = .{};
- var watches: file_watch.Table = @splat(null);
+ var no_watches: file_watch.Watches = .{ .io = io, .gpa = gpa, .queue = &queue };
var no_peers: replica.Peers = .{};
var shell: Shell = .{
.core = start.r.core(),
@@ -1483,8 +1427,7 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_
.ptys = &ptys,
.queue = &queue,
.pipe_tasks = &pipe_tasks,
- .inotify_fd = -1,
- .watches = &watches,
+ .watches = &no_watches,
.test_mode = test_mode,
.client = &window,
.peers = &no_peers,
@@ -2410,8 +2353,9 @@ const Shell = struct {
batch: Queue.Batch = .{},
pipe_tasks: *PipeTasks,
job_tasks: PipeTasks = .{},
- inotify_fd: c_int,
- watches: *file_watch.Table,
+ /// The session's file watches (runtime/session.zig's); a client's,
+ /// none.
+ watches: *file_watch.Watches,
threads_ok: bool = false,
test_mode: bool = false,
feed: StdinFeed = .{},
@@ -2588,10 +2532,10 @@ const Shell = struct {
.tty_taken = PtyCalls.ttyTaken,
.fg_name = PtyCalls.fgName,
.kill_job = PtyCalls.killJob,
- .write_file = writeFile,
+ .write_file = WatchCalls.writeFile,
.write_dump = writeDump,
- .watch_file = watchFile,
- .watch_theme = watchTheme,
+ .watch_file = WatchCalls.watchFile,
+ .watch_theme = WatchCalls.watchTheme,
.dump_themes = dumpThemes,
.set_clipboard = setClipboard,
.read_clipboard = readClipboard,
@@ -2605,6 +2549,7 @@ const Shell = struct {
};
const PtyCalls = host_io.Ptys.Calls(Shell);
+ const WatchCalls = file_watch.Watches.Calls(Shell);
/// The command panes' exits, told once their output is in (host_io).
fn takeExits(s: *Shell) void {
@@ -2635,15 +2580,7 @@ const Shell = struct {
for (msgs.slice()) |m| switch (m) {
.output => |o| s.ptys.output(s.core, o),
.eof => |e| s.ptys.eof(s.core, e),
- .lsp => |l| {
- s.core.update(.{ .lsp_resp = .{ .id = l.id, .rows = l.rows } });
- if (l.rows) |rows| s.lsp_allocator.free(rows);
- },
- .lsp_status => |text| {
- var mbuf: [256]u8 = undefined;
- s.core.setStatus(.at(s.core.view().active), message.stamp(&mbuf, "lsp", text));
- s.lsp_allocator.free(text);
- },
+ .lsp, .lsp_status => s.queue.landLsp(s.core, m),
.pipe => |response_value| {
var response = response_value;
s.core.update(.{ .pipe_resp = .{
@@ -2671,8 +2608,7 @@ const Shell = struct {
.fs_ready => {},
.adopted => |a| s.ptys.adoptions.adoptShell(s.core, a.pid, a.fd),
};
- if (check_files and file_watch.reloadChanged(s.core, s.io, s.gpa, s.watches))
- s.queue.push(.files_changed);
+ if (check_files) s.watches.reload(s.core);
}
};
@@ -3192,35 +3128,10 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void {
}
}
-fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void {
- const pane = id.index();
- const s = shellOf(ctx);
- host_io.writeFile(s.core, s.watches, pane, path, bytes);
-}
-
fn writeDump(ctx: ?*anyopaque, bytes: []const u8) void {
host_io.writeDump(shellOf(ctx).core, bytes);
}
-fn watchFile(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, on: bool, mode: pardes.WatchMode) void {
- const pane = id.index();
- const s = shellOf(ctx);
- if (file_watch.applyEffect(s.core, s.io, s.inotify_fd, s.watches, pane, on, mode))
- s.queue.push(.files_changed);
-}
-
-fn watchTheme(ctx: ?*anyopaque, generation: u32, on: bool) void {
- const s = shellOf(ctx);
- if (file_watch.applyThemeEffect(
- s.core,
- s.gpa,
- s.inotify_fd,
- s.watches,
- generation,
- on,
- )) s.queue.push(.files_changed);
-}
-
fn dumpThemes(ctx: ?*anyopaque, id: pardes.Pane.Id) void {
const pane = id.index();
const s = shellOf(ctx);
@@ -6017,25 +5928,6 @@ test "installed Nerd Symbols fallback covers Yazi directory icons" {
try std.testing.expect(atlas.glyphForCodepoint(0xe5ff).glyph != 0);
}
-test "GUI PTY file watcher stops and joins without closing its watched descriptor" {
- if (!file_watch.supported) return error.SkipZigTest;
- const gpa = std.testing.allocator;
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa };
- defer queue.deinit();
- const fd = file_watch.init(true);
- if (fd < 0) return error.WatchInitFailed;
- defer _ = libc.close(fd);
- const stop = try stopPipe();
- defer for (stop) |pipe_fd| {
- _ = libc.close(pipe_fd);
- };
- const thread = try std.Thread.spawn(.{}, watchThread, .{ fd, stop[0], &queue });
- _ = host_io.writeFd(stop[1], "x");
- thread.join();
- try std.testing.expect(libc.fcntl(fd, libc.F.GETFD) >= 0);
- try std.testing.expectEqual(@as(usize, 0), queue.take().len);
-}
-
test "GUI completion failure survives backlog and leaves the document unchanged" {
const gpa = std.testing.allocator;
const core = try pardes.Pardes.init(gpa, .{ .tty_only = true });
@@ -7785,9 +7677,8 @@ test "a detached GUI authority follows terminal directory changes with no window
var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = &rcs, .gens = @splat(1) };
defer ptys.deinit();
var pipes: PipeTasks = .{};
- var watches: file_watch.Table = @splat(null);
var no_peers: replica.Peers = .{};
- var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .ptys = &ptys, .queue = &queue, .pipe_tasks = &pipes, .inotify_fd = -1, .watches = &watches, .peers = &no_peers };
+ var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .ptys = &ptys, .queue = &queue, .pipe_tasks = &pipes, .watches = undefined, .peers = &no_peers };
const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80);
ptys.slots[0] = .{ .fd = child.file.handle, .pid = child.pid };
try std.testing.expect(host_io.writeFd(child.file.handle, "cd child\n"));
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig
index c54447c7..543be3aa 100644
--- a/dev/tui/tui.zig
+++ b/dev/tui/tui.zig
@@ -120,14 +120,11 @@ const Msg = union(enum) {
paste: []const u8,
paste_start,
paste_end,
- lsp_done: struct { id: u32, rows: ?[]u8 },
- lsp_status: []u8,
pipe_done: selection_pipe.Response,
/// A streaming search's rows (pardes.Stream).
search_rows: pardes.Stream.Batch,
/// A kind's job done on a worker (host_io.KindJob).
kind_job: *host_io.KindJob,
- files_changed,
fs_ready,
/// A replica's connection, handed over by the daemon (Peers).
replica: daemon.Joining,
@@ -768,6 +765,7 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
var no_inbox: host_io.Inbox = .{ .io = io, .gpa = gpa, .lsp_allocator = start.allocs.lsp };
defer no_inbox.deinit();
var no_ptys: host_io.Ptys = .{ .queue = &no_inbox, .prompt_rcs = undefined };
+ var no_watches: file_watch.Watches = .{ .io = io, .gpa = gpa, .queue = &no_inbox };
var sh: Shell = .{
.io = io,
.gpa = gpa,
@@ -777,7 +775,7 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
.loop = &loop,
.frame = &frame_arena,
.paste_buf = &paste_buf,
- .inotify_fd = -1,
+ .watches = &no_watches,
.own = start.joined.view,
.screen = &screen,
.client = &c,
@@ -954,7 +952,7 @@ const Own = struct {
.loop = o.loop,
.frame = &o.frame_arena,
.paste_buf = o.paste_buf,
- .inotify_fd = file_watch.init(false),
+ .watches = &l.watches,
.fs = l.fs,
.test_clock = host_io.testClock(),
.peers = &l.peers,
@@ -966,7 +964,6 @@ const Own = struct {
l.inbox.wake_ctx = sh;
l.peers.wake_ctx = o.loop;
l.peers.wake = wakeForReplica;
- pardes.lsp.setStatusSink(sh, lspStatusSink);
host_io.setExitWake(o.loop, wakeForExit);
const host = sh.host();
l.core.host = host;
@@ -988,28 +985,12 @@ const Own = struct {
stopInput(io, &o.screen.thread);
pardes.Kind.drawer = null;
host_io.setExitWake(null, null);
- pardes.lsp.setStatusSink(null, null);
- if (sh.lsp_task) |*t| {
- t.future.cancel(io);
- sh.lsp_task = null;
- }
sh.pipe_tasks.cancelAll(io);
sh.job_tasks.cancelAll(io);
// Its worker may be waiting on the loop's queue for room.
sh.core.stream.cancelTask();
- file_watch.stop(sh.inotify_fd);
- if (sh.watch_task) |*t| {
- t.cancel(io);
- sh.watch_task = null;
- }
- if (sh.inotify_fd >= 0) {
- _ = libc.close(sh.inotify_fd);
- sh.inotify_fd = -1;
- }
while (loop.tryEvent() catch null) |ev| switch (ev) {
.paste => |b| gpa.free(@constCast(b)),
- .lsp_done => |d| if (d.rows) |rows| sh.lsp_gpa.free(rows),
- .lsp_status => |text| sh.lsp_gpa.free(text),
.pipe_done => |response_value| {
var response = response_value;
response.deinit(gpa);
@@ -1038,7 +1019,6 @@ const Own = struct {
if (!screen.on) l.watch();
sh.threads_ok = true;
- if (sh.inotify_fd >= 0) sh.watch_task = io.concurrent(watchFiles, .{ io, sh.inotify_fd, loop }) catch null;
}
/// Detached, the unit is its clients' frame: its output is paced to it
@@ -1057,17 +1037,9 @@ const Own = struct {
deliverNote(o.loop, n);
}
- pub fn watched(o: *Own) session_loop.Watched {
- return .{ .fd = o.sh.inotify_fd, .table = &o.sh.watches };
- }
-
pub fn restoring(o: *Own, nc: *pardes.Pardes) void {
const sh = &o.sh;
const io = sh.io;
- if (sh.lsp_task) |*task| {
- task.future.cancel(io);
- sh.lsp_task = null;
- }
sh.pipe_tasks.cancelAll(io);
// One view, the terminal's (host_io.restoreCore).
const live = o.screen;
@@ -1139,7 +1111,9 @@ const Shell = struct {
own: u8 = 0,
frame: *std.heap.ArenaAllocator,
paste_buf: *std.Io.Writer.Allocating,
- inotify_fd: c_int,
+ /// The session's file watches (runtime/session.zig's); a client's,
+ /// none.
+ watches: *file_watch.Watches,
fs: ?*ninep_io.Listener = null,
/// The session's ptys (runtime/session.zig's); a client's, an empty
/// table over an empty queue.
@@ -1150,9 +1124,6 @@ const Shell = struct {
batch: host_io.Inbox.Batch = .{},
batch_at: usize = 0,
queue_woken: std.atomic.Value(bool) = .init(false),
- watches: file_watch.Table = @splat(null),
- watch_task: ?std.Io.Future(void) = null,
- lsp_task: ?host_io.Lsp.Task = null,
pipe_tasks: PipeTasks = .{},
job_tasks: PipeTasks = .{},
threads_ok: bool = false,
@@ -1248,10 +1219,10 @@ const Shell = struct {
.tty_taken = PtyCalls.ttyTaken,
.fg_name = PtyCalls.fgName,
.kill_job = PtyCalls.killJob,
- .write_file = writeFile,
+ .write_file = WatchCalls.writeFile,
.write_dump = writeDump,
- .watch_file = watchFile,
- .watch_theme = watchTheme,
+ .watch_file = WatchCalls.watchFile,
+ .watch_theme = WatchCalls.watchTheme,
.dump_themes = dumpThemes,
.set_clipboard = setClipboard,
.read_clipboard = readClipboard,
@@ -1317,6 +1288,7 @@ const Shell = struct {
}
const PtyCalls = host_io.Ptys.Calls(Shell);
+ const WatchCalls = file_watch.Watches.Calls(Shell);
fn upgradeAsked(ctx: ?*anyopaque, id: pardes.Pane.Id) void {
const pane = id.index();
@@ -1675,21 +1647,7 @@ const Shell = struct {
if (pasted.len > 0) s.feed(.{ .paste = pasted });
s.paste_buf.clearRetainingCapacity();
},
- .files_changed => s.check_files = true,
.fs_ready => {},
- .lsp_done => |d| {
- core.update(.{ .lsp_resp = .{ .id = d.id, .rows = d.rows } });
- if (d.rows) |rows| s.lsp_gpa.free(rows);
- if (s.lsp_task) |*t| if (t.id == d.id) {
- t.future.await(s.io);
- s.lsp_task = null;
- };
- },
- .lsp_status => |text| {
- var mbuf: [256]u8 = undefined;
- core.setStatus(.at(core.view().active), message.stamp(&mbuf, "lsp", text));
- s.lsp_gpa.free(text);
- },
.pipe_done => |response_value| {
var response = response_value;
core.update(.{ .pipe_resp = .{
@@ -1738,8 +1696,7 @@ const Shell = struct {
fn reloadWatched(s: *Shell) void {
if (!s.check_files) return;
s.check_files = false;
- if (file_watch.reloadChanged(s.core, s.io, s.gpa, &s.watches))
- s.loop.postEvent(.files_changed) catch {};
+ s.watches.reload(s.core);
}
/// The tty's own motions (its glide, a jump's trail) are under way.
@@ -1947,6 +1904,8 @@ const Shell = struct {
return;
},
.eof => |e| s.ptys.eof(s.core, e),
+ .files_changed => s.check_files = true,
+ .lsp, .lsp_status => s.ptys.queue.landLsp(s.core, m),
else => m.deinit(s.gpa, s.lsp_gpa),
}
}
@@ -1960,29 +1919,10 @@ const Shell = struct {
}
}
- fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void {
- const pane = id.index();
- const s = of(ctx);
- host_io.writeFile(s.core, &s.watches, pane, path, bytes);
- }
-
fn writeDump(ctx: ?*anyopaque, bytes: []const u8) void {
host_io.writeDump(of(ctx).core, bytes);
}
- fn watchFile(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, on: bool, mode: pardes.WatchMode) void {
- const pane = id.index();
- const s = of(ctx);
- if (file_watch.applyEffect(s.core, s.io, s.inotify_fd, &s.watches, pane, on, mode))
- s.loop.postEvent(.files_changed) catch {};
- }
-
- fn watchTheme(ctx: ?*anyopaque, generation: u32, on: bool) void {
- const s = of(ctx);
- if (file_watch.applyThemeEffect(s.core, s.gpa, s.inotify_fd, &s.watches, generation, on))
- s.loop.postEvent(.files_changed) catch {};
- }
-
fn dumpThemes(ctx: ?*anyopaque, id: pardes.Pane.Id) void {
const pane = id.index();
const s = of(ctx);
@@ -2010,16 +1950,9 @@ const Shell = struct {
fn lsp(ctx: ?*anyopaque, req: host_io.Lsp.Request) void {
const s = of(ctx);
- if (!s.threads_ok) {
- s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } });
- return s.core.reportError(req.pane, "lsp", error.WorkersUnavailable);
- }
- host_io.Lsp.start(s.io, s.lsp_gpa, s.core, &s.lsp_task, req, s, deliverLspRows);
- }
-
- fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void {
- const s = of(ctx);
- s.loop.postEvent(.{ .lsp_done = .{ .id = id, .rows = rows } }) catch if (rows) |owned| s.lsp_gpa.free(owned);
+ if (s.threads_ok) return s.ptys.queue.startLsp(s.core, req);
+ s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } });
+ s.core.reportError(req.pane, "lsp", error.WorkersUnavailable);
}
fn pipe(ctx: ?*anyopaque, id: u32) void {
@@ -2075,29 +2008,6 @@ const Shell = struct {
}
};
-fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void {
- const s: *Shell = @ptrCast(@alignCast(ctx orelse return));
- const copy = s.lsp_gpa.dupe(u8, text) catch return;
- const posted = s.loop.tryPostEvent(.{ .lsp_status = copy }) catch false;
- if (!posted) s.lsp_gpa.free(copy);
-}
-
-fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) void {
- if (comptime builtin.os.tag != .linux and builtin.os.tag != .freebsd) {
- while (file_watch.wait(fd)) loop.postEvent(.files_changed) catch break;
- return;
- }
- const file: std.Io.File = .{ .handle = fd, .flags = .{ .nonblocking = false } };
- // One buffer, read into directly: a Reader around it only added a second
- // (and in a safe build, filled both as `undefined` on every start).
- var buf: [4096]u8 = undefined;
- while (true) {
- const n = file.readStreaming(io, &.{&buf}) catch break;
- if (n == 0) break;
- loop.postEvent(.files_changed) catch break;
- }
-}
-
fn keepSignal(_: posix.SIG) callconv(.c) void {}
/// A replica client's (`runClient`): a hangup is its end, whoever sent it.
@@ -2775,38 +2685,6 @@ test "on a terminal with no kitty graphics a program's 64x64 image over 8x4 cell
try std.testing.expectEqual(@as(usize, 32), painted);
}
-test "TTY queued results reject an old LSP request ID" {
- const gpa = std.testing.allocator;
- const core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 });
- defer core.deinit();
- var no_peers: replica.Peers = .{};
- var shell: Shell = .{
- .io = std.testing.io,
- .gpa = gpa,
- .lsp_gpa = gpa,
- .core = core,
- .frames = core.frames,
- .loop = undefined,
- .frame = undefined,
- .paste_buf = undefined,
- .inotify_fd = -1,
- .peers = &no_peers,
- .ptys = undefined,
- };
- core.lspRequest(0, .status, "");
- const old_id = core.lsp_ask.wait.?.id;
- core.lspRequest(0, .status, "");
- const current_id = core.lsp_ask.wait.?.id;
- shell.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } };
- _ = shell.apply(.{ .lsp_done = .{ .id = old_id, .rows = try gpa.dupe(u8, "stale result\n") } });
- try std.testing.expect(shell.lsp_task != null);
- try std.testing.expectEqual(current_id, shell.lsp_task.?.id);
- try std.testing.expectEqual(current_id, core.lsp_ask.wait.?.id);
- _ = shell.apply(.{ .lsp_done = .{ .id = current_id, .rows = try gpa.dupe(u8, "") } });
- try std.testing.expect(shell.lsp_task == null);
- try std.testing.expect(core.lsp_ask.wait == null);
-}
-
test "TTY worker setup failures finish matching LSP and pipe requests" {
const gpa = std.testing.allocator;
var failing_vtable = std.testing.io.vtable.*;
@@ -2820,6 +2698,9 @@ test "TTY worker setup failures finish matching LSP and pipe requests" {
.fail_index = if (failure == 0) 0 else std.math.maxInt(usize),
});
var no_peers: replica.Peers = .{};
+ var queue: host_io.Inbox = .{ .io = failing_io, .gpa = gpa, .lsp_allocator = failing.allocator() };
+ defer queue.deinit();
+ var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = undefined };
var shell: Shell = .{
.io = failing_io,
.gpa = failing.allocator(),
@@ -2829,10 +2710,10 @@ test "TTY worker setup failures finish matching LSP and pipe requests" {
.loop = undefined,
.frame = undefined,
.paste_buf = undefined,
- .inotify_fd = -1,
+ .watches = undefined,
.threads_ok = true,
.peers = &no_peers,
- .ptys = undefined,
+ .ptys = &ptys,
};
core.lspRequest(core.view().active, .status, "");
const req: host_io.Lsp.Request = .{
@@ -2855,7 +2736,7 @@ test "TTY worker setup failures finish matching LSP and pipe requests" {
Shell.pipe(&shell, pipe_id);
try std.testing.expect(core.pipe.wait == null);
try std.testing.expectEqualStrings("abc", pane.file.?.content);
- try std.testing.expect(shell.lsp_task == null);
+ try std.testing.expect(queue.lsp_task == null);
try std.testing.expectEqual(@as(usize, 0), shell.pipe_tasks.len);
}
}
diff --git a/runtime/session.zig b/runtime/session.zig
index 4172ba8d..fdd72c2b 100644
--- a/runtime/session.zig
+++ b/runtime/session.zig
@@ -3,11 +3,13 @@
//! the session's allocators and workers, its core (an upgrade's, a dump's
//! or a new one), the prompt files its shells start with, its 9P listener
//! its replica clients (`Peers`), its queue (the workers' and readers'
-//! posts, `Inbox`) and its ptys (`Ptys`): a step, its unit to the clients
-//! and theirs taken in, an Upgrade or a Restore between steps; and the
-//! order its parts end in. Its other devices (the files watched, the LSP's
-//! and the pipes' and addons' jobs, the session's own input) are still its
-//! frontend's (each `Shell`). What a frontend does is:
+//! posts, `Inbox`), its ptys (`Ptys`) and the files it watches
+//! (`file_watch.Watches`): a step, its unit to the clients and theirs
+//! taken in, an Upgrade or a Restore between steps; and the order its
+//! parts end in. Its other devices (the LSP's and the pipes' and addons'
+//! jobs, the session's own input) are still its frontend's (each `Shell`);
+//! the language servers' answers come through its queue (`Inbox.startLsp`).
+//! What a frontend does is:
//!
//! - `options(o)`: before the core: what it gives the core it starts (a
//! window's size, for a dump's layout).
@@ -17,15 +19,14 @@
//! core's first effects (an upgraded terminal's panes its own again,
//! whether anyone looks).
//! - `close(l)`: its devices stopped, after the clients are let go and
-//! before the ptys, the queue and the core go.
-//! - `start(l)`: after the first effects, the ptys read: its input, its
-//! readers, the
-//! listener's wake, a detached session's first wait (`l.watch`).
+//! before the watches, the ptys, the queue and the core go.
+//! - `start(l)`: after the first effects, the ptys and the watches
+//! read: its input, its readers, the listener's wake, a detached
+//! session's first wait (`l.watch`).
//! - `sent()`: after each step's unit went to the clients.
//! - `upgradeAsked()`: an Upgrade asked in the step, done (it execs).
//! - `note(n)`: what the daemon says (an adopted shell, a client's
//! connection, a new daemon's hello taken), onto its queue.
-//! - `watched()`: the files it watches (a Restore stops watching them).
//! - `restoring(nc)`, `restored(l)`: a Restore's new core, before it
//! replaces the old one (its devices let go, what it is told of the
//! frontend) and after (its own view of it again).
@@ -39,9 +40,6 @@ const daemon = @import("daemon.zig");
const upgrade = @import("upgrade.zig");
const ninep_io = @import("9p_io.zig");
-/// The files a frontend watches (host_io.restoreCore lets them go).
-pub const Watched = struct { fd: c_int, table: *file_watch.Table };
-
pub fn Loop(comptime Frontend: type) type {
return struct {
const L = @This();
@@ -58,6 +56,7 @@ pub fn Loop(comptime Frontend: type) type {
/// to take in its wait.
inbox: host_io.Inbox = undefined,
ptys: Ptys = undefined,
+ watches: file_watch.Watches = undefined,
fs: ?*ninep_io.Listener = null,
peers: replica.Peers = .{},
host: pardes.Host = undefined,
@@ -91,8 +90,13 @@ pub fn Loop(comptime Frontend: type) type {
l.peers = .{ .gpa = l.gpa, .io = l.io, .name = daemon.name() };
l.inbox = .{ .io = l.io, .gpa = l.gpa, .lsp_allocator = l.allocs.lsp };
defer l.inbox.deinit();
+ // The language servers' status lines, from the first effects on.
+ pardes.lsp.setStatusSink(&l.inbox, host_io.Inbox.lspStatus);
+ defer pardes.lsp.setStatusSink(null, null);
l.ptys = .{ .queue = &l.inbox, .prompt_rcs = &l.prompt_rcs };
defer l.ptys.deinit();
+ l.watches = .open(l.io, l.gpa, &l.inbox);
+ defer l.watches.deinit();
l.host = try fe.open(&l);
defer fe.close(&l);
@@ -107,6 +111,7 @@ pub fn Loop(comptime Frontend: type) type {
l.effects();
}
l.ptys.start(l.core);
+ l.watches.start();
try fe.start(&l);
while (!l.core.quit) {
@@ -155,9 +160,10 @@ pub fn Loop(comptime Frontend: type) type {
/// let go (host_io.restoreCore); its shells and the frontend's
/// devices with it.
fn restore(l: *L, path: []const u8) void {
- const w = l.fe.watched();
- const nc = host_io.restoreCore(l.gpa, l.core, path, &l.peers, w.fd, w.table) orelse return;
+ const nc = host_io.restoreCore(l.gpa, l.core, path, &l.peers, l.watches.fd, &l.watches.table) orelse return;
l.fe.restoring(nc);
+ // An answer to the old core's query is turned away.
+ l.inbox.discardCompletions();
l.ptys.stop();
nc.host = l.host;
if (l.fs) |f| f.reset(nc);
diff --git a/test/replicas.zig b/test/replicas.zig
index 2211952b..7bf79055 100644
--- a/test/replicas.zig
+++ b/test/replicas.zig
@@ -1121,10 +1121,6 @@ const Soak = struct {
pub fn upgradeAsked(_: *Soak) void {}
pub fn note(_: *Soak, _: anytype) void {}
- /// No Restore is typed here (host_io.restoreCore is the session's own).
- pub fn watched(_: *Soak) session_loop.Watched {
- unreachable;
- }
pub fn restoring(_: *Soak, _: *pardes.Pardes) void {}
pub fn restored(_: *Soak, _: *SessionLoop) void {}