summaryrefslogtreecommitdiff
path: root/dev/Inbox.zig
diff options
context:
space:
mode:
Diffstat (limited to 'dev/Inbox.zig')
-rw-r--r--dev/Inbox.zig75
1 files changed, 64 insertions, 11 deletions
diff --git a/dev/Inbox.zig b/dev/Inbox.zig
index aac1f7a1..d733b971 100644
--- a/dev/Inbox.zig
+++ b/dev/Inbox.zig
@@ -47,8 +47,12 @@ pub const Msg = union(enum) {
}
};
-pub const capacity = 512;
-pub const output_capacity = capacity - pardes.MAX_PANES;
+/// The ring's room: output up to `output_capacity` (a reader waits for
+/// more), an Adopt's shells beyond it (one note's: never lost to a flood),
+/// and each pane's end of file beyond those (lost to neither).
+pub const output_capacity = 512 - pardes.MAX_PANES;
+const adopt_capacity = output_capacity + pardes.MAX_PANES + 1;
+pub const capacity = adopt_capacity + pardes.MAX_PANES;
pub const Batch = struct {
items: [capacity + 2 * selection_pipe.Tasks.capacity + 2]Msg = undefined,
@@ -63,6 +67,11 @@ 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,
+/// The `|` pipes' and the addons' jobs' and draws' workers in flight, each
+/// posting its answer here (`pushPipe`, `pushJob`); the loop's thread's
+/// alone: joined as their answers land (`land`), canceled at the end.
+pipe_tasks: selection_pipe.Tasks = .{},
+job_tasks: selection_pipe.Tasks = .{},
/// 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,
@@ -77,6 +86,11 @@ len: usize = 0,
closed: bool = false,
files_changed: bool = false,
readers: [pardes.MAX_PANES]?u32 = @splat(null),
+/// Each pane's reads read out of its pty and not yet applied (`applied`):
+/// a command's exit is told only once none is (Ptys.unapplied), since
+/// the poll that asks whether its output is in (CommandWatch.due) cannot
+/// see a read the reader holds or the queue does.
+unapplied: [pardes.MAX_PANES]std.atomic.Value(u32) = @splat(.init(0)),
waiting: usize = 0,
lsp_id: ?u32 = null,
completions: [2 * selection_pipe.Tasks.capacity + 1]Msg = undefined,
@@ -101,6 +115,12 @@ pub fn acceptReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void {
q.readers[pane.index()] = gen;
}
+/// One of `pane`'s reads applied, or dropped unapplied.
+pub fn applied(q: *Inbox, pane: pardes.Pane.Id) void {
+ const n = &q.unapplied[pane.index()];
+ if (n.load(.acquire) > 0) _ = n.fetchSub(1, .acq_rel);
+}
+
pub fn cancelReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void {
q.lock();
defer q.unlock();
@@ -114,7 +134,10 @@ pub fn cancelReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void {
.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;
+ if (matches) {
+ if (msg == .output) q.applied(pane);
+ q.removeAt(i).deinit(q.gpa, q.lsp_allocator);
+ } else i += 1;
}
q.space.broadcast(q.io);
}
@@ -189,7 +212,7 @@ pub fn push(q: *Inbox, m: Msg) void {
return;
}
std.debug.assert(q.len < q.items.len);
- } else if (q.len >= output_capacity) {
+ } else if (q.len >= @as(usize, if (m == .adopted) adopt_capacity else output_capacity)) {
q.unlock();
m.deinit(q.gpa, q.lsp_allocator);
return;
@@ -262,9 +285,11 @@ pub fn close(q: *Inbox) void {
}
pub fn deinit(q: *Inbox) void {
- // The query in flight ends first: what it still delivers is dropped.
+ // The workers in flight end first: what they still deliver is dropped.
if (q.lsp_task) |*t| t.future.cancel(q.io);
q.lsp_task = null;
+ q.pipe_tasks.cancelAll(q.io);
+ q.job_tasks.cancelAll(q.io);
q.close();
std.debug.assert(q.waiting == 0);
}
@@ -291,11 +316,14 @@ fn readPty(q: *Inbox, pty: std.Io.File, pane: pardes.Pane.Id, gen: u32) void {
},
};
if (n == 0) continue;
+ // Counted at once: out of the pty, the poll no longer sees it.
+ _ = q.unapplied[pane.index()].fetchAdd(1, .acq_rel);
const bytes = q.gpa.dupe(u8, buf[0..n]) catch |err| {
+ q.applied(pane);
failure = err;
break;
};
- if (!q.pushOutput(pane, gen, bytes)) return;
+ if (!q.pushOutput(pane, gen, bytes)) return q.applied(pane);
}
q.push(.{ .eof = .{ .pane = pane, .gen = gen, .failure = failure } });
}
@@ -311,9 +339,10 @@ pub fn startLsp(q: *Inbox, core: *pardes.Pardes, e: Lsp.Request) void {
Lsp.start(q.io, q.lsp_allocator, core, &q.lsp_task, e, q, pushLspRows);
}
-/// A language server's answer or status line taken off the queue, to the
-/// core (whichever frontend took it).
-pub fn landLsp(q: *Inbox, core: *pardes.Pardes, m: Msg) void {
+/// A worker's answer taken off the queue, to the core (whichever frontend
+/// took it), its worker joined: a language server's answer or status line,
+/// a pipe's, a search's rows, an addon's job or draw, a handover's end.
+pub fn land(q: *Inbox, core: *pardes.Pardes, m: Msg) void {
switch (m) {
.lsp => |l| {
core.update(.{ .lsp_resp = .{ .id = l.id, .rows = l.rows } });
@@ -324,7 +353,30 @@ pub fn landLsp(q: *Inbox, core: *pardes.Pardes, m: Msg) void {
core.setStatus(.at(core.view().active), pardes.Messages.Message.stamp(&mbuf, "lsp", text));
q.lsp_allocator.free(text);
},
- else => unreachable,
+ .pipe => |response_value| {
+ var response = response_value;
+ core.update(.{ .pipe_resp = .{
+ .id = response.id,
+ .success = response.success,
+ .outputs = response.outputs,
+ .failure = response.failure,
+ } });
+ response.deinit(q.gpa);
+ q.pipe_tasks.finish(q.io, response_value.id);
+ },
+ .search => |b| {
+ core.update(.{ .search = b });
+ b.deinit(q.gpa);
+ },
+ .kind_job => |j| {
+ q.job_tasks.finish(q.io, j.id);
+ j.land(core);
+ },
+ .handed => |h| {
+ core.update(.{ .handed = .{ .seq = h.seq, .to = if (h.ok) h.words else "", .failure = if (h.ok) null else h.words } });
+ q.gpa.free(h.words);
+ },
+ .output, .eof, .files_changed, .fs_ready, .adopted => unreachable,
}
}
@@ -340,12 +392,13 @@ pub fn lspStatus(ctx: ?*anyopaque, text: []const u8) void {
q.push(.{ .lsp_status = copy });
}
-/// A `|` job's answer (selection_pipe.start's sink).
+/// An addon's job or draw done (KindJob.go's sink).
pub fn pushJob(ctx: ?*anyopaque, j: *KindJob) void {
const q: *Inbox = @ptrCast(@alignCast(ctx.?));
q.push(.{ .kind_job = j });
}
+/// A `|` pipe'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 });