summaryrefslogtreecommitdiff
path: root/src/tty/tty.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-23 11:00:49 -0300
committerGabriel Schneider <[email protected]>2026-10-23 17:10:58 -0300
commitb8f076b63d6fcbb01539d5541520a32bccc0e219 (patch)
treea7ecf78a52b84f1ead885094540ed88cb4baa296 /src/tty/tty.zig
parent0073d05329335dbb0eb85f5c3044e42805d144e5 (diff)
downloadpardes-b8f076b63d6fcbb01539d5541520a32bccc0e219.tar.gz
pardes-b8f076b63d6fcbb01539d5541520a32bccc0e219.zip
Audit: the hosts' wire on std.Io Reader/Writer, Lsp and selection_pipe deliver through a sink, std.Io clock and sleep
wire.zig reads and writes through std.Io.Reader/Writer instead of its own framing helpers; host_io.Lsp.start and selection_pipe.start take a callback sink, so each shell keeps no copy of the worker plumbing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/tty/tty.zig')
-rw-r--r--src/tty/tty.zig96
1 files changed, 25 insertions, 71 deletions
diff --git a/src/tty/tty.zig b/src/tty/tty.zig
index afc0dbf0..5eecb3e9 100644
--- a/src/tty/tty.zig
+++ b/src/tty/tty.zig
@@ -755,7 +755,6 @@ test "terminal input cancellation joins blocked reads and queued EOF" {
}
}
-const PipeTask = selection_pipe.Tasks.Task;
const PipeTasks = selection_pipe.Tasks;
const Pty = struct {
@@ -1351,7 +1350,7 @@ fn localSession(
slot.* = null;
};
if (sh.lsp_task) |*t| {
- t.future.cancel(io) catch {};
+ t.future.cancel(io);
sh.lsp_task = null;
}
sh.pipe_tasks.cancelAll(io);
@@ -1417,7 +1416,7 @@ fn localSession(
break :blk;
};
if (sh.lsp_task) |*task| {
- task.future.cancel(io) catch {};
+ task.future.cancel(io);
sh.lsp_task = null;
}
sh.pipe_tasks.cancelAll(io);
@@ -1562,7 +1561,7 @@ const Shell = struct {
fn clockNow(ctx: ?*anyopaque) u64 {
const s = of(ctx);
- return s.test_clock orelse host_io.monotonicNs();
+ return s.test_clock orelse @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
}
fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void {
@@ -1700,7 +1699,7 @@ const Shell = struct {
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) catch {};
+ t.future.await(s.io);
s.lsp_task = null;
};
},
@@ -1758,7 +1757,7 @@ const Shell = struct {
s.tracks = canonical.panelTracks();
// The last frame is not answered yet: this one stays owed, and the
// answer's wake draws the state as it is then.
- if (FrameAck.holds(host_io.monotonicNs(), s.io)) {
+ if (FrameAck.holds(@intCast(std.Io.Clock.awake.now(s.io).nanoseconds), s.io)) {
s.core.present_skipped = true;
return;
}
@@ -1868,7 +1867,7 @@ const Shell = struct {
const tz_render = tracy.zone(@src(), "vx.render");
const before = s.tty.tty_writer.pos + s.tty.writer().end;
vx.render(s.tty.writer()) catch {};
- if (s.tty.tty_writer.pos + s.tty.writer().end != before) FrameAck.ask(s.tty.writer(), host_io.monotonicNs());
+ if (s.tty.tty_writer.pos + s.tty.writer().end != before) FrameAck.ask(s.tty.writer(), @intCast(std.Io.Clock.awake.now(s.io).nanoseconds));
tz_render.end();
}
@@ -2011,20 +2010,12 @@ const Shell = struct {
s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } });
return s.core.reportError(req.pane, "lsp", error.WorkersUnavailable);
}
- const job = host_io.Lsp.snapshot(s.lsp_gpa, s.core, req) catch |err| {
- s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } });
- return s.core.reportError(req.pane, "lsp", err);
- };
- if (s.lsp_task) |*old| {
- old.future.cancel(s.io) catch {};
- s.lsp_task = null;
- }
- const future = s.io.concurrent(lspWorker, .{ s.io, s.lsp_gpa, job, s.loop }) catch |err| {
- job.free(s.lsp_gpa);
- s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } });
- return s.core.reportError(req.pane, "lsp", err);
- };
- s.lsp_task = .{ .id = req.id, .future = future };
+ 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);
}
fn pipe(ctx: ?*anyopaque, id: u32) void {
@@ -2033,21 +2024,13 @@ const Shell = struct {
s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } });
return s.core.reportError(s.core.active, "pipe", error.WorkersUnavailable);
}
- if (s.pipe_tasks.full()) {
- s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } });
- return;
- }
- const view = s.core.pipe.pipeRequest(id) orelse return;
- const job = selection_pipe.Job.copy(s.gpa, view) catch |err| {
- s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } });
- return s.core.reportError(s.core.active, "pipe", err);
- };
- const future = s.io.concurrent(pipeWorker, .{ s.io, s.gpa, job, s.loop }) catch |err| {
- job.deinit(s.gpa);
- s.core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } });
- return s.core.reportError(s.core.active, "pipe", err);
- };
- std.debug.assert(s.pipe_tasks.add(.{ .id = id, .future = future }));
+ selection_pipe.start(s.io, s.gpa, s.core, &s.pipe_tasks, id, s, deliverPipe);
+ }
+
+ fn deliverPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void {
+ const s = of(ctx);
+ var owned = response;
+ s.loop.postEvent(.{ .pipe_done = owned }) catch owned.deinit(s.gpa);
}
};
@@ -2153,23 +2136,6 @@ fn requestClipboard(vx: *vaxis.Vaxis, tty: *vaxis.Tty) void {
vx.requestSystemClipboard(tty.writer()) catch {};
}
-fn lspWorker(io: std.Io, allocator: std.mem.Allocator, job: *host_io.Lsp.Job, loop: *Loop) anyerror!void {
- var sink: LspRowSink = .{ .allocator = allocator, .loop = loop };
- host_io.Lsp.work(io, allocator, job, &sink, LspRowSink.take);
-}
-
-const LspRowSink = struct {
- allocator: std.mem.Allocator,
- loop: *Loop,
-
- fn take(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void {
- const s: *LspRowSink = @ptrCast(@alignCast(ctx orelse return));
- s.loop.postEvent(.{ .lsp_done = .{ .id = id, .rows = rows } }) catch {
- if (rows) |owned| s.allocator.free(owned);
- };
- }
-};
-
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;
@@ -2177,17 +2143,6 @@ fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void {
if (!posted) s.lsp_gpa.free(copy);
}
-fn pipeWorker(
- io: std.Io,
- gpa: std.mem.Allocator,
- job: *selection_pipe.Job,
- loop: *Loop,
-) anyerror!void {
- defer job.deinit(gpa);
- var response = selection_pipe.runJob(gpa, io, job);
- loop.postEvent(.{ .pipe_done = response }) catch response.deinit(gpa);
-}
-
fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void {
if (comptime builtin.os.tag != .linux) {
while (file_watch.wait(fd)) loop.postEvent(.files_changed) catch break;
@@ -2254,9 +2209,9 @@ fn wakeWatch(io: std.Io, loop: *Loop) void {
fn timeWake(io: std.Io, seen: *u32) bool {
while (true) {
const ms = wake_ms.load(.acquire);
- const deadline = host_io.monotonicNs() + @as(u64, ms) * std.time.ns_per_ms;
+ const deadline = @as(u64, @intCast(std.Io.Clock.awake.now(io).nanoseconds)) + @as(u64, ms) * std.time.ns_per_ms;
while (true) {
- const now = host_io.monotonicNs();
+ const now: u64 = @intCast(std.Io.Clock.awake.now(io).nanoseconds);
if (now >= deadline) return true;
io.futexWaitTimeout(u32, &wake_gen.raw, seen.*, .{ .duration = .{
.raw = .fromNanoseconds(@intCast(deadline - now)),
@@ -2280,14 +2235,13 @@ test "a short wake request cuts short a long one still sleeping" {
fn run(timer_io: std.Io, start_seen: u32, done: *std.atomic.Value(u64)) void {
var local = start_seen;
_ = timeWake(timer_io, &local);
- done.store(host_io.monotonicNs(), .release);
+ done.store(@intCast(std.Io.Clock.awake.now(timer_io).nanoseconds), .release);
}
};
var done: std.atomic.Value(u64) = .init(0);
- const start = host_io.monotonicNs();
+ const start: u64 = @intCast(std.Io.Clock.awake.now(io).nanoseconds);
const thread = try std.Thread.spawn(.{}, Timer.run, .{ io, seen, &done });
- var pause: std.c.timespec = .{ .sec = 0, .nsec = 50 * std.time.ns_per_ms };
- _ = std.c.nanosleep(&pause, null);
+ try io.sleep(.fromMilliseconds(50), .awake);
requestWake(io, 16);
thread.join();
// The 16 ms request lands about 66 ms in: well before the 800 ms one
@@ -2898,7 +2852,7 @@ const Attach = struct {
fn send(a: *Attach, ev: pardes.Event) ?AttachEnd {
a.client.send(.{ .event = ev }) catch |err| switch (err) {
- error.Overlong, error.NoSpace => return null,
+ error.Overlong, error.WriteFailed => return null,
else => return .{ .lost = err },
};
return null;