From b8f076b63d6fcbb01539d5541520a32bccc0e219 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 23 Oct 2026 11:00:49 -0300 Subject: 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 --- src/host_io.zig | 54 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 24 deletions(-) (limited to 'src/host_io.zig') diff --git a/src/host_io.zig b/src/host_io.zig index 2c736e20..a17414fb 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -50,16 +50,10 @@ pub const Host = struct { }; }; -/// The shells' monotonic clock in ns, the one `Host.now` answers with. -/// PARDES_TEST_CLOCK makes a shell answer a virtual clock instead, moved -/// only when a timed wait runs out -- straight to the core's next wake -- so -/// the snapshot harness sees the same frames whatever the machine's load. -pub fn monotonicNs() u64 { - var ts: libc.timespec = undefined; - if (libc.clock_gettime(.MONOTONIC, &ts) != 0) return 0; - return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec)); -} - +/// PARDES_TEST_CLOCK makes a shell's `Host.now` answer a virtual clock +/// instead of `std.Io.Clock.awake`, moved only when a timed wait runs out -- +/// straight to the core's next wake -- so the snapshot harness sees the same +/// frames whatever the machine's load. pub fn testClock() ?u64 { return if (libc.getenv("PARDES_TEST_CLOCK")) |v| (if (std.mem.span(v).len != 0) 0 else null) else null; } @@ -131,7 +125,7 @@ pub const Lsp = struct { pub const Task = struct { id: u32, - future: std.Io.Future(anyerror!void), + future: std.Io.Future(void), }; /// One language query, owned by the worker that runs it. @@ -227,6 +221,26 @@ pub const Lsp = struct { // Null is failure; the receiver frees non-null rows with the job's allocator. pub const Deliver = *const fn (ctx: ?*anyopaque, id: u32, rows: ?[]u8) void; + /// Start `req` on a worker that hands its rows to `deliver`, the query + /// in `task` canceled first. One that cannot start is answered (no rows) + /// and reported here. + pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, task: *?Task, req: Request, ctx: ?*anyopaque, deliver: Deliver) void { + if (task.*) |*old| { + old.future.cancel(io); + task.* = null; + } + const job = snapshot(gpa, core, req) catch |err| { + core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); + return core.reportError(req.pane, "lsp", err); + }; + const future = io.concurrent(work, .{ io, gpa, job, ctx, deliver }) catch |err| { + job.free(gpa); + core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); + return core.reportError(req.pane, "lsp", err); + }; + task.* = .{ .id = req.id, .future = future }; + } + pub fn work(io: std.Io, gpa: std.mem.Allocator, job: *Job, ctx: ?*anyopaque, deliver: Deliver) void { defer job.free(gpa); // The query's memory is the fixed region memory.zig reserved at @@ -1910,10 +1924,8 @@ test "a terminal is where its shell is: under a wrapper the foreground shell, ne try std.testing.expect(sh.waitText("pardes-in-", 5_000)); // printf runs before the inner shell takes the foreground. var tries: usize = 0; - while (ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) == null and tries < 50) : (tries += 1) { - const ts: libc.timespec = .{ .sec = 0, .nsec = 20 * std.time.ns_per_ms }; - _ = libc.nanosleep(&ts, null); - } + while (ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) == null and tries < 50) : (tries += 1) + try std.testing.io.sleep(.fromMilliseconds(20), .awake); try std.testing.expectEqualStrings(start, shellCwd(child.pid, &result) orelse return error.MissingCwd); try std.testing.expectEqualStrings(inner, ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) orelse return error.MissingCwd); } @@ -2694,18 +2706,12 @@ fn haveFile(path: [*:0]const u8) bool { return true; } -pub fn nowMs() i64 { - var ts: libc.timespec = undefined; - _ = libc.clock_gettime(.MONOTONIC, &ts); - return @as(i64, @intCast(ts.sec)) * 1000 + @divFloor(@as(i64, @intCast(ts.nsec)), 1_000_000); +fn nowMs() i64 { + return std.Io.Clock.awake.now(std.testing.io).toMilliseconds(); } fn sleepMs(ms: i64) void { - const ts = libc.timespec{ - .sec = @intCast(@divFloor(ms, 1000)), - .nsec = @intCast(@mod(ms, 1000) * 1_000_000), - }; - _ = libc.nanosleep(&ts, null); + std.testing.io.sleep(.fromMilliseconds(ms), .awake) catch {}; } test "an idle prompt is free, a foreground job takes the tty, and Ctrl-C hands it back" { -- cgit v1.3