summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-23 17:11:12 -0300
committerGabriel Schneider <[email protected]>2026-10-23 17:11:12 -0300
commit19c83c90ebed4817acd7a12d4d225f5af0957bc1 (patch)
treee6ae660d2768063eb0c3ff0548c23077927de520 /src
parentef0de36bf473f22d37cc885054bf4eed98bf275f (diff)
downloadpardes-19c83c90ebed4817acd7a12d4d225f5af0957bc1.tar.gz
pardes-19c83c90ebed4817acd7a12d4d225f5af0957bc1.zip
One inbox for every host: the GUI's queue moves to host_io.Inbox, macOS and the detached server use it
The inbox takes a wake callback instead of an SDL flag and owns starting a pty reader and an LSP query and delivering their answers, status and pipe results. macOS gains the GUI's rule that only a pane's current reader and the current query get in; its reap now cancels the reader on the inbox before its future, since a reader blocked on a full queue cannot be canceled. The detached server's completion mailbox goes; its socket stays the loop's wakeup. Two old macOS ABI test compile errors fixed, so its unit tests run on Linux. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/detached/client.zig2
-rw-r--r--src/detached/server.zig203
-rw-r--r--src/gui/gui.zig382
-rw-r--r--src/host_io.zig318
-rw-r--r--src/macos.zig425
5 files changed, 507 insertions, 823 deletions
diff --git a/src/detached/client.zig b/src/detached/client.zig
index cd17871e..617ae342 100644
--- a/src/detached/client.zig
+++ b/src/detached/client.zig
@@ -886,7 +886,7 @@ test "detached Restore keeps attached frontends and follows queued frames with a
try testing.expect(h.session.clients[c.slot].attached);
try testing.expectEqualStrings("saved body\n", h.core.panes[0].?.file.?.content);
try testing.expect(h.session.clients[c.slot].need_full);
- var wake = [_]libc.pollfd{.{ .fd = h.session.mailbox.wake[0], .events = poll_in, .revents = 0 }};
+ var wake = [_]libc.pollfd{.{ .fd = h.session.wake[0], .events = poll_in, .revents = 0 }};
try testing.expectEqual(@as(c_int, 1), libc.poll(&wake, wake.len, 0));
for (0..Harness.rounds) |_| {
if ((try h.pumpUntil(&c, .frame)).frame.kind == .full) break;
diff --git a/src/detached/server.zig b/src/detached/server.zig
index 62dff011..124a4c5a 100644
--- a/src/detached/server.zig
+++ b/src/detached/server.zig
@@ -79,68 +79,13 @@ const Pty = struct {
cmd: host_io.CommandWatch = .{},
};
-/// A watched child exited: wake the loop, which takes it (`takeExits`).
+/// A watched child exited, or a worker finished: wake the loop.
fn wakeForExit(ctx: ?*anyopaque) void {
- const box: *Mailbox = @ptrCast(@alignCast(ctx.?));
- box.signal();
+ Session.signal(@ptrCast(@alignCast(ctx.?)));
}
const RetiredShell = struct { pid: posix.pid_t = 0, kill_at: i64 = 0 };
-const Completion = union(enum) {
- lsp: struct { id: u32, rows: ?[]u8 },
- pipe: selection_pipe.Response,
-
- fn deinit(completion: *Completion, gpa: std.mem.Allocator) void {
- switch (completion.*) {
- .lsp => |result| if (result.rows) |rows| gpa.free(rows),
- .pipe => |*result| result.deinit(gpa),
- }
- }
-};
-
-const Mailbox = struct {
- const capacity = selection_pipe.Tasks.capacity + 1;
- const Batch = struct {
- items: [capacity]Completion = undefined,
- len: usize = 0,
- status: ?[]u8 = null,
- };
-
- /// Taken by the workers and the loop; `io` is the session's (initAsync).
- mutex: std.Io.Mutex = .init,
- io: std.Io = undefined,
- batch: Batch = .{},
- wake: [2]c_int = .{ -1, -1 },
-
- fn post(box: *Mailbox, completion: Completion) void {
- box.mutex.lockUncancelable(box.io);
- // Tasks retain their slot until the owner consumes their one completion.
- std.debug.assert(box.batch.len < capacity);
- box.batch.items[box.batch.len] = completion;
- box.batch.len += 1;
- box.mutex.unlock(box.io);
- box.signal();
- }
-
- fn signal(box: *Mailbox) void {
- while (libc.send(box.wake[1], "w", 1, nosignal) < 0) {
- if (libc.errno(-1) != .INTR) break;
- }
- }
-
- fn take(box: *Mailbox) Batch {
- var bytes: [128]u8 = undefined;
- while (libc.recv(box.wake[0], &bytes, bytes.len, 0) > 0) {}
- box.mutex.lockUncancelable(box.io);
- defer box.mutex.unlock(box.io);
- const batch = box.batch;
- box.batch.len = 0;
- box.batch.status = null;
- return batch;
- }
-};
-
const Source = union(enum) {
listener,
completion,
@@ -167,8 +112,10 @@ pub const Session = struct {
scratch: std.ArrayListUnmanaged(u8) = .empty,
ptys: [pardes.MAX_PANES]Pty = @splat(.{}),
retired_shells: [pardes.MAX_PANES]RetiredShell = @splat(.{}),
- mailbox: Mailbox = .{},
- lsp_task: ?host_io.Lsp.Task = null,
+ /// The workers' answers (initAsync sets it up); `wake` is what wakes
+ /// the loop's poll for them, and for a watched child's exit.
+ inbox: host_io.Inbox = undefined,
+ wake: [2]c_int = .{ -1, -1 },
pipe_tasks: selection_pipe.Tasks = .{},
prompt_rcs: host_io.Shell.PromptFiles = .{},
inotify_fd: c_int = -1,
@@ -186,7 +133,6 @@ pub const Session = struct {
accept_paused_ms: i64 = 0,
pub fn initAsync(s: *Session) !void {
- s.mailbox.io = s.io;
var pair: [2]c_int = undefined;
if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM, 0, &pair) != 0) return error.SocketFailed;
errdefer for (pair) |fd| {
@@ -201,31 +147,41 @@ pub const Session = struct {
return error.SocketOptionFailed;
}
}
- s.mailbox.wake = pair;
- pardes.lsp.setStatusSink(s, lspStatus);
- host_io.setExitWake(&s.mailbox, wakeForExit);
+ s.wake = pair;
+ s.inbox = .{ .gpa = s.worker_gpa, .lsp_allocator = s.worker_gpa, .io = s.io, .wake_fn = wakeForExit, .wake_ctx = s };
+ pardes.lsp.setStatusSink(&s.inbox, host_io.Inbox.lspStatus);
+ host_io.setExitWake(s, wakeForExit);
+ }
+
+ fn signal(s: *Session) void {
+ while (libc.send(s.wake[1], "w", 1, nosignal) < 0) {
+ if (libc.errno(-1) != .INTR) break;
+ }
}
fn cancelWorkers(s: *Session) void {
- if (s.lsp_task) |*task| {
+ if (s.wake[0] < 0) return; // no initAsync: no workers
+ if (s.inbox.lsp_task) |*task| {
task.future.cancel(s.io);
- s.lsp_task = null;
+ s.inbox.lsp_task = null;
}
s.pipe_tasks.cancelAll(s.io);
_ = s.drainCompletions(false);
}
fn drainCompletions(s: *Session, apply_results: bool) bool {
- var batch = s.mailbox.take();
- for (batch.items[0..batch.len]) |*completion| {
- defer completion.deinit(s.worker_gpa);
+ var bytes: [128]u8 = undefined;
+ while (libc.recv(s.wake[0], &bytes, bytes.len, 0) > 0) {}
+ var batch = s.inbox.take();
+ for (batch.slice()) |msg| {
+ defer msg.deinit(s.worker_gpa, s.worker_gpa);
if (!apply_results) continue;
- switch (completion.*) {
+ switch (msg) {
.lsp => |result| {
s.core.update(.{ .lsp_resp = .{ .id = result.id, .rows = result.rows } });
- if (s.lsp_task) |*task| if (task.id == result.id) {
+ if (s.inbox.lsp_task) |*task| if (task.id == result.id) {
task.future.await(s.io);
- s.lsp_task = null;
+ s.inbox.lsp_task = null;
};
},
.pipe => |result| {
@@ -237,16 +193,15 @@ pub const Session = struct {
} });
s.pipe_tasks.finish(s.io, result.id);
},
+ .lsp_status => |status| {
+ var buf: [256]u8 = undefined;
+ s.core.setStatus(s.core.active, message.stamp(&buf, "lsp", status));
+ },
+ // Ptys are read in the loop's own poll, not through the inbox.
+ .output, .eof, .files_changed, .fs_ready => {},
}
}
- if (batch.status) |status| {
- defer s.worker_gpa.free(status);
- if (apply_results) {
- var buf: [256]u8 = undefined;
- s.core.setStatus(s.core.active, message.stamp(&buf, "lsp", status));
- }
- }
- return batch.len != 0 or batch.status != null;
+ return batch.len != 0;
}
pub fn restore(s: *Session, bytes: []const u8, from: []const u8) !void {
@@ -268,19 +223,20 @@ pub const Session = struct {
for (&s.clients) |*client| if (client.attached) {
client.need_full = true;
};
- s.mailbox.signal();
+ s.signal();
}
pub fn deinit(s: *Session) void {
- // First: its compile thread wakes the mailbox, closed below.
+ // First: its compile thread wakes the loop, whose socket closes below.
s.shaders.deinit(s.gpa, s.inotify_fd, &s.watches);
- if (s.mailbox.wake[0] >= 0) pardes.lsp.setStatusSink(null, null);
- if (s.mailbox.wake[0] >= 0) host_io.setExitWake(null, null);
+ if (s.wake[0] >= 0) pardes.lsp.setStatusSink(null, null);
+ if (s.wake[0] >= 0) host_io.setExitWake(null, null);
s.cancelWorkers();
- for (s.mailbox.wake) |fd| if (fd >= 0) {
+ if (s.wake[0] >= 0) s.inbox.close();
+ for (s.wake) |fd| if (fd >= 0) {
_ = libc.close(fd);
};
- s.mailbox.wake = .{ -1, -1 };
+ s.wake = .{ -1, -1 };
if (s.ninep) |l| {
l.deinit(s.gpa);
s.ninep = null;
@@ -399,40 +355,13 @@ pub const Session = struct {
fn lspRequest(ctx: ?*anyopaque, request: host_io.Lsp.Request) void {
const s = of(ctx);
_ = s.drainCompletions(true);
- // Canceled here, not in `start`: what it delivered before it went
- // is drained first, which leaves its slot in the mailbox free.
- if (s.lsp_task) |*task| {
- task.future.cancel(s.io);
- s.lsp_task = null;
- _ = s.drainCompletions(true);
- }
- host_io.Lsp.start(s.io, s.worker_gpa, s.core, &s.lsp_task, request, &s.mailbox, deliverLspRows);
- }
-
- fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void {
- const box: *Mailbox = @ptrCast(@alignCast(ctx.?));
- box.post(.{ .lsp = .{ .id = id, .rows = rows } });
- }
-
- fn lspStatus(ctx: ?*anyopaque, text: []const u8) void {
- const s = of(ctx);
- const copy = s.worker_gpa.dupe(u8, text) catch return;
- s.mailbox.mutex.lockUncancelable(s.io);
- if (s.mailbox.batch.status) |old| s.worker_gpa.free(old);
- s.mailbox.batch.status = copy;
- s.mailbox.mutex.unlock(s.io);
- s.mailbox.signal();
+ s.inbox.startLsp(s.core, request);
}
fn pipeRequest(ctx: ?*anyopaque, id: u32) void {
const s = of(ctx);
_ = s.drainCompletions(true);
- selection_pipe.start(s.io, s.worker_gpa, s.core, &s.pipe_tasks, id, &s.mailbox, deliverPipe);
- }
-
- fn deliverPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void {
- const box: *Mailbox = @ptrCast(@alignCast(ctx.?));
- box.post(.{ .pipe = response });
+ selection_pipe.start(s.io, s.worker_gpa, s.core, &s.pipe_tasks, id, &s.inbox, host_io.Inbox.pushPipe);
}
fn primary(s: *Session) ?*Client {
@@ -619,7 +548,7 @@ pub const Session = struct {
fn wakeNinep(ctx: ?*anyopaque) void {
const s = of(ctx);
s.ninep_wake.store(true, .release);
- s.mailbox.signal();
+ s.signal();
}
fn watched(ctx: ?*anyopaque) bool {
@@ -814,8 +743,8 @@ pub const Session = struct {
var fds: [poll_slots]libc.pollfd = undefined;
var src: [poll_slots]Source = undefined;
var n: usize = 0;
- if (s.mailbox.wake[0] >= 0) {
- fds[n] = .{ .fd = s.mailbox.wake[0], .events = poll_in, .revents = 0 };
+ if (s.wake[0] >= 0) {
+ fds[n] = .{ .fd = s.wake[0], .events = poll_in, .revents = 0 };
src[n] = .completion;
n += 1;
}
@@ -852,7 +781,7 @@ pub const Session = struct {
}
if (s.ninep) |l| {
// Unix and TCP connections run on cloud9's runner, which wakes
- // this loop through the mailbox; only QUIC is polled here.
+ // this loop through `wake`; only QUIC is polled here.
if (comptime ninep_io.quic_enabled) {
if (l.quic) |*listener| {
fds[n] = listener.poll();
@@ -1087,7 +1016,7 @@ pub const Session = struct {
}
/// Compiles the post chain's files (a finished compile wakes the loop
- /// through the mailbox) and sends each attached frontend the chain it
+ /// through `wake`) and sends each attached frontend the chain it
/// has not seen: on attach, and when it or a file's SPIR-V changes. True
/// when a compile was taken in, so the loop draws what it said.
/// ponytail: a terminal frontend is sent it too and drops it; a hello
@@ -1096,7 +1025,7 @@ pub const Session = struct {
const files = for (s.core.settings.post.list()) |entry| {
if (entry.scene == null) break true;
} else false;
- const compiled = s.shaders.sync(s.gpa, s.io, s.core, if (files) s.inotify() else s.inotify_fd, &s.watches, .{ .ctx = &s.mailbox, .call = wakeForExit });
+ const compiled = s.shaders.sync(s.gpa, s.io, s.core, if (files) s.inotify() else s.inotify_fd, &s.watches, .{ .ctx = s, .call = wakeForExit });
const key = @as(u64, s.shaders.revision) << 8 | @backingInt(s.core.settings.shader_animation);
var chain: wire.Post = .{ .animation = s.core.settings.shader_animation, .len = s.core.settings.post.len };
_ = s.shaders.view(&s.core.settings.post, &chain.passes);
@@ -1283,7 +1212,7 @@ pub fn run(init: std.process.Init, opts: pardes.Options, name: []const u8) !void
core.host = h;
while (core.nextEffect()) |effect| core.perform(effect);
session.in_loop = true;
- quit_wake = session.mailbox.wake[1];
+ quit_wake = session.wake[1];
const on_quit: std.posix.Sigaction = .{ .handler = .{ .handler = onQuitSignal }, .mask = std.posix.sigemptyset(), .flags = 0 };
for ([_]std.posix.SIG{ .HUP, .INT, .TERM }) |sig| std.posix.sigaction(sig, &on_quit, null);
defer quit_wake = -1;
@@ -1339,24 +1268,27 @@ test "detached queued results preserve current requests and are discarded before
const old_id = s.core.lsp_wait.?.id;
s.core.lspRequest(0, .status, "");
const current_id = s.core.lsp_wait.?.id;
- s.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } };
- s.mailbox.post(.{ .lsp = .{ .id = old_id, .rows = try gpa.dupe(u8, "old result\n") } });
- try std.testing.expect(s.drainCompletions(true));
- try std.testing.expectEqual(current_id, s.lsp_task.?.id);
+ s.inbox.lsp_id = current_id;
+ s.inbox.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } };
+ // The inbox turns the superseded answer away.
+ s.inbox.push(.{ .lsp = .{ .id = old_id, .rows = try gpa.dupe(u8, "old result\n") } });
+ try std.testing.expectEqual(@as(usize, 0), s.inbox.completion_len);
+ _ = s.drainCompletions(true);
+ try std.testing.expectEqual(current_id, s.inbox.lsp_task.?.id);
try std.testing.expectEqual(current_id, s.core.lsp_wait.?.id);
try dump.dumpState(s.core);
const saved = try gpa.dupe(u8, s.core.dump_out.?);
defer gpa.free(saved);
while (s.core.nextEffect()) |_| {}
- s.mailbox.post(.{ .lsp = .{ .id = current_id, .rows = try gpa.dupe(u8, "queued before restore\n") } });
+ s.inbox.push(.{ .lsp = .{ .id = current_id, .rows = try gpa.dupe(u8, "queued before restore\n") } });
const outputs = try gpa.alloc([]u8, 1);
outputs[0] = try gpa.dupe(u8, "old filter output\n");
try std.testing.expect(s.pipe_tasks.add(.{ .id = 77, .future = .{ .any_future = null, .result = {} } }));
- s.mailbox.post(.{ .pipe = .{ .id = 77, .success = true, .outputs = outputs } });
- Session.lspStatus(&s, "old status");
+ s.inbox.push(.{ .pipe = .{ .id = 77, .success = true, .outputs = outputs } });
+ host_io.Inbox.lspStatus(&s.inbox, "old status");
try s.restore(saved, "test");
- try std.testing.expect(s.lsp_task == null);
+ try std.testing.expect(s.inbox.lsp_task == null);
try std.testing.expectEqual(@as(usize, 0), s.pipe_tasks.len);
try std.testing.expect(!s.drainCompletions(true));
try std.testing.expectEqualStrings("saved body\n", s.core.panes[0].?.file.?.content);
@@ -1364,15 +1296,16 @@ test "detached queued results preserve current requests and are discarded before
s.core.lspRequest(0, .status, "");
const restored_id = s.core.lsp_wait.?.id;
try std.testing.expect(restored_id > current_id);
- s.lsp_task = .{ .id = restored_id, .future = .{ .any_future = null, .result = {} } };
- s.mailbox.post(.{ .lsp = .{ .id = current_id, .rows = try gpa.dupe(u8, "late old result\n") } });
+ s.inbox.lsp_id = restored_id;
+ s.inbox.lsp_task = .{ .id = restored_id, .future = .{ .any_future = null, .result = {} } };
+ s.inbox.push(.{ .lsp = .{ .id = current_id, .rows = try gpa.dupe(u8, "late old result\n") } });
_ = s.drainCompletions(true);
- try std.testing.expectEqual(restored_id, s.lsp_task.?.id);
+ try std.testing.expectEqual(restored_id, s.inbox.lsp_task.?.id);
try std.testing.expectEqual(restored_id, s.core.lsp_wait.?.id);
- s.mailbox.post(.{ .lsp = .{ .id = restored_id, .rows = &.{} } });
+ s.inbox.push(.{ .lsp = .{ .id = restored_id, .rows = &.{} } });
const host = s.host();
host.vtable.wait_input.?(host.ctx, 1);
- try std.testing.expect(s.lsp_task == null);
+ try std.testing.expect(s.inbox.lsp_task == null);
try std.testing.expect(s.core.lsp_wait == null);
}
@@ -1434,7 +1367,7 @@ test "detached worker setup failure completes requests without changing document
s.core.lspRequest(0, .status, "");
while (s.core.nextEffect()) |effect| s.core.perform(effect);
try std.testing.expect(s.core.lsp_wait == null);
- try std.testing.expect(s.lsp_task == null);
+ try std.testing.expect(s.inbox.lsp_task == null);
pane.body.cur_col = 2;
pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true };
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 795ea743..1daf8f41 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -835,264 +835,25 @@ const Pty = struct {
cmd: host_io.CommandWatch = .{},
};
-/// A watched child exited: wake the loop, which takes it (`takeExits`).
-fn wakeForExit(ctx: ?*anyopaque) void {
- const q: *Queue = @ptrCast(@alignCast(ctx.?));
- q.wake();
-}
const RetiredShell = struct { pid: libc.pid_t = 0, kill_at: i64 = 0 };
-const Msg = union(enum) {
- output: struct { pane: u8, gen: u32, bytes: []u8 },
- eof: struct { pane: u8, gen: u32, failure: ?anyerror = null },
- lsp: struct { id: u32, rows: ?[]u8 },
- lsp_status: []u8,
- pipe: selection_pipe.Response,
- files_changed,
- fs_ready,
-
- fn deinit(m: Msg, gpa: std.mem.Allocator, lsp_allocator: std.mem.Allocator) void {
- switch (m) {
- .output => |o| gpa.free(o.bytes),
- .lsp => |l| if (l.rows) |rows| lsp_allocator.free(rows),
- .lsp_status => |t| lsp_allocator.free(t),
- .pipe => |response_value| {
- var response = response_value;
- response.deinit(gpa);
- },
- .eof, .files_changed, .fs_ready => {},
- }
- }
-};
+const Queue = host_io.Inbox;
+/// Wakes a blocking SDL_WaitEventTimeout after a push from another thread.
+fn sdlWake(_: ?*anyopaque) void {
+ var sev = std.mem.zeroes(c.SDL_Event);
+ sev.type = c.SDL_EVENT_USER;
+ _ = c.SDL_PushEvent(&sev);
+}
+const Msg = Queue.Msg;
+const MessageBatch = Queue.Batch;
const PipeTasks = selection_pipe.Tasks;
-
-const queue_capacity = 512;
-const output_capacity = queue_capacity - pardes.MAX_PANES;
-
-const MessageBatch = struct {
- items: [queue_capacity + PipeTasks.capacity + 2]Msg = undefined,
- len: usize = 0,
-
- fn slice(batch: *MessageBatch) []Msg {
- return batch.items[0..batch.len];
- }
-};
-
-const Queue = struct {
- gpa: std.mem.Allocator,
- lsp_allocator: std.mem.Allocator,
- /// The query in flight; the UI thread's alone (spawnLsp, teardown).
- lsp_task: ?host_io.Lsp.Task = null,
- sdl_wake: bool, // wake a blocking SDL_WaitEventTimeout on cross-thread push
- /// The process's Io: its terminal readers wait for space on `space`.
- io: std.Io,
- mutex: std.Io.Mutex = .init,
- space: std.Io.Condition = .init,
- items: [queue_capacity]Msg = undefined,
- head: usize = 0,
- len: usize = 0,
- closed: bool = false,
- files_changed: bool = false,
- readers: [pardes.MAX_PANES]?u32 = @splat(null),
- waiting: usize = 0,
- lsp_id: ?u32 = null,
- completions: [PipeTasks.capacity + 1]Msg = undefined,
- completion_len: usize = 0,
-
- fn lock(q: *Queue) void {
- q.mutex.lockUncancelable(q.io);
- }
-
- fn unlock(q: *Queue) void {
- q.mutex.unlock(q.io);
- }
-
- fn wake(q: *Queue) void {
- if (q.sdl_wake) {
- var sev = std.mem.zeroes(c.SDL_Event);
- sev.type = c.SDL_EVENT_USER;
- _ = c.SDL_PushEvent(&sev);
- }
- }
-
- fn acceptReader(q: *Queue, pane: u8, gen: u32) void {
- q.lock();
- defer q.unlock();
- std.debug.assert(q.readers[pane] == null);
- q.readers[pane] = gen;
- }
-
- fn cancelReader(q: *Queue, pane: u8, gen: u32) void {
- q.lock();
- defer q.unlock();
- if (q.readers[pane] != gen) return;
- q.readers[pane] = null;
- var i: usize = 0;
- while (i < q.len) {
- const msg = q.items[(q.head + i) % q.items.len];
- const matches = switch (msg) {
- .output => |o| o.pane == pane and o.gen == gen,
- .eof => |e| e.pane == pane and e.gen == gen,
- else => false,
- };
- if (matches) q.removeAt(i).deinit(q.gpa, q.lsp_allocator) else i += 1;
- }
- q.space.broadcast(q.io);
- }
-
- fn pushOutput(q: *Queue, pane: u8, gen: u32, bytes: []u8) bool {
- q.lock();
- while (!q.closed and q.readers[pane] == gen and q.len >= output_capacity) {
- q.waiting += 1;
- q.space.waitUncancelable(q.io, &q.mutex);
- q.waiting -= 1;
- }
- if (q.closed or q.readers[pane] != gen) {
- q.unlock();
- q.gpa.free(bytes);
- return false;
- }
- q.items[(q.head + q.len) % q.items.len] = .{ .output = .{ .pane = pane, .gen = gen, .bytes = bytes } };
- q.len += 1;
- q.unlock();
- q.wake();
- return true;
- }
-
- fn removeAt(q: *Queue, offset: usize) Msg {
- const removed = q.items[(q.head + offset) % q.items.len];
- var i = offset;
- while (i + 1 < q.len) : (i += 1)
- q.items[(q.head + i) % q.items.len] = q.items[(q.head + i + 1) % q.items.len];
- q.len -= 1;
- return removed;
- }
-
- fn discardLsp(q: *Queue) void {
- for (q.completions[0..q.completion_len], 0..) |msg, i| if (msg == .lsp) {
- msg.deinit(q.gpa, q.lsp_allocator);
- q.completion_len -= 1;
- std.mem.copyForwards(Msg, q.completions[i..q.completion_len], q.completions[i + 1 .. q.completion_len + 1]);
- return;
- };
- }
-
- fn push(q: *Queue, m: Msg) void {
- if (m == .output) {
- _ = q.pushOutput(m.output.pane, m.output.gen, m.output.bytes);
- return;
- }
- q.lock();
- if (q.closed) {
- q.unlock();
- m.deinit(q.gpa, q.lsp_allocator);
- return;
- }
- switch (m) {
- .files_changed => q.files_changed = true,
- .lsp, .pipe => {
- if (m == .lsp) {
- if (q.lsp_id != m.lsp.id) {
- q.unlock();
- m.deinit(q.gpa, q.lsp_allocator);
- return;
- }
- q.discardLsp();
- }
- std.debug.assert(q.completion_len < q.completions.len);
- q.completions[q.completion_len] = m;
- q.completion_len += 1;
- },
- else => {
- if (m == .eof) {
- if (q.readers[m.eof.pane] != m.eof.gen) {
- q.unlock();
- return;
- }
- std.debug.assert(q.len < q.items.len);
- } else if (q.len >= output_capacity) {
- q.unlock();
- m.deinit(q.gpa, q.lsp_allocator);
- return;
- }
- q.items[(q.head + q.len) % q.items.len] = m;
- q.len += 1;
- },
- }
- q.unlock();
- q.wake();
- }
-
- fn take(q: *Queue) MessageBatch {
- var batch: MessageBatch = .{};
- q.takeInto(&batch);
- return batch;
- }
-
- /// `take` into a batch the caller keeps: an idle wakeup copies nothing
- /// (the batch is some 20 KB, and was built and returned by value each
- /// time).
- fn takeInto(q: *Queue, batch: *MessageBatch) void {
- batch.len = 0;
- q.lock();
- defer q.unlock();
- if (q.len == 0 and !q.files_changed and q.completion_len == 0) return;
- while (q.len > 0) {
- batch.items[batch.len] = q.items[q.head];
- batch.len += 1;
- q.head = (q.head + 1) % q.items.len;
- q.len -= 1;
- }
- q.head = 0;
- if (q.files_changed) {
- batch.items[batch.len] = .files_changed;
- batch.len += 1;
- q.files_changed = false;
- }
- @memcpy(batch.items[batch.len..][0..q.completion_len], q.completions[0..q.completion_len]);
- batch.len += q.completion_len;
- q.completion_len = 0;
- q.space.broadcast(q.io);
- }
-
- fn discardCompletions(q: *Queue) void {
- q.lock();
- defer q.unlock();
- q.lsp_id = null;
- for (q.completions[0..q.completion_len]) |msg| msg.deinit(q.gpa, q.lsp_allocator);
- q.completion_len = 0;
- }
-
- fn close(q: *Queue) void {
- q.lock();
- q.closed = true;
- q.files_changed = false;
- q.readers = @splat(null);
- q.space.broadcast(q.io);
- q.unlock();
- q.discardCompletions();
- q.lock();
- defer q.unlock();
- while (q.len > 0) {
- const m = q.items[q.head];
- m.deinit(q.gpa, q.lsp_allocator);
- q.head = (q.head + 1) % q.items.len;
- q.len -= 1;
- }
- q.head = 0;
- }
-
- fn deinit(q: *Queue) void {
- q.close();
- std.debug.assert(q.waiting == 0);
- }
-};
+const output_capacity = Queue.output_capacity;
test "GUI completion survives a full output queue" {
const gpa = std.testing.allocator;
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .lsp_id = 7 };
+ var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .lsp_id = 7 };
defer queue.deinit();
queue.acceptReader(0, 1);
for (0..output_capacity) |_| queue.push(.{ .output = .{
@@ -1148,7 +909,7 @@ const PtyTests = struct {
test "GUI PTY backpressure retains byte order and tail before EOF" {
const gpa = std.testing.allocator;
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false };
+ var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa };
defer queue.deinit();
queue.acceptReader(0, 1);
for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "before") } });
@@ -1182,7 +943,7 @@ test "GUI PTY backpressure retains byte order and tail before EOF" {
test "GUI PTY backpressure cancellation and close release owned producers" {
const gpa = std.testing.allocator;
for ([_]bool{ false, true }) |close| {
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false };
+ var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa };
defer queue.deinit();
queue.acceptReader(0, 1);
for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "before") } });
@@ -1213,14 +974,14 @@ test "GUI PTY backpressure cancellation and close release owned producers" {
test "GUI PTY EOF reserve cannot evict terminal output" {
const gpa = std.testing.allocator;
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false };
+ var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa };
defer queue.deinit();
for (0..pardes.MAX_PANES) |pane| queue.acceptReader(@intCast(pane), 1);
for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "before") } });
for (0..pardes.MAX_PANES) |pane| queue.push(.{ .eof = .{ .pane = @intCast(pane), .gen = 1 } });
var batch = queue.take();
defer for (batch.slice()) |msg| msg.deinit(gpa, gpa);
- try std.testing.expectEqual(queue_capacity, batch.len);
+ try std.testing.expectEqual(Queue.capacity, batch.len);
for (batch.items[0..output_capacity]) |msg| try std.testing.expectEqualStrings("before", msg.output.bytes);
for (batch.items[output_capacity..batch.len], 0..) |msg, pane| try std.testing.expectEqual(pane, msg.eof.pane);
}
@@ -1229,7 +990,7 @@ test "GUI PTY reader delivers a real shell tail before EOF and joins on close" {
const gpa = std.testing.allocator;
const core = try pardes.Pardes.init(gpa, .{ .tty_only = true });
defer core.deinit();
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false };
+ var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa };
defer queue.deinit();
var ptys: [pardes.MAX_PANES]?Pty = @splat(null);
var gens: [pardes.MAX_PANES]u32 = @splat(1);
@@ -1244,7 +1005,7 @@ test "GUI PTY reader delivers a real shell tail before EOF and joins on close" {
const rcs: host_io.Shell.PromptFiles = .{};
const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80, null);
ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial };
- try spawnReader(gpa, &ptys[0].?, 0, 1, &queue);
+ ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, 0, 1);
try std.testing.expect(host_io.writeFd(child.file.handle, "printf '\\120\\101\\122\\104\\105\\123\\055\\124\\101\\111\\114'; exit\n"));
var bytes: std.ArrayList(u8) = .empty;
defer bytes.deinit(gpa);
@@ -1279,7 +1040,7 @@ test "GUI PTY Restore joins a real reader waiting for queue space" {
var core = try pardes.Pardes.init(gpa, .{ .tty_only = true });
defer core.deinit();
try dump.dumpState(core);
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false };
+ var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa };
defer queue.deinit();
queue.acceptReader(1, 1);
for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = 1, .gen = 1, .bytes = try gpa.dupe(u8, "other pane") } });
@@ -1296,7 +1057,7 @@ test "GUI PTY Restore joins a real reader waiting for queue space" {
const rcs: host_io.Shell.PromptFiles = .{};
const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80, null);
ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial };
- try spawnReader(gpa, &ptys[0].?, 0, 1, &queue);
+ ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, 0, 1);
try std.testing.expect(host_io.writeFd(child.file.handle, "printf 'after-full'; exit\n"));
try PtyTests.waitBlocked(&queue);
const old_serial = core.panes[0].?.serial;
@@ -1323,7 +1084,7 @@ test "GUI PTY deletion joins an idle reader and retains only its owned child for
const gpa = std.testing.allocator;
const core = try pardes.Pardes.init(gpa, .{ .tty_only = true });
defer core.deinit();
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false };
+ var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa };
defer queue.deinit();
var ptys: [pardes.MAX_PANES]?Pty = @splat(null);
var gens: [pardes.MAX_PANES]u32 = @splat(1);
@@ -1338,7 +1099,7 @@ test "GUI PTY deletion joins an idle reader and retains only its owned child for
const rcs: host_io.Shell.PromptFiles = .{};
const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80, null);
ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial };
- try spawnReader(gpa, &ptys[0].?, 0, 1, &queue);
+ ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, 0, 1);
try core.removePane(0, null);
shell.reconcilePtys();
try std.testing.expect(queue.readers[0] == null);
@@ -1354,7 +1115,7 @@ test "GUI PTY deletion joins an idle reader and retains only its owned child for
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, .sdl_wake = false };
+ 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;
@@ -1378,7 +1139,7 @@ test "GUI completion failure survives backlog and leaves the document unchanged"
pane.body.cur_col = 1;
core.lspRequest(core.active, .completion, "");
const id = core.lsp_wait.?.id;
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .lsp_id = id };
+ var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .lsp_id = id };
defer queue.deinit();
queue.acceptReader(0, 1);
for (0..output_capacity) |_| queue.push(.{ .output = .{
@@ -1407,7 +1168,7 @@ test "GUI completion failure survives backlog and leaves the document unchanged"
test "GUI completions retain their arrival order across LSP and pipes" {
const gpa = std.testing.allocator;
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .lsp_id = 2 };
+ var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .lsp_id = 2 };
defer queue.deinit();
queue.push(.{ .pipe = .{ .id = 1, .success = true, .outputs = &.{} } });
queue.push(.{ .lsp = .{ .id = 2, .rows = try gpa.dupe(u8, "second") } });
@@ -1423,7 +1184,7 @@ test "GUI completions retain their arrival order across LSP and pipes" {
test "GUI completion reset frees queued payloads and rejects late LSP workers" {
const gpa = std.testing.allocator;
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .lsp_id = 9 };
+ var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .lsp_id = 9 };
defer queue.deinit();
queue.acceptReader(0, 1);
queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "retained") } });
@@ -1470,35 +1231,7 @@ test "GUI completion reset frees queued payloads and rejects late LSP workers" {
try std.testing.expectEqual(@as(usize, 0), queue.take().len);
}
-fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void {
- const q: *Queue = @ptrCast(@alignCast(ctx orelse return));
- const copy = q.lsp_allocator.dupe(u8, text) catch return;
- q.push(.{ .lsp_status = copy });
-}
-fn readPty(io: std.Io, gpa: std.mem.Allocator, fd: c_int, pane: u8, gen: u32, q: *Queue) void {
- const pty: std.Io.File = .{ .handle = fd, .flags = .{ .nonblocking = false } };
- var buf: [0x10000]u8 = undefined;
- var failure: ?anyerror = null;
- while (true) {
- const n = pty.readStreaming(io, &.{&buf}) catch |err| switch (err) {
- // A pty whose child has gone answers EIO: its end of file.
- error.EndOfStream, error.InputOutput => break,
- error.Canceled => return,
- else => {
- failure = err;
- break;
- },
- };
- if (n == 0) continue;
- const bytes = gpa.dupe(u8, buf[0..n]) catch |err| {
- failure = err;
- break;
- };
- if (!q.pushOutput(pane, gen, bytes)) return;
- }
- q.push(.{ .eof = .{ .pane = pane, .gen = gen, .failure = failure } });
-}
fn stopPipe() ![2]c_int {
var fds: [2]c_int = undefined;
@@ -1510,12 +1243,6 @@ fn stopPipe() ![2]c_int {
return fds;
}
-fn spawnReader(gpa: std.mem.Allocator, pt: *Pty, pane: u8, gen: u32, q: *Queue) !void {
- std.debug.assert(pt.reader == null and pt.fd >= 0);
- q.acceptReader(pane, gen);
- errdefer q.cancelReader(pane, gen);
- pt.reader = try q.io.concurrent(readPty, .{ q.io, gpa, pt.fd, pane, gen, q });
-}
fn watchThread(fd: c_int, stop: c_int, q: *Queue) void {
while (true) {
@@ -1533,20 +1260,7 @@ fn watchThread(fd: c_int, stop: c_int, q: *Queue) void {
}
}
-fn pushLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void {
- const q: *Queue = @ptrCast(@alignCast(ctx orelse return));
- q.push(.{ .lsp = .{ .id = id, .rows = rows } });
-}
-fn spawnLsp(core: *pardes.Pardes, io: std.Io, q: *Queue, e: host_io.Lsp.Request) void {
- q.lock();
- q.lsp_id = e.id;
- q.discardLsp();
- q.unlock();
- // The old query's waits are cancelation points: its join is short, and
- // what it still delivers carries an id the queue now rejects.
- host_io.Lsp.start(io, q.lsp_allocator, core, &q.lsp_task, e, q, pushLspRows);
-}
test "GUI LSP query answers, and a newer one cancels it" {
const gpa = std.testing.allocator;
@@ -1554,11 +1268,11 @@ test "GUI LSP query answers, and a newer one cancels it" {
const core = try pardes.Pardes.init(gpa, .{ .tty_only = true });
defer core.deinit();
_ = try core.setTestFile("abc");
- var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false };
+ var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa };
defer queue.deinit();
for (0..2) |_| {
core.lspRequest(core.active, .status, "");
- spawnLsp(core, io, &queue, .{ .id = core.lsp_wait.?.id, .kind = .status, .pane = @intCast(core.active), .offset = 0, .arg = "" });
+ queue.startLsp(core, .{ .id = core.lsp_wait.?.id, .kind = .status, .pane = @intCast(core.active), .offset = 0, .arg = "" });
}
const last = core.lsp_wait.?.id;
queue.lsp_task.?.future.await(io);
@@ -1572,10 +1286,6 @@ test "GUI LSP query answers, and a newer one cancels it" {
try std.testing.expect(core.lsp_wait == null);
}
-fn pushPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void {
- const q: *Queue = @ptrCast(@alignCast(ctx.?));
- q.push(.{ .pipe = response });
-}
test "GUI worker setup failures finish matching LSP and pipe requests" {
const gpa = std.testing.allocator;
@@ -1593,7 +1303,6 @@ test "GUI worker setup failures finish matching LSP and pipe requests" {
.io = std.testing.io,
.gpa = gpa,
.lsp_allocator = failing.allocator(),
- .sdl_wake = false,
};
defer queue.deinit();
var tasks: PipeTasks = .{};
@@ -1608,7 +1317,7 @@ test "GUI worker setup failures finish matching LSP and pipe requests" {
if (failure == 0) {
queue.lsp_id = req.id -% 1;
queue.push(.{ .lsp = .{ .id = queue.lsp_id.?, .rows = try gpa.dupe(u8, "old queued result") } });
- spawnLsp(core, std.testing.io, &queue, req);
+ queue.startLsp(core, req);
try std.testing.expectEqual(req.id, queue.lsp_id.?);
try std.testing.expectEqual(@as(usize, 0), queue.completion_len);
queue.push(.{ .lsp = .{ .id = req.id -% 1, .rows = try gpa.dupe(u8, "late old result") } });
@@ -1628,7 +1337,7 @@ test "GUI worker setup failures finish matching LSP and pipe requests" {
core.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } });
core.update(.{ .key = .{ .cp = pardes.Key.enter } });
const pipe_id = core.pipe.wait.?.id;
- selection_pipe.start(failing_io, failing.allocator(), core, &tasks, pipe_id, &queue, pushPipe);
+ selection_pipe.start(failing_io, failing.allocator(), core, &tasks, pipe_id, &queue, Queue.pushPipe);
try std.testing.expect(core.pipe.wait == null);
try std.testing.expectEqualStrings("abc", pane.file.?.content);
try std.testing.expect(queue.lsp_task == null);
@@ -2489,7 +2198,7 @@ fn localSession(
.io = io,
.gpa = gpa,
.lsp_allocator = allocs.lsp,
- .sdl_wake = true,
+ .wake_fn = sdlWake,
};
defer {
if (queue.lsp_task) |*task| task.future.cancel(io);
@@ -2542,8 +2251,9 @@ fn localSession(
core.host = host;
while (core.nextEffect()) |e| core.perform(e);
- for (&ptys, 0..) |*slot, id| if (slot.*) |*pt|
- try spawnReader(gpa, pt, @intCast(id), gens[id], &queue);
+ for (&ptys, 0..) |*slot, id| if (slot.*) |*pt| {
+ pt.reader = try queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, @intCast(id), gens[id]);
+ };
if (inotify_fd >= 0) {
watch_stop = try stopPipe();
watch_reader = try std.Thread.spawn(.{}, watchThread, .{ inotify_fd, watch_stop[0], &queue });
@@ -2554,9 +2264,9 @@ fn localSession(
if (test_mode) setStdinRaw() catch {};
shell.threads_ok = true;
- pardes.lsp.setStatusSink(&queue, lspStatusSink);
+ pardes.lsp.setStatusSink(&queue, Queue.lspStatus);
defer pardes.lsp.setStatusSink(null, null);
- host_io.setExitWake(&queue, wakeForExit);
+ host_io.setExitWake(&queue, Queue.wakeForExit);
defer host_io.setExitWake(null, null);
while (!core.quit) {
@@ -2860,7 +2570,6 @@ fn runGrid(init: std.process.Init, opts_in: pardes.Options) !void {
.io = io,
.gpa = gpa,
.lsp_allocator = allocs.lsp,
- .sdl_wake = false,
};
defer {
if (queue.lsp_task) |*task| task.future.cancel(io);
@@ -2889,12 +2598,13 @@ fn runGrid(init: std.process.Init, opts_in: pardes.Options) !void {
const host = shell.host();
core.host = host;
while (core.nextEffect()) |e| core.perform(e);
- for (&ptys, 0..) |*slot, id| if (slot.*) |*pt|
- try spawnReader(gpa, pt, @intCast(id), gens[id], &queue);
+ for (&ptys, 0..) |*slot, id| if (slot.*) |*pt| {
+ pt.reader = try queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, @intCast(id), gens[id]);
+ };
shell.threads_ok = true;
- pardes.lsp.setStatusSink(&queue, lspStatusSink);
+ pardes.lsp.setStatusSink(&queue, Queue.lspStatus);
defer pardes.lsp.setStatusSink(null, null);
- host_io.setExitWake(&queue, wakeForExit);
+ host_io.setExitWake(&queue, Queue.wakeForExit);
defer host_io.setExitWake(null, null);
setStdinRaw() catch {}; // stdin may be a pipe, not a pty — best effort
@@ -4019,7 +3729,7 @@ const Shell = struct {
// in it; then its read is canceled (or, at its end, it is joined).
s.queue.cancelReader(pane, s.gens[pane]);
if (pt.reader) |*reader| {
- reader.cancel(s.queue.io); // the Io it was started on (spawnReader)
+ reader.cancel(s.queue.io); // the Io it was started on (startReader)
pt.reader = null;
}
_ = libc.close(pt.fd);
@@ -4551,9 +4261,10 @@ fn spawnPane(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void {
s.ptys[pane] = pt;
var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined;
if (host_io.shellCwd(pt.pid, &lbuf)) |wd| s.core.setCwd(pane, wd);
- if (s.threads_ok) spawnReader(s.gpa, &s.ptys[pane].?, pane, s.gens[pane], s.queue) catch |err| {
+ if (!s.threads_ok) return;
+ s.ptys[pane].?.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, pane, s.gens[pane]) catch |err| {
s.closePty(pane);
- s.core.reportError(pane, "terminal reader", err);
+ return s.core.reportError(pane, "terminal reader", err);
};
}
@@ -4671,7 +4382,6 @@ test "the headless grid host round-trips a yank back as a paste" {
.io = std.testing.io,
.gpa = gpa,
.lsp_allocator = gpa,
- .sdl_wake = false,
};
defer queue.deinit();
var pipe_tasks: PipeTasks = .{};
@@ -4751,14 +4461,14 @@ fn openLink(ctx: ?*anyopaque, url: []const u8) void {
fn lsp(ctx: ?*anyopaque, req: host_io.Lsp.Request) void {
const s = shellOf(ctx);
- if (s.threads_ok) return spawnLsp(s.core, s.io, s.queue, req);
+ if (s.threads_ok) return s.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 {
const s = shellOf(ctx);
- if (s.threads_ok) return selection_pipe.start(s.io, s.gpa, s.core, s.pipe_tasks, id, s.queue, pushPipe);
+ if (s.threads_ok) return selection_pipe.start(s.io, s.gpa, s.core, s.pipe_tasks, id, s.queue, Queue.pushPipe);
s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } });
s.core.reportError(s.core.active, "pipe", error.WorkersUnavailable);
}
diff --git a/src/host_io.zig b/src/host_io.zig
index a17414fb..b32fff55 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -3,6 +3,7 @@ const std = @import("std");
const posix = std.posix;
const libc = std.c;
const pardes = @import("pardes.zig");
+const selection_pipe = @import("selection_pipe.zig");
const ninep_io = @import("9p_io.zig");
const filesystem = @import("fs.zig");
const file_watch = @import("file_watch.zig");
@@ -114,6 +115,323 @@ pub const Fallback = struct {
}
};
+/// What a pixel host's loop takes from its workers, in order: each pane's
+/// pty output (a reader waits while `output_capacity` is full, so output is
+/// never dropped or reordered), its end of file, the language client's
+/// answers and status and the `|` jobs' answers (those kept apart, so a
+/// full output queue never holds them up), and file-watch news. Output and
+/// end of file of a reader that was canceled or replaced, and an answer to
+/// a language query since superseded, are dropped here.
+pub const Inbox = struct {
+ pub const Msg = union(enum) {
+ output: struct { pane: u8, gen: u32, bytes: []u8 },
+ eof: struct { pane: u8, gen: u32, failure: ?anyerror = null },
+ lsp: struct { id: u32, rows: ?[]u8 },
+ lsp_status: []u8,
+ pipe: selection_pipe.Response,
+ files_changed,
+ fs_ready,
+
+ pub fn deinit(m: Msg, gpa: std.mem.Allocator, lsp_allocator: std.mem.Allocator) void {
+ switch (m) {
+ .output => |o| gpa.free(o.bytes),
+ .lsp => |l| if (l.rows) |rows| lsp_allocator.free(rows),
+ .lsp_status => |t| lsp_allocator.free(t),
+ .pipe => |response_value| {
+ var response = response_value;
+ response.deinit(gpa);
+ },
+ .eof, .files_changed, .fs_ready => {},
+ }
+ }
+ };
+
+ pub const capacity = 512;
+ pub const output_capacity = capacity - pardes.MAX_PANES;
+
+ pub const Batch = struct {
+ items: [capacity + selection_pipe.Tasks.capacity + 2]Msg = undefined,
+ len: usize = 0,
+
+ pub fn slice(batch: *Batch) []Msg {
+ return batch.items[0..batch.len];
+ }
+ };
+
+ gpa: std.mem.Allocator,
+ lsp_allocator: std.mem.Allocator,
+ /// The query in flight; the UI thread's alone (spawnLsp, teardown).
+ lsp_task: ?Lsp.Task = null,
+ /// Wakes the host's loop after a push from another thread: an SDL
+ /// event, a macOS runtime callback. Null: nobody to wake (tests).
+ wake_fn: ?*const fn (?*anyopaque) void = null,
+ wake_ctx: ?*anyopaque = null,
+ /// The process's Io: its terminal readers wait for space on `space`.
+ io: std.Io,
+ mutex: std.Io.Mutex = .init,
+ space: std.Io.Condition = .init,
+ items: [capacity]Msg = undefined,
+ head: usize = 0,
+ len: usize = 0,
+ closed: bool = false,
+ files_changed: bool = false,
+ readers: [pardes.MAX_PANES]?u32 = @splat(null),
+ waiting: usize = 0,
+ lsp_id: ?u32 = null,
+ completions: [selection_pipe.Tasks.capacity + 1]Msg = undefined,
+ completion_len: usize = 0,
+
+ pub fn lock(q: *Inbox) void {
+ q.mutex.lockUncancelable(q.io);
+ }
+
+ pub fn unlock(q: *Inbox) void {
+ q.mutex.unlock(q.io);
+ }
+
+ pub fn wake(q: *Inbox) void {
+ if (q.wake_fn) |f| f(q.wake_ctx);
+ }
+
+ pub fn acceptReader(q: *Inbox, pane: u8, gen: u32) void {
+ q.lock();
+ defer q.unlock();
+ std.debug.assert(q.readers[pane] == null);
+ q.readers[pane] = gen;
+ }
+
+ pub fn cancelReader(q: *Inbox, pane: u8, gen: u32) void {
+ q.lock();
+ defer q.unlock();
+ if (q.readers[pane] != gen) return;
+ q.readers[pane] = null;
+ var i: usize = 0;
+ while (i < q.len) {
+ const msg = q.items[(q.head + i) % q.items.len];
+ const matches = switch (msg) {
+ .output => |o| o.pane == pane and o.gen == gen,
+ .eof => |e| e.pane == pane and e.gen == gen,
+ else => false,
+ };
+ if (matches) q.removeAt(i).deinit(q.gpa, q.lsp_allocator) else i += 1;
+ }
+ q.space.broadcast(q.io);
+ }
+
+ pub fn pushOutput(q: *Inbox, pane: u8, gen: u32, bytes: []u8) bool {
+ q.lock();
+ while (!q.closed and q.readers[pane] == gen and q.len >= output_capacity) {
+ q.waiting += 1;
+ q.space.waitUncancelable(q.io, &q.mutex);
+ q.waiting -= 1;
+ }
+ if (q.closed or q.readers[pane] != gen) {
+ q.unlock();
+ q.gpa.free(bytes);
+ return false;
+ }
+ q.items[(q.head + q.len) % q.items.len] = .{ .output = .{ .pane = pane, .gen = gen, .bytes = bytes } };
+ q.len += 1;
+ q.unlock();
+ q.wake();
+ return true;
+ }
+
+ fn removeAt(q: *Inbox, offset: usize) Msg {
+ const removed = q.items[(q.head + offset) % q.items.len];
+ var i = offset;
+ while (i + 1 < q.len) : (i += 1)
+ q.items[(q.head + i) % q.items.len] = q.items[(q.head + i + 1) % q.items.len];
+ q.len -= 1;
+ return removed;
+ }
+
+ pub fn discardLsp(q: *Inbox) void {
+ for (q.completions[0..q.completion_len], 0..) |msg, i| if (msg == .lsp) {
+ msg.deinit(q.gpa, q.lsp_allocator);
+ q.completion_len -= 1;
+ std.mem.copyForwards(Msg, q.completions[i..q.completion_len], q.completions[i + 1 .. q.completion_len + 1]);
+ return;
+ };
+ }
+
+ pub fn push(q: *Inbox, m: Msg) void {
+ if (m == .output) {
+ _ = q.pushOutput(m.output.pane, m.output.gen, m.output.bytes);
+ return;
+ }
+ q.lock();
+ if (q.closed) {
+ q.unlock();
+ m.deinit(q.gpa, q.lsp_allocator);
+ return;
+ }
+ switch (m) {
+ .files_changed => q.files_changed = true,
+ .lsp, .pipe => {
+ if (m == .lsp) {
+ if (q.lsp_id != m.lsp.id) {
+ q.unlock();
+ m.deinit(q.gpa, q.lsp_allocator);
+ return;
+ }
+ q.discardLsp();
+ }
+ std.debug.assert(q.completion_len < q.completions.len);
+ q.completions[q.completion_len] = m;
+ q.completion_len += 1;
+ },
+ else => {
+ if (m == .eof) {
+ if (q.readers[m.eof.pane] != m.eof.gen) {
+ q.unlock();
+ return;
+ }
+ std.debug.assert(q.len < q.items.len);
+ } else if (q.len >= output_capacity) {
+ q.unlock();
+ m.deinit(q.gpa, q.lsp_allocator);
+ return;
+ }
+ q.items[(q.head + q.len) % q.items.len] = m;
+ q.len += 1;
+ },
+ }
+ q.unlock();
+ q.wake();
+ }
+
+ pub fn take(q: *Inbox) Batch {
+ var batch: Batch = .{};
+ q.takeInto(&batch);
+ return batch;
+ }
+
+ /// `take` into a batch the caller keeps: an idle wakeup copies nothing
+ /// (the batch is some 20 KB, and was built and returned by value each
+ /// time).
+ pub fn takeInto(q: *Inbox, batch: *Batch) void {
+ batch.len = 0;
+ q.lock();
+ defer q.unlock();
+ if (q.len == 0 and !q.files_changed and q.completion_len == 0) return;
+ while (q.len > 0) {
+ batch.items[batch.len] = q.items[q.head];
+ batch.len += 1;
+ q.head = (q.head + 1) % q.items.len;
+ q.len -= 1;
+ }
+ q.head = 0;
+ if (q.files_changed) {
+ batch.items[batch.len] = .files_changed;
+ batch.len += 1;
+ q.files_changed = false;
+ }
+ @memcpy(batch.items[batch.len..][0..q.completion_len], q.completions[0..q.completion_len]);
+ batch.len += q.completion_len;
+ q.completion_len = 0;
+ q.space.broadcast(q.io);
+ }
+
+ pub fn discardCompletions(q: *Inbox) void {
+ q.lock();
+ defer q.unlock();
+ q.lsp_id = null;
+ for (q.completions[0..q.completion_len]) |msg| msg.deinit(q.gpa, q.lsp_allocator);
+ q.completion_len = 0;
+ }
+
+ pub fn close(q: *Inbox) void {
+ q.lock();
+ q.closed = true;
+ q.files_changed = false;
+ q.readers = @splat(null);
+ q.space.broadcast(q.io);
+ q.unlock();
+ q.discardCompletions();
+ q.lock();
+ defer q.unlock();
+ while (q.len > 0) {
+ const m = q.items[q.head];
+ m.deinit(q.gpa, q.lsp_allocator);
+ q.head = (q.head + 1) % q.items.len;
+ q.len -= 1;
+ }
+ q.head = 0;
+ }
+
+ pub fn deinit(q: *Inbox) void {
+ q.close();
+ std.debug.assert(q.waiting == 0);
+ }
+
+ /// A pane's reader: its pty's output pushed as it comes, then its end
+ /// of file; canceled (closePty), it pushes nothing more.
+ pub fn startReader(q: *Inbox, pty: std.Io.File, pane: u8, gen: u32) !std.Io.Future(void) {
+ q.acceptReader(pane, gen);
+ errdefer q.cancelReader(pane, gen);
+ return q.io.concurrent(readPty, .{ q, pty, pane, gen });
+ }
+
+ fn readPty(q: *Inbox, pty: std.Io.File, pane: u8, gen: u32) void {
+ var buf: [0x10000]u8 = undefined;
+ var failure: ?anyerror = null;
+ while (true) {
+ const n = pty.readStreaming(q.io, &.{&buf}) catch |err| switch (err) {
+ // A pty whose child has gone answers EIO: its end of file.
+ error.EndOfStream, error.InputOutput => break,
+ error.Canceled => return,
+ else => {
+ failure = err;
+ break;
+ },
+ };
+ if (n == 0) continue;
+ const bytes = q.gpa.dupe(u8, buf[0..n]) catch |err| {
+ failure = err;
+ break;
+ };
+ if (!q.pushOutput(pane, gen, bytes)) return;
+ }
+ q.push(.{ .eof = .{ .pane = pane, .gen = gen, .failure = failure } });
+ }
+
+ /// Starts `e`, its answer let in and every older one's turned away.
+ pub fn startLsp(q: *Inbox, core: *pardes.Pardes, e: Lsp.Request) void {
+ q.lock();
+ q.lsp_id = e.id;
+ q.discardLsp();
+ q.unlock();
+ // The old query's waits are cancelation points: its join is short, and
+ // what it still delivers carries an id the queue now rejects.
+ Lsp.start(q.io, q.lsp_allocator, core, &q.lsp_task, e, q, pushLspRows);
+ }
+
+ fn pushLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void {
+ const q: *Inbox = @ptrCast(@alignCast(ctx orelse return));
+ q.push(.{ .lsp = .{ .id = id, .rows = rows } });
+ }
+
+ /// The language client's status line (lsp.setStatusSink).
+ pub fn lspStatus(ctx: ?*anyopaque, text: []const u8) void {
+ const q: *Inbox = @ptrCast(@alignCast(ctx orelse return));
+ const copy = q.lsp_allocator.dupe(u8, text) catch return;
+ q.push(.{ .lsp_status = copy });
+ }
+
+ /// A `|` job's answer (selection_pipe.start's sink).
+ pub fn pushPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void {
+ const q: *Inbox = @ptrCast(@alignCast(ctx.?));
+ q.push(.{ .pipe = response });
+ }
+
+ /// A watched child exited (setExitWake): wake the loop, which takes it.
+ pub fn wakeForExit(ctx: ?*anyopaque) void {
+ const q: *Inbox = @ptrCast(@alignCast(ctx.?));
+ q.wake();
+ }
+};
+
pub const Lsp = struct {
pub const Request = struct {
id: u32,
diff --git a/src/macos.zig b/src/macos.zig
index 22d2db71..9378359c 100644
--- a/src/macos.zig
+++ b/src/macos.zig
@@ -128,7 +128,7 @@ const Pty = struct {
file: std.Io.File,
pid: posix.pid_t,
gen: u32,
- reader: std.Io.Future(anyerror!void),
+ reader: std.Io.Future(void),
/// A command pane's child, watched to its exit (host_io.watchExit);
/// the pty stays open until both its end of file and its exit.
cmd: host_io.CommandWatch = .{},
@@ -309,22 +309,26 @@ test "mac queued results never cancel a newer LSP request" {
.runtime = .{},
.config_arena = .init(gpa),
.prompt_rcs = .{},
+ .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = std.testing.io },
};
defer st.config_arena.deinit();
- defer st.inbox.close(gpa, std.testing.io);
+ defer st.inbox.close();
core.lspRequest(0, .status, "");
const old_id = core.lsp_wait.?.id;
core.lspRequest(0, .status, "");
const current_id = core.lsp_wait.?.id;
- st.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } };
- st.inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = old_id, .rows = try gpa.dupe(u8, "stale result\n") } });
- try std.testing.expect(drainInbox(&st));
- try std.testing.expect(st.lsp_task != null);
- try std.testing.expectEqual(current_id, st.lsp_task.?.id);
+ st.inbox.lsp_id = current_id;
+ st.inbox.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } };
+ // The inbox turns the superseded answer away.
+ st.inbox.push(.{ .lsp = .{ .id = old_id, .rows = try gpa.dupe(u8, "stale result\n") } });
+ try std.testing.expectEqual(@as(usize, 0), st.inbox.completion_len);
+ _ = drainInbox(&st);
+ try std.testing.expect(st.inbox.lsp_task != null);
+ try std.testing.expectEqual(current_id, st.inbox.lsp_task.?.id);
try std.testing.expectEqual(current_id, core.lsp_wait.?.id);
- st.inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = current_id, .rows = try gpa.dupe(u8, "") } });
+ st.inbox.push(.{ .lsp = .{ .id = current_id, .rows = try gpa.dupe(u8, "") } });
try std.testing.expect(drainInbox(&st));
- try std.testing.expect(st.lsp_task == null);
+ try std.testing.expect(st.inbox.lsp_task == null);
try std.testing.expect(core.lsp_wait == null);
}
@@ -348,9 +352,10 @@ test "mac worker setup failures finish matching LSP and pipe requests" {
.runtime = .{},
.config_arena = .init(gpa),
.prompt_rcs = .{},
+ .inbox = .{ .gpa = failing.allocator(), .lsp_allocator = failing.allocator(), .io = failing_io },
};
defer st.config_arena.deinit();
- defer st.inbox.close(gpa, std.testing.io);
+ defer st.inbox.close();
core.lspRequest(core.active, .status, "");
const req: host_io.Lsp.Request = .{
.id = core.lsp_wait.?.id,
@@ -372,7 +377,7 @@ test "mac worker setup failures finish matching LSP and pipe requests" {
pipeRequest(&st, pipe_id);
try std.testing.expect(core.pipe.wait == null);
try std.testing.expectEqualStrings("abc", pane.file.?.content);
- try std.testing.expect(st.lsp_task == null);
+ try std.testing.expect(st.inbox.lsp_task == null);
try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len);
}
}
@@ -400,264 +405,8 @@ test "mac host watcher covers file and directory vnode events, debounce, and gen
) != null);
}
-const Msg = union(enum) {
- output: struct { pane: u8, gen: u32, bytes: []u8 },
- eof: struct { pane: u8, gen: u32 },
- lsp_done: struct { id: u32, rows: ?[]u8 },
- lsp_status: []u8,
- pipe: selection_pipe.Response,
-
- fn free(m: Msg, gpa: std.mem.Allocator) void {
- switch (m) {
- .output => |o| gpa.free(o.bytes),
- .eof => {},
- .lsp_done => |d| if (d.rows) |rows| gpa.free(rows),
- .lsp_status => |t| gpa.free(t),
- .pipe => |r| {
- var response = r;
- response.deinit(gpa);
- },
- }
- }
-};
-
-const inbox_capacity = 512;
-const inbox_output_limit = inbox_capacity - pardes.MAX_PANES - selection_pipe.Tasks.capacity - 2;
-
-const MessageBatch = struct {
- items: [inbox_capacity]Msg = undefined,
- len: usize = 0,
-
- fn slice(batch: *MessageBatch) []Msg {
- return batch.items[0..batch.len];
- }
-};
-
-const Inbox = struct {
- mutex: std.Io.Mutex = .init,
- space: std.Io.Condition = .init,
- items: [inbox_capacity]Msg = undefined,
- len: usize = 0,
- closed: bool = false,
- wake_pending: std.atomic.Value(bool) = .init(false),
-
- fn removeAt(q: *Inbox, offset: usize) Msg {
- const removed = q.items[offset];
- std.mem.copyForwards(Msg, q.items[offset .. q.len - 1], q.items[offset + 1 .. q.len]);
- q.len -= 1;
- return removed;
- }
-
- fn pushOutput(q: *Inbox, gpa: std.mem.Allocator, io: std.Io, output: @FieldType(Msg, "output")) std.Io.Cancelable!void {
- errdefer gpa.free(output.bytes);
- q.mutex.lockUncancelable(io);
- defer q.mutex.unlock(io);
- while (q.len >= inbox_output_limit and !q.closed) try q.space.wait(io, &q.mutex);
- if (q.closed) return error.Canceled;
- q.items[q.len] = .{ .output = output };
- q.len += 1;
- }
-
- fn push(q: *Inbox, gpa: std.mem.Allocator, io: std.Io, m: Msg) void {
- std.debug.assert(m != .output);
- q.mutex.lockUncancelable(io);
- defer q.mutex.unlock(io);
- if (q.closed) {
- m.free(gpa);
- return;
- }
- if (m != .pipe) {
- var offset: usize = 0;
- while (offset < q.len) : (offset += 1) {
- const old = q.items[offset];
- const replaced = switch (m) {
- .eof => |end| old == .eof and old.eof.pane == end.pane,
- .lsp_done => old == .lsp_done,
- .lsp_status => old == .lsp_status,
- .output, .pipe => unreachable,
- };
- if (replaced) {
- q.removeAt(offset).free(gpa);
- break;
- }
- }
- }
- std.debug.assert(q.len < q.items.len);
- q.items[q.len] = m;
- q.len += 1;
- }
-
- fn take(q: *Inbox, io: std.Io) MessageBatch {
- q.mutex.lockUncancelable(io);
- defer q.mutex.unlock(io);
- var batch: MessageBatch = .{ .len = q.len };
- @memcpy(batch.items[0..q.len], q.items[0..q.len]);
- q.len = 0;
- q.space.broadcast(io);
- return batch;
- }
-
- fn close(q: *Inbox, gpa: std.mem.Allocator, io: std.Io) void {
- q.mutex.lockUncancelable(io);
- defer q.mutex.unlock(io);
- q.closed = true;
- for (q.items[0..q.len]) |msg| msg.free(gpa);
- q.len = 0;
- q.space.broadcast(io);
- }
-};
-
-test "mac inbox coalesces completions at the tail without reordering terminal output" {
- const gpa = std.testing.allocator;
- var inbox: Inbox = .{};
- defer inbox.close(gpa, std.testing.io);
- inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = std.math.maxInt(u32), .rows = try gpa.dupe(u8, "old result") } });
- inbox.push(gpa, std.testing.io, .{ .lsp_status = try gpa.dupe(u8, "old status") });
- try inbox.pushOutput(gpa, std.testing.io, .{ .pane = 0, .gen = std.math.maxInt(u32), .bytes = try gpa.dupe(u8, "old output") });
- inbox.push(gpa, std.testing.io, .{ .eof = .{ .pane = 0, .gen = std.math.maxInt(u32) } });
- try inbox.pushOutput(gpa, std.testing.io, .{ .pane = 0, .gen = 0, .bytes = try gpa.dupe(u8, "new output") });
- inbox.push(gpa, std.testing.io, .{ .eof = .{ .pane = 0, .gen = 0 } });
- inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = 0, .rows = try gpa.dupe(u8, "new result") } });
- inbox.push(gpa, std.testing.io, .{ .lsp_status = try gpa.dupe(u8, "new status") });
- var batch = inbox.take(std.testing.io);
- defer for (batch.slice()) |msg| msg.free(gpa);
- try std.testing.expectEqual(@as(usize, 5), batch.len);
- try std.testing.expectEqualStrings("old output", batch.items[0].output.bytes);
- try std.testing.expectEqualStrings("new output", batch.items[1].output.bytes);
- try std.testing.expectEqual(@as(u32, 0), batch.items[2].eof.gen);
- try std.testing.expectEqual(@as(u32, 0), batch.items[3].lsp_done.id);
- try std.testing.expectEqualStrings("new result", batch.items[3].lsp_done.rows.?);
- try std.testing.expectEqualStrings("new status", batch.items[4].lsp_status);
- inbox.close(gpa, std.testing.io);
- inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = 1, .rows = try gpa.dupe(u8, "closed") } });
- try std.testing.expectEqual(@as(usize, 0), inbox.len);
-}
-
-test "mac inbox admits every retained task completion when output fills the queue" {
- const gpa = std.testing.allocator;
- var inbox: Inbox = .{};
- defer inbox.close(gpa, std.testing.io);
- var tasks: selection_pipe.Tasks = .{};
- defer tasks.cancelAll(std.testing.io);
- for (0..inbox_output_limit) |_| try inbox.pushOutput(gpa, std.testing.io, .{
- .pane = 0,
- .gen = 1,
- .bytes = try gpa.dupe(u8, "output"),
- });
- for (0..pardes.MAX_PANES) |pane| inbox.push(gpa, std.testing.io, .{ .eof = .{ .pane = @intCast(pane), .gen = 1 } });
- inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = 1, .rows = try gpa.dupe(u8, "result") } });
- for (0..selection_pipe.Tasks.capacity) |index| {
- const id: u32 = @intCast(index);
- try std.testing.expect(tasks.add(.{ .id = id, .future = .{ .any_future = null, .result = {} } }));
- if (index == 0) {
- const output = try gpa.dupe(u8, "filtered");
- const outputs = gpa.dupe([]u8, &.{output}) catch |err| {
- gpa.free(output);
- return err;
- };
- inbox.push(gpa, std.testing.io, .{ .pipe = .{ .id = id, .success = true, .outputs = outputs } });
- } else inbox.push(gpa, std.testing.io, .{ .pipe = .{
- .id = id,
- .success = false,
- .outputs = &.{},
- .failure = .{ .kind = .exit, .code = 1, .stderr = try gpa.dupe(u8, "no match") },
- } });
- }
- inbox.push(gpa, std.testing.io, .{ .lsp_status = try gpa.dupe(u8, "current status") });
- try std.testing.expectEqual(inbox_capacity, inbox.len);
- var batch = inbox.take(std.testing.io);
- defer for (batch.slice()) |msg| msg.free(gpa);
- var eof_count: usize = 0;
- var lsp_count: usize = 0;
- var pipe_count: usize = 0;
- var output_count: usize = 0;
- for (batch.slice()) |msg| switch (msg) {
- .eof => eof_count += 1,
- .lsp_done => lsp_count += 1,
- .pipe => |result| {
- pipe_count += 1;
- tasks.finish(std.testing.io, result.id);
- },
- .output => |output| {
- output_count += 1;
- try std.testing.expectEqualStrings("output", output.bytes);
- },
- .lsp_status => |status| try std.testing.expectEqualStrings("current status", status),
- };
- try std.testing.expectEqual(pardes.MAX_PANES, eof_count);
- try std.testing.expectEqual(@as(usize, 1), lsp_count);
- try std.testing.expectEqual(selection_pipe.Tasks.capacity, pipe_count);
- try std.testing.expectEqual(inbox_output_limit, output_count);
- try std.testing.expectEqual(@as(usize, 0), tasks.len);
-}
-
-test "mac inbox preserves output under backpressure and wakes on take cancel and close" {
- const gpa = std.testing.allocator;
- const io = std.testing.io;
- const Producer = struct {
- fn run(q: *Inbox, done: *std.Io.Event) !void {
- defer done.set(std.testing.io);
- defer q.push(std.testing.allocator, std.testing.io, .{ .eof = .{ .pane = 0, .gen = 1 } });
- try q.pushOutput(std.testing.allocator, std.testing.io, .{
- .pane = 0,
- .gen = 1,
- .bytes = try std.testing.allocator.dupe(u8, "tail\x1b[0m"),
- });
- try q.pushOutput(std.testing.allocator, std.testing.io, .{
- .pane = 0,
- .gen = 1,
- .bytes = try std.testing.allocator.dupe(u8, "é😀"),
- });
- }
- };
- for ([_]enum { take, cancel, close }{ .take, .cancel, .close }) |action| {
- var inbox: Inbox = .{};
- defer inbox.close(gpa, io);
- for (0..inbox_output_limit) |_| try inbox.pushOutput(gpa, io, .{
- .pane = 0,
- .gen = 1,
- .bytes = try gpa.dupe(u8, "before"),
- });
- var done: std.Io.Event = .unset;
- var future = try io.concurrent(Producer.run, .{ &inbox, &done });
- defer future.cancel(io) catch {};
- for (0..1000) |_| {
- if (inbox.space.state.load(.acquire).waiters != 0) break;
- try io.sleep(.fromMilliseconds(1), .awake);
- }
- try std.testing.expectEqual(@as(u16, 1), inbox.space.state.load(.acquire).waiters);
- switch (action) {
- .take => {
- var before = inbox.take(io);
- defer for (before.slice()) |msg| msg.free(gpa);
- try std.testing.expectEqual(inbox_output_limit, before.len);
- for (before.slice()) |msg| try std.testing.expectEqualStrings("before", msg.output.bytes);
- try done.waitTimeout(io, .{ .duration = .{ .raw = .fromSeconds(2), .clock = .awake } });
- try future.await(io);
- var after = inbox.take(io);
- defer for (after.slice()) |msg| msg.free(gpa);
- try std.testing.expectEqual(@as(usize, 3), after.len);
- try std.testing.expectEqualStrings("tail\x1b[0m", after.items[0].output.bytes);
- try std.testing.expectEqualStrings("é😀", after.items[1].output.bytes);
- try std.testing.expect(after.items[2] == .eof);
- },
- .cancel => {
- try std.testing.expectError(error.Canceled, future.cancel(io));
- var batch = inbox.take(io);
- defer for (batch.slice()) |msg| msg.free(gpa);
- try std.testing.expectEqual(inbox_output_limit + 1, batch.len);
- for (batch.slice()[0..inbox_output_limit]) |msg| try std.testing.expectEqualStrings("before", msg.output.bytes);
- try std.testing.expect(batch.items[inbox_output_limit] == .eof);
- },
- .close => {
- inbox.close(gpa, io);
- try done.waitTimeout(io, .{ .duration = .{ .raw = .fromSeconds(2), .clock = .awake } });
- try std.testing.expectError(error.Canceled, future.await(io));
- try std.testing.expectEqual(@as(usize, 0), inbox.len);
- },
- }
- }
-}
+const Inbox = host_io.Inbox;
+const Msg = Inbox.Msg;
const State = struct {
ninep: ?*ninep_io.Listener = null,
@@ -681,8 +430,9 @@ const State = struct {
panel_tracks: [pardes.MAX_PANES * 2]PanelTrack = undefined,
panel_tracks_len: usize = 0,
ptys: [pardes.MAX_PANES]?Pty = @splat(null),
- inbox: Inbox = .{},
- lsp_task: ?host_io.Lsp.Task = null,
+ inbox: Inbox = undefined,
+ /// A wakeup is on its way to the runtime: the next ones are not sent.
+ wake_pending: std.atomic.Value(bool) = .init(false),
pipe_tasks: selection_pipe.Tasks = .{},
file_watches: FileWatches = .{},
gens: [pardes.MAX_PANES]u32 = @splat(0),
@@ -795,6 +545,7 @@ fn initCore(runtime: ?*const Runtime, cols_arg: u16, rows_arg: u16) !void {
.runtime = if (runtime) |r| r.* else .{},
};
const st = &state.?;
+ st.inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = io, .wake_fn = wakeHost, .wake_ctx = st };
st.ninep = ninep_io.start(io, gpa, core);
core.host = hostFor(st);
@@ -807,7 +558,7 @@ fn initCore(runtime: ?*const Runtime, cols_arg: u16, rows_arg: u16) !void {
if (st.ninep) |listener| listener.watch(st, wakeNinep) catch |err| core.reportError(0, "9p wake", err);
for (&st.ptys, 0..) |*slot, id| if (slot.*) |*pt| startReader(st, pt, @intCast(id));
- pardes.lsp.setStatusSink(st, lspStatusSink);
+ pardes.lsp.setStatusSink(&st.inbox, Inbox.lspStatus);
host_io.setExitWake(st, wakeForExit);
// From here the core is touched only inside the entry points above,
// each of which takes the turn for its own duration; between them the
@@ -829,9 +580,9 @@ export fn pardes_deinit() void {
}
pardes.lsp.setStatusSink(null, null);
host_io.setExitWake(null, null);
- if (st.lsp_task) |*t| {
+ if (st.inbox.lsp_task) |*t| {
t.future.cancel(st.io);
- st.lsp_task = null;
+ st.inbox.lsp_task = null;
}
st.pipe_tasks.cancelAll(st.io);
for (0..pardes.MAX_PANES) |pane| if (st.file_watches.entries[pane] != null) {
@@ -844,7 +595,7 @@ export fn pardes_deinit() void {
hostWatchFile(theme_watch_pane, generation, null, 0);
}
for (0..pardes.MAX_PANES) |pane| reap(st, @intCast(pane));
- st.inbox.close(st.gpa, st.io);
+ st.inbox.close();
st.file_watches.deinit(st.gpa);
if (st.cells.len > 0) st.gpa.free(st.cells);
for (st.layer_cells) |cells| st.gpa.free(cells);
@@ -1295,12 +1046,12 @@ export fn pardes_watch_changed(pane: u8, generation: u32) void {
}
fn drainInbox(st: *State) bool {
- var batch = st.inbox.take(st.io);
+ var batch = st.inbox.take();
// After this batch's output, which an exit is told behind.
defer takeExits(st);
var did = batch.len > 0;
for (batch.slice()) |msg| {
- defer msg.free(st.gpa);
+ defer msg.deinit(st.gpa, st.gpa);
switch (msg) {
.output => |o| {
if (st.gens[o.pane] != o.gen) continue;
@@ -1330,11 +1081,11 @@ fn drainInbox(st: *State) bool {
reap(st, e.pane);
st.core.update(.{ .eof = .{ .pane = e.pane } });
},
- .lsp_done => |d| {
+ .lsp => |d| {
st.core.update(.{ .lsp_resp = .{ .id = d.id, .rows = d.rows } });
- if (st.lsp_task) |*t| if (t.id == d.id) {
+ if (st.inbox.lsp_task) |*t| if (t.id == d.id) {
t.future.await(st.io);
- st.lsp_task = null;
+ st.inbox.lsp_task = null;
};
},
.lsp_status => |text| {
@@ -1350,6 +1101,8 @@ fn drainInbox(st: *State) bool {
} });
st.pipe_tasks.finish(st.io, value.id);
},
+ // The SDL shell's: this one's file watches and 9P are its own.
+ .files_changed, .fs_ready => {},
}
}
for (0..pardes.MAX_PANES) |pane| {
@@ -1367,7 +1120,7 @@ export fn pardes_tick() bool {
pardes.turn.wake();
defer pardes.turn.rest();
const st = &(state orelse return false);
- st.inbox.wake_pending.store(false, .release);
+ st.wake_pending.store(false, .release);
var did = drainInbox(st);
if (st.ninep) |listener| {
const drained = listener.tick();
@@ -1396,17 +1149,17 @@ fn restoreCore(st: *State) bool {
st.core.reportError(st.core.active, "Restore", err);
return false;
};
- if (st.lsp_task) |*task| {
+ if (st.inbox.lsp_task) |*task| {
task.future.cancel(st.io);
- st.lsp_task = null;
+ st.inbox.lsp_task = null;
}
st.pipe_tasks.cancelAll(st.io);
for (0..pardes.MAX_PANES) |pane| {
reap(st, @intCast(pane));
st.gens[pane] +%= 1;
}
- var stale = st.inbox.take(st.io);
- for (stale.slice()) |msg| msg.free(st.gpa);
+ var stale = st.inbox.take();
+ for (stale.slice()) |msg| msg.deinit(st.gpa, st.gpa);
for (0..watch_slot_count) |pane| if (st.file_watches.entries[pane] != null) {
const id: u8 = @intCast(pane);
const generation = st.file_watches.stop(st.gpa, id);
@@ -1447,13 +1200,14 @@ test "mac Restore keeps host state and rejects callbacks from the old core" {
.runtime = .{},
.config_arena = .init(gpa),
.prompt_rcs = .{},
+ .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = std.testing.io },
.frame_len = 7,
.rotate_coasting = true,
};
defer st.core.deinit();
defer st.config_arena.deinit();
defer st.file_watches.deinit(gpa);
- defer st.inbox.close(gpa, std.testing.io);
+ defer st.inbox.close();
const marker = try st.config_arena.allocator().dupe(u8, "host configuration");
_ = try core.setTestFile("saved body\n");
const old_serial = core.panes[0].?.serial;
@@ -1472,19 +1226,21 @@ test "mac Restore keeps host state and rejects callbacks from the old core" {
core.lspRequest(0, .status, "");
const old_request = core.lsp_wait.?.id;
- st.lsp_task = .{ .id = old_request, .future = .{ .any_future = null, .result = {} } };
- st.inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = old_request, .rows = try gpa.dupe(u8, "old result") } });
+ st.inbox.lsp_id = old_request;
+ st.inbox.lsp_task = .{ .id = old_request, .future = .{ .any_future = null, .result = {} } };
+ st.inbox.push(.{ .lsp = .{ .id = old_request, .rows = try gpa.dupe(u8, "old result") } });
for (0..selection_pipe.Tasks.capacity) |index| {
const id: u32 = @intCast(index);
try std.testing.expect(st.pipe_tasks.add(.{ .id = id, .future = .{ .any_future = null, .result = {} } }));
- st.inbox.push(gpa, std.testing.io, .{ .pipe = .{
+ st.inbox.push(.{ .pipe = .{
.id = id,
.success = false,
.outputs = &.{},
.failure = .{ .kind = .exit, .code = 1, .stderr = try gpa.dupe(u8, "old filter failure") },
} });
}
- try st.inbox.pushOutput(gpa, std.testing.io, .{ .pane = 0, .gen = 23, .bytes = try gpa.dupe(u8, "old PTY output") });
+ st.inbox.acceptReader(0, 23);
+ _ = st.inbox.pushOutput(0, 23, try gpa.dupe(u8, "old PTY output"));
core.update(.{ .command = try std.fmt.bufPrint(&command, "Restore .zig-cache/tmp/{s}/restore.zon", .{tmp.sub_path}) });
try std.testing.expect(restoreCore(&st));
try std.testing.expect(st.core != core);
@@ -1498,17 +1254,18 @@ test "mac Restore keeps host state and rejects callbacks from the old core" {
try std.testing.expect(st.file_watches.generations[0] > old_watch);
try std.testing.expectEqual(@as(usize, 0), st.inbox.len);
try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len);
- try std.testing.expect(st.lsp_task == null);
+ try std.testing.expect(st.inbox.lsp_task == null);
try std.testing.expect(!drainInbox(&st));
try std.testing.expect(!st.cwd_stale[0]);
st.core.lspRequest(0, .status, "");
const new_request = st.core.lsp_wait.?.id;
try std.testing.expect(new_request > old_request);
- st.lsp_task = .{ .id = new_request, .future = .{ .any_future = null, .result = {} } };
- st.inbox.push(gpa, std.testing.io, .{ .lsp_done = .{ .id = new_request, .rows = &.{} } });
+ st.inbox.lsp_id = new_request;
+ st.inbox.lsp_task = .{ .id = new_request, .future = .{ .any_future = null, .result = {} } };
+ st.inbox.push(.{ .lsp = .{ .id = new_request, .rows = &.{} } });
try std.testing.expect(drainInbox(&st));
- try std.testing.expect(st.lsp_task == null);
+ try std.testing.expect(st.inbox.lsp_task == null);
try std.testing.expect(st.core.lsp_wait == null);
const pane = st.core.panes[0].?;
@@ -1524,7 +1281,7 @@ test "mac Restore keeps host state and rejects callbacks from the old core" {
gpa.free(output);
return err;
};
- st.inbox.push(gpa, std.testing.io, .{ .pipe = .{ .id = pipe_id, .success = true, .outputs = outputs } });
+ st.inbox.push(.{ .pipe = .{ .id = pipe_id, .success = true, .outputs = outputs } });
try std.testing.expect(drainInbox(&st));
try std.testing.expect(st.core.pipe.wait == null);
try std.testing.expectEqual(@as(usize, 0), st.pipe_tasks.len);
@@ -1544,19 +1301,17 @@ test "mac Restore cancels a PTY reader waiting for output capacity" {
.runtime = .{},
.config_arena = .init(gpa),
.prompt_rcs = .{},
+ .inbox = .{ .gpa = gpa, .lsp_allocator = gpa, .io = io },
};
defer st.core.deinit();
defer st.config_arena.deinit();
defer st.file_watches.deinit(gpa);
- defer st.inbox.close(gpa, io);
+ defer st.inbox.close();
try dump.dumpState(st.core);
try tmp.dir.writeFile(io, .{ .sub_path = "restore.zon", .data = st.core.dump_out.? });
while (st.core.nextEffect()) |_| {}
- for (0..inbox_output_limit) |_| try st.inbox.pushOutput(gpa, io, .{
- .pane = 0,
- .gen = 1,
- .bytes = try gpa.dupe(u8, "retained output"),
- });
+ st.inbox.acceptReader(1, 1);
+ for (0..Inbox.output_capacity) |_| _ = st.inbox.pushOutput(1, 1, try gpa.dupe(u8, "retained output"));
const child = try host_io.forkShell(null, 0, &st.prompt_rcs, "/bin/sh", "", 12, 40, null);
st.gens[0] = 1;
st.ptys[0] = .{ .file = child.file, .pid = child.pid, .gen = 1, .reader = .{ .any_future = null, .result = {} } };
@@ -1580,9 +1335,10 @@ test "mac Restore cancels a PTY reader waiting for output capacity" {
try std.testing.expect(st.ptys[0] == null);
try std.testing.expectEqual(@as(usize, 0), st.inbox.len);
try std.testing.expectEqual(@as(u16, 0), st.inbox.space.state.load(.acquire).waiters);
- try st.inbox.pushOutput(gpa, io, .{ .pane = 0, .gen = st.gens[0], .bytes = try gpa.dupe(u8, "fresh output") });
- var batch = st.inbox.take(io);
- defer for (batch.slice()) |msg| msg.free(gpa);
+ st.inbox.acceptReader(0, st.gens[0]);
+ _ = st.inbox.pushOutput(0, st.gens[0], try gpa.dupe(u8, "fresh output"));
+ var batch = st.inbox.take();
+ defer for (batch.slice()) |msg| msg.deinit(gpa, gpa);
try std.testing.expectEqual(@as(usize, 1), batch.len);
try std.testing.expectEqualStrings("fresh output", batch.items[0].output.bytes);
}
@@ -2374,31 +2130,12 @@ fn hostFor(st: *State) pardes.Host {
fn lspRequest(ctx: ?*anyopaque, req: host_io.Lsp.Request) void {
const st = hostState(ctx);
- host_io.Lsp.start(st.io, st.gpa, st.core, &st.lsp_task, req, st, deliverLspRows);
+ st.inbox.startLsp(st.core, req);
}
fn pipeRequest(ctx: ?*anyopaque, id: u32) void {
const st = hostState(ctx);
- selection_pipe.start(st.io, st.gpa, st.core, &st.pipe_tasks, id, st, deliverPipe);
-}
-
-fn deliverPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void {
- const st: *State = @ptrCast(@alignCast(ctx orelse return));
- st.inbox.push(st.gpa, st.io, .{ .pipe = response });
- wake(st);
-}
-
-fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void {
- const st: *State = @ptrCast(@alignCast(ctx orelse return));
- st.inbox.push(st.gpa, st.io, .{ .lsp_done = .{ .id = id, .rows = rows } });
- wake(st);
-}
-
-fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void {
- const st: *State = @ptrCast(@alignCast(ctx orelse return));
- const copy = st.gpa.dupe(u8, text) catch return;
- st.inbox.push(st.gpa, st.io, .{ .lsp_status = copy });
- wake(st);
+ selection_pipe.start(st.io, st.gpa, st.core, &st.pipe_tasks, id, &st.inbox, Inbox.pushPipe);
}
fn hostState(ctx: ?*anyopaque) *State {
@@ -2525,7 +2262,7 @@ fn openLink(_: ?*anyopaque, url: []const u8) void {
}
fn startReader(st: *State, pt: *Pty, id: u8) void {
- pt.reader = st.io.concurrent(readPty, .{ st, st.io, pt.file, id, pt.gen }) catch |err| {
+ pt.reader = st.inbox.startReader(pt.file, id, pt.gen) catch |err| {
log.err("pane {d} has no reader ({t}); it will not show output", .{ id, err });
return;
};
@@ -2542,29 +2279,20 @@ fn closePty(ctx: ?*anyopaque, pane: u8) void {
fn reap(st: *State, pane: u8) void {
var pt = st.ptys[pane] orelse return;
st.ptys[pane] = null;
- pt.reader.cancel(st.io) catch {};
+ // The inbox first: a reader waiting for space there wakes and returns.
+ st.inbox.cancelReader(pane, pt.gen);
+ pt.reader.cancel(st.io);
_ = libc.close(pt.file.handle);
host_io.retireShell(pt.pid);
}
-fn readPty(st: *State, io: std.Io, pty: std.Io.File, id: u8, gen: u32) anyerror!void {
- // 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: [0x10000]u8 = undefined;
- while (true) {
- const n = pty.readStreaming(io, &.{&buf}) catch break;
- if (n == 0) break;
- const bytes = st.gpa.dupe(u8, buf[0..n]) catch break;
- st.inbox.pushOutput(st.gpa, io, .{ .pane = id, .gen = gen, .bytes = bytes }) catch break;
- wake(st);
- }
- st.inbox.push(st.gpa, st.io, .{ .eof = .{ .pane = id, .gen = gen } });
- wake(st);
+fn wakeHost(ctx: ?*anyopaque) void {
+ wake(@ptrCast(@alignCast(ctx.?)));
}
fn wake(st: *State) void {
const cb = st.runtime.wakeup orelse return;
- if (st.inbox.wake_pending.swap(true, .acq_rel)) return;
+ if (st.wake_pending.swap(true, .acq_rel)) return;
cb(st.runtime.userdata);
}
@@ -2750,9 +2478,9 @@ fn takeRotationNotches(lag: *f32, degrees: f32) i32 {
fn expectSameAbi(comptime C: type, comptime Z: type) !void {
const c_fn = @typeInfo(C).@"fn";
const z_fn = @typeInfo(Z).@"fn";
- try std.testing.expectEqual(c_fn.params.len, z_fn.params.len);
- inline for (c_fn.params, z_fn.params) |cp, zp|
- try std.testing.expectEqual(@sizeOf(cp.type.?), @sizeOf(zp.type.?));
+ try std.testing.expectEqual(c_fn.param_types.len, z_fn.param_types.len);
+ inline for (c_fn.param_types, z_fn.param_types) |cp, zp|
+ try std.testing.expectEqual(@sizeOf(cp.?), @sizeOf(zp.?));
try std.testing.expectEqual(@sizeOf(c_fn.return_type.?), @sizeOf(z_fn.return_type.?));
}
@@ -2844,13 +2572,8 @@ test "pardes.h matches the Zig boundary" {
inline for (@typeInfo(PanelBox).@"struct".field_names) |name|
try expectEqual(@offsetOf(c.pardes_panel_box_s, name), @offsetOf(PanelBox, name));
try expectEqual(@sizeOf(c.pardes_panel_track_s), @sizeOf(PanelTrack));
- inline for (@typeInfo(PanelTrack).@"struct".field_names) |name| {
- const c_name = comptime if (std.mem.eql(u8, name, "motion"))
- "reserved0"
- else
- name;
- try expectEqual(@offsetOf(c.pardes_panel_track_s, c_name), @offsetOf(PanelTrack, name));
- }
+ inline for (@typeInfo(PanelTrack).@"struct".field_names) |name|
+ try expectEqual(@offsetOf(c.pardes_panel_track_s, name), @offsetOf(PanelTrack, name));
try expectEqual(@sizeOf(c.pardes_image_s), @sizeOf(Image));
inline for (@typeInfo(Image).@"struct".field_names) |name|
try expectEqual(@offsetOf(c.pardes_image_s, name), @offsetOf(Image, name));