diff options
Diffstat (limited to 'src/host_io.zig')
| -rw-r--r-- | src/host_io.zig | 318 |
1 files changed, 318 insertions, 0 deletions
diff --git a/src/host_io.zig b/src/host_io.zig index a17414fb..b32fff55 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -3,6 +3,7 @@ const std = @import("std"); const posix = std.posix; const libc = std.c; const pardes = @import("pardes.zig"); +const selection_pipe = @import("selection_pipe.zig"); const ninep_io = @import("9p_io.zig"); const filesystem = @import("fs.zig"); const file_watch = @import("file_watch.zig"); @@ -114,6 +115,323 @@ pub const Fallback = struct { } }; +/// What a pixel host's loop takes from its workers, in order: each pane's +/// pty output (a reader waits while `output_capacity` is full, so output is +/// never dropped or reordered), its end of file, the language client's +/// answers and status and the `|` jobs' answers (those kept apart, so a +/// full output queue never holds them up), and file-watch news. Output and +/// end of file of a reader that was canceled or replaced, and an answer to +/// a language query since superseded, are dropped here. +pub const Inbox = struct { + 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, + files_changed, + fs_ready, + + 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); + }, + .eof, .files_changed, .fs_ready => {}, + } + } + }; + + pub const capacity = 512; + pub const output_capacity = capacity - pardes.MAX_PANES; + + pub const Batch = struct { + items: [capacity + 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: [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 => { + 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 pushPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void { + const q: *Inbox = @ptrCast(@alignCast(ctx.?)); + q.push(.{ .pipe = response }); + } + + /// 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(); + } +}; + pub const Lsp = struct { pub const Request = struct { id: u32, |
