From 0073d05329335dbb0eb85f5c3044e42805d144e5 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 23 Oct 2026 17:10:58 -0300 Subject: Callstack lists every caller and pages a tree too big for one query; memory bounds and resident-size fixes Callstack walks while the server answers, counts on the message row past a second, puts test callers last at each level, and ends a tree past one query's memory (memory.limits.arena.lsp_query, a 256 MiB region taken and given back per query) in a row whose Callstack shows the next page. Grep reads whole files. New hard limits: undo text per file, image decode pixels, the LSP snapshot of other open files, one paste. Safe builds no longer fill worst-case buffers with 0xAA (memory.unfilled), big regions are kept off transparent huge pages, and malloc_trim runs after big results. Also: Look's git show pages into a +Diff, and dropped marks in File. Co-Authored-By: Claude Opus 5.5 --- src/gui/gui.zig | 329 ++++++++++++++++++++------------------------------------ 1 file changed, 118 insertions(+), 211 deletions(-) (limited to 'src/gui/gui.zig') diff --git a/src/gui/gui.zig b/src/gui/gui.zig index f9921111..9cd37b6b 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -828,18 +828,13 @@ fn emitTouchClick(t: *Touch, in: *Input, point: TouchNormPoint, button: pardes.M in.post(.{ .mouse = .{ .button = button, .kind = .release, .col = cell.col, .row = cell.row, .tag_hit = cell.tag_hit, .body_hit = cell.body_hit } }); } -fn normCell(norm: f32, win: f32, cell: f32) u16 { - const px = std.math.clamp(norm, 0.0, 1.0) * win; - return @intFromFloat(@max(0, @floor(px / cell))); -} - const Pty = struct { fd: c_int, pid: libc.pid_t, serial: u32, kill_at: i64 = 0, - reader: ?std.Thread = null, - stop: [2]c_int = .{ -1, -1 }, + /// Its reader, blocked in an Io read: `closePty` cancels it. + reader: ?std.Io.Future(void) = null, /// 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 = .{}, @@ -876,31 +871,6 @@ const Msg = union(enum) { } }; -const LspWorkers = struct { - const capacity = 16; - active: std.atomic.Value(usize) = .init(0), - - fn start(workers: *LspWorkers) bool { - var count = workers.active.load(.monotonic); - while (count < capacity) - count = workers.active.cmpxchgWeak(count, count + 1, .monotonic, .monotonic) orelse return true; - return false; - } - - fn finish(workers: *LspWorkers) void { - const previous = workers.active.fetchSub(1, .release); - std.debug.assert(previous > 0); - } - - fn wait(workers: *LspWorkers) void { - var spins: u8 = 0; - while (workers.active.load(.acquire) != 0) { - spins +%= 1; - if (spins == 0) std.Thread.yield() catch {} else std.atomic.spinLoopHint(); - } - } -}; - const PipeTasks = selection_pipe.Tasks; const queue_capacity = 512; @@ -918,10 +888,13 @@ const MessageBatch = struct { const Queue = struct { gpa: std.mem.Allocator, lsp_allocator: std.mem.Allocator, - lsp_workers: *LspWorkers, + /// 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 - mutex: libc.pthread_mutex_t = .{}, - space: libc.pthread_cond_t = .{}, + /// 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, @@ -934,13 +907,11 @@ const Queue = struct { completion_len: usize = 0, fn lock(q: *Queue) void { - // unreachable failures: pthread calls on this queue's own initialised, non-recursive - // mutex and condition fail only on misuse (EINVAL, EDEADLK), never on input. - std.debug.assert(libc.pthread_mutex_lock(&q.mutex) == .SUCCESS); + q.mutex.lockUncancelable(q.io); } fn unlock(q: *Queue) void { - std.debug.assert(libc.pthread_mutex_unlock(&q.mutex) == .SUCCESS); + q.mutex.unlock(q.io); } fn wake(q: *Queue) void { @@ -973,14 +944,14 @@ const Queue = struct { }; if (matches) q.removeAt(i).deinit(q.gpa, q.lsp_allocator) else i += 1; } - std.debug.assert(libc.pthread_cond_broadcast(&q.space) == .SUCCESS); + 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; - std.debug.assert(libc.pthread_cond_wait(&q.space, &q.mutex) == .SUCCESS); + q.space.waitUncancelable(q.io, &q.mutex); q.waiting -= 1; } if (q.closed or q.readers[pane] != gen) { @@ -1088,7 +1059,7 @@ const Queue = struct { @memcpy(batch.items[batch.len..][0..q.completion_len], q.completions[0..q.completion_len]); batch.len += q.completion_len; q.completion_len = 0; - std.debug.assert(libc.pthread_cond_broadcast(&q.space) == .SUCCESS); + q.space.broadcast(q.io); } fn discardCompletions(q: *Queue) void { @@ -1104,7 +1075,7 @@ const Queue = struct { q.closed = true; q.files_changed = false; q.readers = @splat(null); - std.debug.assert(libc.pthread_cond_broadcast(&q.space) == .SUCCESS); + q.space.broadcast(q.io); q.unlock(); q.discardCompletions(); q.lock(); @@ -1121,15 +1092,12 @@ const Queue = struct { fn deinit(q: *Queue) void { q.close(); std.debug.assert(q.waiting == 0); - std.debug.assert(libc.pthread_cond_destroy(&q.space) == .SUCCESS); - std.debug.assert(libc.pthread_mutex_destroy(&q.mutex) == .SUCCESS); } }; test "GUI completion survives a full output queue" { const gpa = std.testing.allocator; - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false, .lsp_id = 7 }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .lsp_id = 7 }; defer queue.deinit(); queue.acceptReader(0, 1); for (0..output_capacity) |_| queue.push(.{ .output = .{ @@ -1185,8 +1153,7 @@ const PtyTests = struct { test "GUI PTY backpressure retains byte order and tail before EOF" { const gpa = std.testing.allocator; - var workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); queue.acceptReader(0, 1); for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "before") } }); @@ -1220,8 +1187,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 workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); queue.acceptReader(0, 1); for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "before") } }); @@ -1252,8 +1218,7 @@ 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 workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; 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") } }); @@ -1269,8 +1234,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 workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); var ptys: [pardes.MAX_PANES]?Pty = @splat(null); var gens: [pardes.MAX_PANES]u32 = @splat(1); @@ -1320,8 +1284,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 workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; 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") } }); @@ -1365,8 +1328,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 workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); var ptys: [pardes.MAX_PANES]?Pty = @splat(null); var gens: [pardes.MAX_PANES]u32 = @splat(1); @@ -1397,8 +1359,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 workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); const fd = file_watch.init(true); if (fd < 0) return error.WatchInitFailed; @@ -1422,8 +1383,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 workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false, .lsp_id = id }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .lsp_id = id }; defer queue.deinit(); queue.acceptReader(0, 1); for (0..output_capacity) |_| queue.push(.{ .output = .{ @@ -1452,8 +1412,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 workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false, .lsp_id = 2 }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .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") } }); @@ -1469,8 +1428,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 workers: LspWorkers = .{}; - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = gpa, .lsp_workers = &workers, .sdl_wake = false, .lsp_id = 9 }; + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false, .lsp_id = 9 }; defer queue.deinit(); queue.acceptReader(0, 1); queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "retained") } }); @@ -1523,29 +1481,22 @@ fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void { q.push(.{ .lsp_status = copy }); } -fn readPtyThread(gpa: std.mem.Allocator, fd: c_int, stop: c_int, pane: u8, gen: u32, q: *Queue) void { +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) { - var fds = [_]libc.pollfd{ - .{ .fd = stop, .events = libc.POLL.IN, .revents = 0 }, - .{ .fd = fd, .events = libc.POLL.IN, .revents = 0 }, + 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 (libc.poll(&fds, fds.len, -1) < 0) { - if (libc.errno(-1) == .INTR) continue; - failure = error.PollFailed; - break; - } - if (fds[0].revents != 0) return; - if (fds[1].revents == 0) continue; - const n = libc.read(fd, &buf, buf.len); - if (n < 0) { - if (libc.errno(n) == .INTR) continue; - if (libc.errno(n) != .IO) failure = error.ReadFailed; - break; - } - if (n == 0) break; - const bytes = gpa.dupe(u8, buf[0..@intCast(n)]) catch |err| { + if (n == 0) continue; + const bytes = gpa.dupe(u8, buf[0..n]) catch |err| { failure = err; break; }; @@ -1566,14 +1517,9 @@ fn stopPipe() ![2]c_int { 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); - const stop = try stopPipe(); - errdefer for (stop) |fd| { - _ = libc.close(fd); - }; q.acceptReader(pane, gen); errdefer q.cancelReader(pane, gen); - pt.reader = try std.Thread.spawn(.{}, readPtyThread, .{ gpa, pt.fd, stop[0], pane, gen, q }); - pt.stop = stop; + 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 { @@ -1592,9 +1538,8 @@ fn watchThread(fd: c_int, stop: c_int, q: *Queue) void { } } -fn lspThread(lsp_allocator: std.mem.Allocator, workers: *LspWorkers, job: *host_io.Lsp.Job, q: *Queue) void { - defer workers.finish(); - host_io.Lsp.work(lsp_allocator, job, q, pushLspRows); +fn lspThread(io: std.Io, lsp_allocator: std.mem.Allocator, job: *host_io.Lsp.Job, q: *Queue) anyerror!void { + host_io.Lsp.work(io, lsp_allocator, job, q, pushLspRows); } fn pushLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { @@ -1602,107 +1547,52 @@ fn pushLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { q.push(.{ .lsp = .{ .id = id, .rows = rows } }); } -fn spawnLsp(core: *pardes.Pardes, q: *Queue, e: host_io.Lsp.Request) void { +fn spawnLsp(core: *pardes.Pardes, io: std.Io, q: *Queue, e: host_io.Lsp.Request) void { const lsp_allocator = q.lsp_allocator; q.lock(); q.lsp_id = e.id; q.discardLsp(); q.unlock(); - if (!q.lsp_workers.start()) { - core.update(.{ .lsp_resp = .{ .id = e.id, .rows = null } }); - return core.reportError(e.pane, "lsp", error.WorkersBusy); + // Its waits are cancelation points: this join is short, and what it + // still delivers carries an id the queue now rejects. + if (q.lsp_task) |*old| { + old.future.cancel(io) catch {}; + q.lsp_task = null; } const job = host_io.Lsp.snapshot(lsp_allocator, core, e) catch |err| { - q.lsp_workers.finish(); core.update(.{ .lsp_resp = .{ .id = e.id, .rows = null } }); return core.reportError(e.pane, "lsp", err); }; - const th = std.Thread.spawn(.{}, lspThread, .{ lsp_allocator, q.lsp_workers, job, q }) catch |err| { - q.lsp_workers.finish(); + const future = io.concurrent(lspThread, .{ io, lsp_allocator, job, q }) catch |err| { job.free(lsp_allocator); core.update(.{ .lsp_resp = .{ .id = e.id, .rows = null } }); return core.reportError(e.pane, "lsp", err); }; - th.detach(); + q.lsp_task = .{ .id = e.id, .future = future }; } -test "GUI LSP worker limit rejects before allocation and recovers after owned workers exit" { +test "GUI LSP query answers, and a newer one cancels it" { const gpa = std.testing.allocator; + const io = std.testing.io; const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); defer core.deinit(); - const pane = try core.setTestFile("abc"); - pane.body.cur_col = 1; - var workers: LspWorkers = .{}; - var gate: std.Io.Event = .unset; - var threads: [LspWorkers.capacity]?std.Thread = @splat(null); - defer { - gate.set(std.testing.io); - for (threads) |thread| if (thread) |owned| owned.join(); - workers.wait(); - } - for (&threads) |*thread| { - try std.testing.expect(workers.start()); - thread.* = std.Thread.spawn(.{}, struct { - fn run(active: *LspWorkers, ready: *std.Io.Event) void { - defer active.finish(); - ready.waitUncancelable(std.testing.io); - } - }.run, .{ &workers, &gate }) catch |err| { - workers.finish(); - return err; - }; - } - try std.testing.expectEqual(@as(usize, LspWorkers.capacity), workers.active.load(.monotonic)); - try std.testing.expect(!workers.start()); - var failing = std.testing.FailingAllocator.init(gpa, .{ .fail_index = 0 }); - var queue: Queue = .{ .gpa = gpa, .lsp_allocator = failing.allocator(), .lsp_workers = &workers, .sdl_wake = false }; + _ = try core.setTestFile("abc"); + var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .sdl_wake = false }; defer queue.deinit(); - core.lspRequest(core.active, .completion, ""); - const rejected = core.lsp_wait.?.id; - queue.lsp_id = rejected -% 1; - queue.push(.{ .lsp = .{ .id = queue.lsp_id.?, .rows = try gpa.dupe(u8, "obsolete queued result") } }); - spawnLsp(core, &queue, .{ - .id = rejected, - .kind = .completion, - .pane = @intCast(core.active), - .offset = 1, - .arg = "", - }); - try std.testing.expect(core.lsp_wait == null); - try std.testing.expectEqualStrings("abc", pane.file.?.content); - try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); - try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "workers busy") != null); - try std.testing.expectEqual(@as(usize, 0), failing.alloc_index); - try std.testing.expect(!failing.has_induced_failure); - try std.testing.expectEqual(rejected, queue.lsp_id.?); - try std.testing.expectEqual(@as(usize, 0), queue.completion_len); - queue.push(.{ .lsp = .{ .id = rejected -% 1, .rows = try gpa.dupe(u8, "obsolete late result") } }); - try std.testing.expectEqual(@as(usize, 0), queue.completion_len); - try std.testing.expectEqual(@as(usize, LspWorkers.capacity), workers.active.load(.monotonic)); - - gate.set(std.testing.io); - workers.wait(); - try std.testing.expectEqual(@as(usize, 0), workers.active.load(.monotonic)); - queue.lsp_allocator = gpa; - core.lspRequest(core.active, .status, ""); - const accepted = core.lsp_wait.?.id; - spawnLsp(core, &queue, .{ - .id = accepted, - .kind = .status, - .pane = @intCast(core.active), - .offset = 0, - .arg = "", - }); - workers.wait(); + 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 = "" }); + } + const last = core.lsp_wait.?.id; + try queue.lsp_task.?.future.await(io); + queue.lsp_task = null; var batch = queue.take(); defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); try std.testing.expectEqual(@as(usize, 1), batch.len); - try std.testing.expectEqual(accepted, batch.items[0].lsp.id); + try std.testing.expectEqual(last, batch.items[0].lsp.id); try std.testing.expect(batch.items[0].lsp.rows != null); - core.update(.{ .lsp_resp = .{ .id = accepted, .rows = batch.items[0].lsp.rows } }); + core.update(.{ .lsp_resp = .{ .id = last, .rows = batch.items[0].lsp.rows } }); try std.testing.expect(core.lsp_wait == null); - try std.testing.expectEqual(@as(usize, 0), workers.active.load(.monotonic)); - try std.testing.expectEqualStrings("abc", pane.file.?.content); } fn pipeThread(io: std.Io, gpa: std.mem.Allocator, job: *selection_pipe.Job, q: *Queue) anyerror!void { @@ -1748,11 +1638,10 @@ test "GUI worker setup failures finish matching LSP and pipe requests" { var failing = std.testing.FailingAllocator.init(gpa, .{ .fail_index = if (failure == 0) 0 else std.math.maxInt(usize), }); - var workers: LspWorkers = .{}; var queue: Queue = .{ + .io = std.testing.io, .gpa = gpa, .lsp_allocator = failing.allocator(), - .lsp_workers = &workers, .sdl_wake = false, }; defer queue.deinit(); @@ -1768,7 +1657,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, &queue, req); + spawnLsp(core, std.testing.io, &queue, 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") } }); @@ -1791,7 +1680,7 @@ test "GUI worker setup failures finish matching LSP and pipe requests" { spawnPipe(core, failing_io, failing.allocator(), &queue, &tasks, pipe_id); try std.testing.expect(core.pipe.wait == null); try std.testing.expectEqualStrings("abc", pane.file.?.content); - try std.testing.expectEqual(@as(usize, 0), workers.active.load(.monotonic)); + try std.testing.expect(queue.lsp_task == null); try std.testing.expectEqual(@as(usize, 0), tasks.len); try std.testing.expectEqual(@as(usize, 0), queue.len); } @@ -2071,22 +1960,6 @@ fn fixedCellLayout(g: *const Gui) CellLayout { return .{ .w = @floatFromInt(g.cell_w), .h = @floatFromInt(g.cell_h), .x_off = 0, .y_off = 0 }; } -fn compactTaglineLayout(g: *const Gui, origin_col: f32) CellLayout { - const body_w: f32 = @floatFromInt(g.cell_w); - const tag_w: f32 = @floatFromInt(g.tagline_width); - return .{ - .w = tag_w, - .h = @floatFromInt(g.cell_h), - .x_off = origin_col * (body_w - tag_w), - .y_off = 0, - }; -} - -fn taglineLayoutForCell(g: *const Gui, col: u16) CellLayout { - // Canonical tag cells are physical grips; text is drawn by TagLayer. - return compactTaglineLayout(g, @floatFromInt(col)); -} - const boxContains = pardes.animation.Box.contains; /// What one tier draws, or one track in tiers 2 and 3 (docs/render-pipeline.md @@ -2663,15 +2536,14 @@ fn localSession( var ptys: [pardes.MAX_PANES]?Pty = @splat(null); var gens: [pardes.MAX_PANES]u32 = @splat(0); - var lsp_workers: LspWorkers = .{}; var queue: Queue = .{ + .io = io, .gpa = gpa, .lsp_allocator = allocs.lsp, - .lsp_workers = &lsp_workers, .sdl_wake = true, }; defer { - lsp_workers.wait(); + if (queue.lsp_task) |*task| task.future.cancel(io) catch {}; queue.deinit(); } var pipe_tasks: PipeTasks = .{}; @@ -3035,15 +2907,14 @@ fn runGrid(init: std.process.Init, opts_in: pardes.Options) !void { var ptys: [pardes.MAX_PANES]?Pty = @splat(null); var gens: [pardes.MAX_PANES]u32 = @splat(0); - var lsp_workers: LspWorkers = .{}; var queue: Queue = .{ + .io = io, .gpa = gpa, .lsp_allocator = allocs.lsp, - .lsp_workers = &lsp_workers, .sdl_wake = false, }; defer { - lsp_workers.wait(); + if (queue.lsp_task) |*task| task.future.cancel(io) catch {}; queue.deinit(); } var pipe_tasks: PipeTasks = .{}; @@ -4195,13 +4066,12 @@ const Shell = struct { fn closePty(s: *Shell, pane: u8) void { const pt = if (s.ptys[pane]) |*pt| pt else return; if (pt.fd < 0) return; + // Out of the queue first, which lets go a reader waiting for room + // in it; then its read is canceled (or, at its end, it is joined). s.queue.cancelReader(pane, s.gens[pane]); - if (pt.reader) |thread| { - _ = host_io.writeFd(pt.stop[1], "x"); - thread.join(); + if (pt.reader) |*reader| { + reader.cancel(s.queue.io); // the Io it was started on (spawnReader) pt.reader = null; - for (pt.stop) |fd| _ = libc.close(fd); - pt.stop = .{ -1, -1 }; } _ = libc.close(pt.fd); pt.fd = -1; @@ -4848,11 +4718,10 @@ test "the headless grid host round-trips a yank back as a paste" { var ptys: [pardes.MAX_PANES]?Pty = @splat(null); var gens: [pardes.MAX_PANES]u32 = @splat(0); - var lsp_workers: LspWorkers = .{}; var queue: Queue = .{ + .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, - .lsp_workers = &lsp_workers, .sdl_wake = false, }; defer queue.deinit(); @@ -4933,7 +4802,7 @@ 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.queue, req); + if (s.threads_ok) return spawnLsp(s.core, s.io, s.queue, req); s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); s.core.reportError(req.pane, "lsp", error.WorkersUnavailable); } @@ -5198,6 +5067,12 @@ const Glide = struct { below: []pardes.Cell = &.{}, nabove: u16 = 0, nbelow: u16 = 0, + /// The core's lines a scroll moved, not yet measured; and what the + /// glide in flight has moved, in lines and in the picture's rows. A + /// wrapped line is several rows, and the thumb counts lines. + pending_lines: f32 = 0, + lines_moved: f32 = 0, + rows_moved: f32 = 0, /// The Motion flavour the last loop step read. motion: pardes.animation.Motion = .{}, @@ -5211,6 +5086,16 @@ const Glide = struct { glide.pending = false; glide.nabove = 0; glide.nbelow = 0; + glide.pending_lines = 0; + glide.lines_moved = 0; + glide.rows_moved = 0; + } + + /// The glide's lag in the core's lines: its rows, scaled by the lines + /// they carried. + fn lagLines(glide: *const Glide) f32 { + if (glide.rows_moved == 0) return glide.offset; + return glide.offset * glide.lines_moved / glide.rows_moved; } fn deinit(glide: *Glide, gpa: std.mem.Allocator) void { @@ -5270,6 +5155,9 @@ const Glide = struct { break :found kk; } else 0; if (shift == 0) return glide.stop(); + glide.rows_moved += @floatFromInt(shift); + glide.lines_moved += glide.pending_lines; + glide.pending_lines = 0; // The rows that left: past the top going down, the bottom going up. if (!glide.ensure(gpa)) return glide.stop(); const keep = if (glide.dir > 0) &glide.above else &glide.below; @@ -5326,6 +5214,8 @@ fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { glide.offset = 0; glide.nabove = 0; glide.nbelow = 0; + glide.lines_moved = 0; + glide.rows_moved = 0; } } const notch = g.scroll_notch; @@ -5357,6 +5247,8 @@ fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { } var left = notch; var moved = false; + const first = pane.scroll(); + defer glide.pending_lines += @floatFromInt(@abs(pane.scroll() - first)); while (left != 0) { const down = left > 0; left += if (down) -1 else 1; @@ -9347,7 +9239,7 @@ fn paneDecor(g: *Gui, gpa: std.mem.Allocator, regions: []const pardes.Region, gr // In pixels, not rows, and with the picture while it glides // or lags behind the core's rows (both in rows of the body). const lag: f32 = if (g.glide.offset != 0 and g.glide.pane == rail.owner) - g.glide.offset + g.glide.lagLines() else if (g.scroll_pane != null and g.scroll_pane.? == rail.owner) g.scroll_lag else 0; const track_px = body_y1 - y0; const size = @min(track_px, @max(layout.h, rail.thumb_span * track_px)); @@ -9983,6 +9875,12 @@ test "the scrollbar thumb moves by the pixel and follows a glide" { g.glide.pane = core.active; g.glide.offset = -1; try std.testing.expectApproxEqAbs(at10, try thumb.top(&g, core), 1e-3); + // A wrapped line: the picture moved two rows for the one line, and the + // thumb lags the line, not two. + g.glide.lines_moved = 1; + g.glide.rows_moved = 2; + g.glide.offset = -2; + try std.testing.expectApproxEqAbs(at10, try thumb.top(&g, core), 1e-3); } test "the rule between stacked panes stays in the tag band's slack, at any tagline size" { @@ -10581,12 +10479,11 @@ fn appendWorkspacePet(g: *Gui, core: ?*const pardes.Pardes, surface: *const pard return; } // Read only the rendered global row. No pane/column tags, status text or - // mouse targets are changed, and editing the workspace parks the pet. + // mouse targets are changed; editing the workspace tag leaves it be. var occupied: usize = 0; for (surface.cells[0..surface.cols], 0..) |*cell, i| { if (cellCodepoint(cell) != ' ') occupied = i + 1; } - const editing = p.header_focus and p.header_column == null; const band_top: f32 = @floatFromInt(taglineBandOffset(0, builder.win_h, g.cell_h, g.tagline_height)); const region = if (occupied >= surface.cols) null else pet.lane( occupied, @@ -10594,7 +10491,6 @@ fn appendWorkspacePet(g: *Gui, core: ?*const pardes.Pardes, surface: *const pard builder.win_w, band_top, @floatFromInt(g.tagline_height), - editing, ); // SDL's regular pump already wakes at least every 16 ms. Quantization in // sample keeps this independent of input, render rate and scene effects, @@ -10602,16 +10498,24 @@ fn appendWorkspacePet(g: *Gui, core: ?*const pardes.Pardes, surface: *const pard const pose = g.pet_state.sample(kind, c.SDL_GetTicks(), region) orelse return; const ink = overlayRgb(p.chromeTheme().tag_fg); const eye = overlayRgb(p.chromeTheme().tag_bg); + // Behind the cursor: the overlay is over the cells, so the pixels a + // cursor covers are left out. + var boxes: [max_cursors]CursorBox = undefined; + const cursors = boxes[0..cursorBoxes(g, surface, fixedCellLayout(g), builder.win_h, &boxes)]; for (0..pet.height) |y| for (0..pet.width) |x| { const color = pet.pixel(kind, pose, x, y); if (color == 0) continue; const x0 = pose.x + @as(f32, @floatFromInt(x)) * pose.scale; const y0 = pose.y + @as(f32, @floatFromInt(y)) * pose.scale; + const covered = for (cursors) |box| { + if (x0 < box.x1 and x0 + pose.scale > box.x0 and y0 < box.y1 and y0 + pose.scale > box.y0) break true; + } else false; + if (covered) continue; builder.addRect(x0, y0, x0 + pose.scale, y0 + pose.scale, if (color == 2) eye else ink); }; } -test "pet overlay stays beyond workspace text and vanishes while its tag is edited" { +test "pet overlay stays beyond workspace text and keeps going while its tag is edited" { std.testing.refAllDecls(pet); const core = try pardes.Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); defer core.deinit(); @@ -10638,10 +10542,13 @@ test "pet overlay stays beyond workspace text and vanishes while its tag is edit try std.testing.expect(x >= 20 * 8 - 0.01); try std.testing.expect(y >= 0 and y < 20); } + const before = g.pet_state; pardes.tagline.enterHeader(core, null); builder.len = 0; appendWorkspacePet(&g, core, &surface, &builder); - try std.testing.expectEqual(@as(usize, 0), builder.len); + try std.testing.expect(builder.len > 0); + try std.testing.expectEqual(before.tick, g.pet_state.tick); + try std.testing.expectEqual(before.x, g.pet_state.x); } fn cursorColor(chrome: *const pardes.Chrome, cell: *const pardes.Cell, role: pardes.FontRole) [3]u8 { -- cgit v1.3