diff options
Diffstat (limited to 'src/serve.zig')
| -rw-r--r-- | src/serve.zig | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/serve.zig b/src/serve.zig index ad774a3..7ee47ba 100644 --- a/src/serve.zig +++ b/src/serve.zig @@ -123,6 +123,7 @@ pub fn Runner(comptime Backend: type, comptime opts: fs.Options, comptime limits flags: std.atomic.Value(u8) = .init(0), signal: Io.Event = .unset, room: Io.Event = .unset, + output_room: Io.Condition = .init, used: std.atomic.Value(bool) = .init(false), in: [msize]u8 = undefined, out: [2 * msize]u8 = undefined, @@ -152,6 +153,32 @@ pub fn Runner(comptime Backend: type, comptime opts: fs.Options, comptime limits c.raise(output); } + /// Completion from an independent task. Keep one full frame of + /// output capacity reserved for the synchronous handler that the + /// connection loop may already have dispatched. Waiting must not + /// hold a backend lock: that handler must be able to finish so the + /// connection loop can drain output. Cancellation/clunk must join + /// the producer task before this connection slot can be reused. + pub fn replyAsync(c: *Conn, r: *const Backend.Reply, bytes: []const u8) Io.Cancelable!void { + const io = c.runner.io; + c.lock(); + defer c.unlock(); + while (true) { + if (c.engine.protocol.dead or c.runner.stopping.load(.acquire)) { + return; + } + _ = c.engine.protocol.hasRoom(); // compact drained prefix + const reserve = 2 * @as(usize, @max(c.engine.protocol.msize, 24)); + if (c.engine.protocol.out.len - c.engine.protocol.out_len >= reserve) { + c.engine.reply(r, bytes); + c.raise(output); + return; + } + c.raise(output); + try c.output_room.wait(io, &c.mutex); + } + } + /// Answers a read or write `.again` and hands back the ticket /// of the park, to answer it through `answerWith()` when the backend /// has something (lib9p's kept Req); null when it could not @@ -230,6 +257,7 @@ pub fn Runner(comptime Backend: type, comptime opts: fs.Options, comptime limits c.flags = .init(0); c.signal = .unset; c.room = .unset; + c.output_room = .init; // Under the lock: the slot is already visible as live. c.lock(); c.engine.initIn(.{ .in = &c.in, .out = &c.out, .root = r.root, .seed = r.seed ^ c.index }); @@ -315,6 +343,7 @@ pub fn Runner(comptime Backend: type, comptime opts: fs.Options, comptime limits return; }; c.engine.wrote(pending.len); + c.output_room.broadcast(io); const dead = c.engine.protocol.dead; greeted = greeted or c.engine.protocol.msize != 0; c.unlock(); |
