Skip to content

Commit

Permalink
change how completions get attached
Browse files Browse the repository at this point in the history
  • Loading branch information
mookums committed Sep 22, 2024
1 parent c39cf18 commit 1d257be
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/async/io_uring.zig
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ pub const AsyncIoUring = struct {
pub fn to_async(self: *AsyncIoUring) Async {
return Async{
.runner = self.runner,
.completions = undefined,
._queue_accept = queue_accept,
._queue_recv = queue_recv,
._queue_send = queue_send,
Expand Down
16 changes: 14 additions & 2 deletions src/async/lib.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const assert = std.debug.assert;
const builtin = @import("builtin");
const Socket = @import("../core/socket.zig").Socket;
const Completion = @import("completion.zig").Completion;
Expand Down Expand Up @@ -27,7 +28,8 @@ pub const AsyncError = error{

pub const Async = struct {
runner: *anyopaque,
completions: []Completion,
attached: bool = false,
completions: []Completion = undefined,

_queue_accept: *const fn (
self: *Async,
Expand Down Expand Up @@ -58,15 +60,20 @@ pub const Async = struct {
_reap: *const fn (self: *Async) AsyncError![]Completion,
_submit: *const fn (self: *Async) AsyncError!void,

pub fn attach_completions(self: *Async, completions: []Completion) void {
/// This provides the completions that the backend will utilize when
/// submitting and reaping. This MUST be called before any other
/// methods on this Async instance.
pub fn attach(self: *Async, completions: []Completion) void {
self.completions = completions;
self.attached = true;
}

pub fn queue_accept(
self: *Async,
context: *anyopaque,
socket: Socket,
) AsyncError!void {
assert(self.attached);
try @call(.auto, self._queue_accept, .{ self, context, socket });
}

Expand All @@ -76,6 +83,7 @@ pub const Async = struct {
socket: Socket,
buffer: []u8,
) AsyncError!void {
assert(self.attached);
try @call(.auto, self._queue_recv, .{ self, context, socket, buffer });
}

Expand All @@ -85,6 +93,7 @@ pub const Async = struct {
socket: Socket,
buffer: []const u8,
) AsyncError!void {
assert(self.attached);
try @call(.auto, self._queue_send, .{ self, context, socket, buffer });
}

Expand All @@ -93,14 +102,17 @@ pub const Async = struct {
context: *anyopaque,
socket: Socket,
) AsyncError!void {
assert(self.attached);
try @call(.auto, self._queue_close, .{ self, context, socket });
}

pub fn reap(self: *Async) AsyncError![]Completion {
assert(self.attached);
return try @call(.auto, self._reap, .{self});
}

pub fn submit(self: *Async) AsyncError!void {
assert(self.attached);
try @call(.auto, self._submit, .{self});
}
};
4 changes: 2 additions & 2 deletions src/core/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ pub fn Server(
self.config.size_completions_reap_max,
);

backend.attach_completions(completions);
backend.attach(completions);
}

defer {
Expand Down Expand Up @@ -741,7 +741,7 @@ pub fn Server(
z_config.size_completions_reap_max,
) catch unreachable;

thread_backend.attach_completions(completions);
thread_backend.attach(completions);
}

defer {
Expand Down
1 change: 0 additions & 1 deletion src/examples/custom/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ pub const CustomAsync = struct {
pub fn to_async(self: *CustomAsync) Async {
return Async{
.runner = self.inner,
.completions = undefined,
._queue_accept = queue_accept,
._queue_recv = queue_recv,
._queue_send = queue_send,
Expand Down

0 comments on commit 1d257be

Please sign in to comment.