const std = @import("std"); const libc = std.c; const pardes = @import("core"); const selection_pipe = @import("core").selection_pipe; const host_io = @import("host_io.zig"); const KindJob = host_io.KindJob; const Lsp = host_io.Lsp; const Inbox = @This(); 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, /// A streaming search's rows (pardes.Stream), of whatever generation: /// the core drops a stale one. search: pardes.Stream.Batch, /// How a handover went (Handover.zig): `words` is where it went, or /// why it did not; gpa-owned. handed: struct { seq: u32, ok: bool, words: []u8 }, /// A kind's job done on a worker (KindJob), never dropped. kind_job: *KindJob, files_changed, fs_ready, /// The daemon asks for the window again (`pardes-gui --attach`). window, /// A shell a dead session left, its pty's master (Adopt); pid 0 is /// the last of them told. adopted: struct { pid: libc.pid_t, fd: c_int }, 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); }, .search => |b| b.deinit(gpa), .handed => |h| gpa.free(h.words), .kind_job => |j| j.drop(), .adopted => |a| if (a.fd >= 0) { _ = libc.close(a.fd); }, .eof, .files_changed, .fs_ready, .window => {}, } } }; pub const capacity = 512; pub const output_capacity = capacity - pardes.MAX_PANES; pub const Batch = struct { items: [capacity + 2 * 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: [2 * 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, .kind_job => { 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 pushJob(ctx: ?*anyopaque, j: *KindJob) void { const q: *Inbox = @ptrCast(@alignCast(ctx.?)); q.push(.{ .kind_job = j }); } pub fn pushPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void { const q: *Inbox = @ptrCast(@alignCast(ctx.?)); q.push(.{ .pipe = response }); } /// A handover's end (Handover.Job.post). pub fn pushHanded(ctx: ?*anyopaque, seq: u32, ok: bool, words: []const u8) void { const q: *Inbox = @ptrCast(@alignCast(ctx.?)); const copy = q.gpa.dupe(u8, words) catch return; q.push(.{ .handed = .{ .seq = seq, .ok = ok, .words = copy } }); } /// A streaming search's batch (pardes.Stream.start's sink). Never /// dropped for want of room, as output is not: the worker waits for /// room, a wait its cancel ends (false: it stops). pub fn pushSearch(ctx: ?*anyopaque, batch: pardes.Stream.Batch) bool { const q: *Inbox = @ptrCast(@alignCast(ctx.?)); q.lock(); while (!q.closed and q.len >= output_capacity) { q.waiting += 1; q.space.wait(q.io, &q.mutex) catch { q.waiting -= 1; q.unlock(); batch.deinit(q.gpa); return false; }; q.waiting -= 1; } if (q.closed) { q.unlock(); batch.deinit(q.gpa); return false; } q.items[(q.head + q.len) % q.items.len] = .{ .search = batch }; q.len += 1; q.unlock(); q.wake(); return true; } /// 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(); }