diff options
Diffstat (limited to 'src/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 382 |
1 files changed, 46 insertions, 336 deletions
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); } |
