summaryrefslogtreecommitdiff
path: root/dev/KindJob.zig
blob: e9cea5b464fdec4ff17776a374b3b61ffe686b85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const std = @import("std");
const pardes = @import("core");
const selection_pipe = @import("core").selection_pipe;
const KindJob = @This();

gpa: std.mem.Allocator,
pane: u8,
serial: u32,
token: u32,
gen: u32,
ops: *const pardes.Kind.Ops,
in: []u8,
/// What the job allocates (`pa_out`), freed with it; `out` the last.
arena: std.heap.ArenaAllocator,
out: []u8 = &.{},
id: u32 = 0,
/// A draw's: the core it was asked in.
draw: ?*const pardes.Pardes = null,

pub fn take(p: *pardes.Pardes, pane: u8, serial: u32, token: u32) ?*KindJob {
    const pn = p.panes[pane] orelse return null;
    const slot = pn.addon orelse return null;
    const ops = slot.ops orelse return null;
    if (pn.serial != serial or ops.job_run == null) return null;
    const at = for (&slot.jobs) |*j| {
        if (j.*) |job| if (job.token == token) break j;
    } else return null;
    const j = slot.gpa.create(KindJob) catch return null;
    j.* = .{ .gpa = slot.gpa, .pane = pane, .serial = serial, .token = token, .gen = slot.gen, .ops = ops, .in = at.*.?.in, .arena = .init(slot.gpa) };
    at.* = null;
    pardes.Addon.holdJob(ops, true);
    return j;
}

pub fn run(j: *KindJob) void {
    const Out = struct {
        fn alloc(out: [*c]pardes.Kind.c.pa_out, n: usize) callconv(.c) ?*anyopaque {
            const job: *KindJob = @ptrCast(@alignCast(out.*.ctx));
            job.out = job.arena.allocator().alignedAlloc(u8, .@"16", n) catch &.{};
            return if (job.out.len == n) job.out.ptr else null;
        }
    };
    var out: pardes.Kind.c.pa_out = .{ .alloc = Out.alloc, .ctx = j };
    const n = j.ops.job_run.?(j.in.ptr, @intCast(j.in.len), &out);
    j.out = j.out[0..@min(n, j.out.len)];
}

/// Its answer to the core, and it gone.
pub fn land(j: *KindJob, p: *pardes.Pardes) void {
    if (j.draw != null) return pardes.Kind.landDraw(p, j);
    p.update(.{ .job = .{ .pane = .at(j.pane), .serial = j.serial, .token = j.token, .gen = j.gen, .bytes = j.out } });
    j.drop();
}

pub fn drop(j: *KindJob) void {
    pardes.Addon.holdJob(j.ops, false);
    j.gpa.free(j.in);
    j.arena.deinit();
    j.gpa.destroy(j);
}

/// On a worker of `io`, posting itself to `sink` done: a host's `job`.
pub fn start(io: std.Io, p: *pardes.Pardes, tasks: *selection_pipe.Tasks, pane: u8, serial: u32, token: u32, ctx: ?*anyopaque, sink: *const fn (?*anyopaque, *KindJob) void) void {
    const j = take(p, pane, serial, token) orelse return;
    if (!go(io, tasks, j, ctx, sink)) here(j, p);
}

/// `j` on a worker of `io`, posting itself to `sink` done; false when
/// none could take it.
pub fn go(io: std.Io, tasks: *selection_pipe.Tasks, j: *KindJob, ctx: ?*anyopaque, sink: *const fn (?*anyopaque, *KindJob) void) bool {
    next_id +%= 1;
    j.id = next_id | 0x8000_0000;
    const work = struct {
        fn work(job: *KindJob, c: ?*anyopaque, s: *const fn (?*anyopaque, *KindJob) void) void {
            job.run();
            s(c, job);
        }
    }.work;
    if (tasks.full()) return false;
    const future = io.concurrent(work, .{ j, ctx, sink }) catch return false;
    _ = tasks.add(.{ .id = j.id, .future = future });
    return true;
}

var next_id: u32 = 0;

fn here(j: *KindJob, p: *pardes.Pardes) void {
    j.run();
    j.land(p);
}

/// No workers (a test, the fallback): it runs on the loop, at once.
pub fn runHere(p: *pardes.Pardes, pane: u8, serial: u32, token: u32) void {
    here(take(p, pane, serial, token) orelse return, p);
}