Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/zig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,5 @@ jobs:
version: ${{ matrix.zig-version }}
- run: zig env
- run: zig build test
# libxev not on windows yet
- if: ${{ matrix.os != 'windows-latest' }}
run: zig build test-aio
- run: zig build test-aio
- run: zig build benchmark
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
.version = "0.0.0",
.dependencies = .{
.libxev = .{
.url = "https://api.github.com/repos/mitchellh/libxev/tarball/f06915",
.hash = "1220622831138efa0105981c34677a254894d33748cc0b2ff9694972ae3ec6408ce1",
.url = "https://api.github.com/repos/mitchellh/libxev/tarball/ecbc161",
.hash = "1220f34357168affd9aab1a3fcafcaff093c44beb75ce1d4d4b75490e90729221771",
},
},
}
2 changes: 1 addition & 1 deletion src/asyncio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ pub const AsyncNotification = struct {
}

const WaitResult = xev.Async.WaitError!void;
pub fn wait(self: Self) !void {
pub fn wait(self: *Self) !void {
const Data = XCallback(WaitResult);

const loop = getExec(self.exec).loop;
Expand Down
9 changes: 6 additions & 3 deletions src/coro.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ pub const Error = error{
};
pub const StackT = []align(base.stack_alignment) u8;
pub const stack_alignment = base.stack_alignment;
pub const default_stack_size = libcoro_options.default_stack_size;

pub const Frame = *Coro;

pub const Env = struct {
stack_allocator: ?std.mem.Allocator = null,
default_stack_size: ?usize = null,
executor: ?*Executor = null,

fn getDefaultStackSize(self: @This()) usize {
return self.default_stack_size orelse libcoro_options.default_stack_size;
}
};
threadlocal var env: Env = .{};
pub fn initEnv(e: Env) void {
Expand All @@ -55,7 +58,7 @@ fn getStack(stack: anytype) !StackInfo {
if (T == @TypeOf(null) or (is_optional and stack == null)) {
// Use env allocator with default stack size
if (env.stack_allocator == null) @panic("No explicit stack passed and no default stack allocator available");
return .{ .stack = try stackAlloc(env.stack_allocator.?, env.default_stack_size), .owned = true };
return .{ .stack = try stackAlloc(env.stack_allocator.?, null), .owned = true };
} else if (T == comptime_int or T == usize) {
// Use env allocator with provided stack size
const stack_size: usize = @intCast(stack);
Expand Down Expand Up @@ -102,7 +105,7 @@ pub const FrameT = CoroT.fromFunc;
// Allocate a stack suitable for coroutine usage.
// Caller is responsible for freeing memory.
pub fn stackAlloc(allocator: std.mem.Allocator, size: ?usize) !StackT {
return try allocator.alignedAlloc(u8, stack_alignment, size orelse default_stack_size);
return try allocator.alignedAlloc(u8, stack_alignment, size orelse env.getDefaultStackSize());
}

// True if within a coroutine, false if at top-level.
Expand Down
20 changes: 9 additions & 11 deletions src/test_aio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const AioTest = struct {
tp: *xev.ThreadPool,
loop: *xev.Loop,
exec: *aio.Executor,
stacks: []u8,

fn init() !@This() {
const allocator = std.testing.allocator;
Expand All @@ -23,8 +22,6 @@ const AioTest = struct {
loop.* = try xev.Loop.init(.{ .thread_pool = tp });
exec.* = aio.Executor.init(loop);
const stack_size = 1024 * 128;
const num_stacks = 5;
const stacks = try allocator.alignedAlloc(u8, libcoro.stack_alignment, num_stacks * stack_size);

// Thread-local env
env = .{
Expand All @@ -43,7 +40,6 @@ const AioTest = struct {
.tp = tp,
.loop = loop,
.exec = exec,
.stacks = stacks,
};
}

Expand All @@ -54,7 +50,6 @@ const AioTest = struct {
self.allocator.destroy(self.tp);
self.allocator.destroy(self.loop);
self.allocator.destroy(self.exec);
self.allocator.free(self.stacks);
}

fn run(self: @This(), func: anytype) !void {
Expand Down Expand Up @@ -120,7 +115,7 @@ test "aio concurrent sleep" {

const stack = try libcoro.stackAlloc(
t.allocator,
1024 * 8,
null,
);
defer t.allocator.free(stack);
const before = std.time.milliTimestamp();
Expand Down Expand Up @@ -261,6 +256,7 @@ fn processTest() !void {
}

test "aio process" {
if (@import("builtin").os.tag == .windows) return;
const t = try AioTest.init();
defer t.deinit();
try t.run(processTest);
Expand Down Expand Up @@ -300,7 +296,8 @@ fn tcpServer(info: *ServerInfo) !void {
try xserver.listen(1);

var sock_len = address.getOsSockLen();
try std.os.getsockname(xserver.fd, &address.any, &sock_len);
const fd = if (xev.backend == .iocp) @as(std.os.windows.ws2_32.SOCKET, @ptrCast(xserver.fd)) else xserver.fd;
try std.os.getsockname(fd, &address.any, &sock_len);
info.addr = address;

const server = aio.TCP.init(env.exec, xserver);
Expand Down Expand Up @@ -332,7 +329,8 @@ fn udpServer(info: *ServerInfo) !void {
try xserver.bind(address);

var sock_len = address.getOsSockLen();
try std.os.getsockname(xserver.fd, &address.any, &sock_len);
const fd = if (xev.backend == .iocp) @as(std.os.windows.ws2_32.SOCKET, @ptrCast(xserver.fd)) else xserver.fd;
try std.os.getsockname(fd, &address.any, &sock_len);
info.addr = address;

const server = aio.UDP.init(env.exec, xserver);
Expand Down Expand Up @@ -360,7 +358,7 @@ const NotifierState = struct {
};

fn asyncTest(state: *NotifierState) !void {
const notif = aio.AsyncNotification.init(env.exec, state.x);
var notif = aio.AsyncNotification.init(env.exec, state.x);
try notif.wait();
state.notified = true;
}
Expand Down Expand Up @@ -403,8 +401,8 @@ test "aio concurrent sleep env" {
try aio.run(null, sleepTaskEnv, .{}, null);
const after = std.time.milliTimestamp();

try std.testing.expect(after > (before + 17));
try std.testing.expect(after < (before + 23));
try std.testing.expect(after > (before + 16));
try std.testing.expect(after < (before + 24));
}

const UsizeChannel = libcoro.Channel(usize, .{ .capacity = 10 });
Expand Down