summaryrefslogtreecommitdiff
path: root/src/selection_pipe.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/selection_pipe.zig')
-rw-r--r--src/selection_pipe.zig38
1 files changed, 32 insertions, 6 deletions
diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig
index 229b3d67..73bac520 100644
--- a/src/selection_pipe.zig
+++ b/src/selection_pipe.zig
@@ -1,10 +1,11 @@
//! Native runner and boundary-owned values for `|`: pipe every editor
//! selection through one shell command. The core imports only the plain value
-//! types below; native shells call `Job.copy` before starting a worker, then
-//! `runOne` on that worker. No subprocess or borrowed core memory reaches the
-//! editor/event-loop thread.
+//! types below; native shells call `start`, which copies the request
+//! (`Job.copy`) and runs it on a worker. No subprocess or borrowed core
+//! memory reaches the editor/event-loop thread.
const std = @import("std");
const filesystem = @import("fs.zig");
+const pardes = @import("pardes.zig");
/// A deliberately finite answer. One selection cannot retain more than 1 MiB
/// and a multi-selection command cannot retain more than 4 MiB in total.
@@ -180,7 +181,7 @@ pub const Tasks = struct {
pub const Task = struct {
id: u32,
- future: std.Io.Future(anyerror!void),
+ future: std.Io.Future(void),
};
items: [capacity]Task = undefined,
@@ -201,7 +202,7 @@ pub const Tasks = struct {
/// stays deterministic.
pub fn finish(tasks: *Tasks, io: std.Io, id: u32) void {
for (tasks.items[0..tasks.len], 0..) |*task, i| if (task.id == id) {
- task.future.await(io) catch {};
+ task.future.await(io);
tasks.len -= 1;
std.mem.copyForwards(Task, tasks.items[i..tasks.len], tasks.items[i + 1 .. tasks.len + 1]);
return;
@@ -209,11 +210,36 @@ pub const Tasks = struct {
}
pub fn cancelAll(tasks: *Tasks, io: std.Io) void {
- for (tasks.items[0..tasks.len]) |*task| task.future.cancel(io) catch {};
+ for (tasks.items[0..tasks.len]) |*task| task.future.cancel(io);
tasks.len = 0;
}
};
+/// Where a worker hands its response; the host owns it from there.
+pub const Sink = *const fn (ctx: ?*anyopaque, response: Response) void;
+
+/// Start pipe `id` on a worker that hands its response to `sink`. One that
+/// cannot start (`tasks` full, no memory, no worker) is answered failed here.
+pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, tasks: *Tasks, id: u32, ctx: ?*anyopaque, sink: Sink) void {
+ if (tasks.full()) return core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } });
+ const view = core.pipe.pipeRequest(id) orelse return;
+ const job = Job.copy(gpa, view) catch |err| {
+ core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } });
+ return core.reportError(core.active, "pipe", err);
+ };
+ const future = io.concurrent(work, .{ io, gpa, job, ctx, sink }) catch |err| {
+ job.deinit(gpa);
+ core.update(.{ .pipe_resp = .{ .id = id, .success = false, .outputs = &.{} } });
+ return core.reportError(core.active, "pipe", err);
+ };
+ std.debug.assert(tasks.add(.{ .id = id, .future = future }));
+}
+
+fn work(io: std.Io, gpa: std.mem.Allocator, job: *Job, ctx: ?*anyopaque, sink: Sink) void {
+ defer job.deinit(gpa);
+ sink(ctx, runJob(gpa, io, job));
+}
+
/// The stdin writer's own: its copy of the input, freed by the writer when
/// it is done, so a writer stuck on a command that never reads (and cannot
/// be killed, below) can be let go of without its input dying under it.