Skip to content

Commit

Permalink
merge 0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rsepassi committed Apr 23, 2024
2 parents 7cf8756 + 5297a84 commit 7330643
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/zig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- uses: actions/checkout@v3
- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.11.0
version: 0.12.0
- run: zig env
- run: zig build test
# libxev not on windows yet
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ Supports async IO via [`libxev`][libxev].

[![test][ci-badge]][ci]

Branch `main` tested against Zig v0.11.0.

Use branch [`zigmaster`](https://github.com/rsepassi/zigcoro/tree/zigmaster) to
use Zig master. See its README for what version it is tested against.
Branch `main` tested against Zig v0.12.0.

Coroutines supported on Windows `x86_64`, Linux {`x86_64`, `aarch64`, `riscv64`}, and Mac {`x86_64`, `aarch64`}.

Expand Down
8 changes: 4 additions & 4 deletions benchmark.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn contextSwitchBm() !void {
// warmup
num_bounces = 100_000;
{
var test_coro = try libcoro.xasync(testFn, .{}, stack);
const test_coro = try libcoro.xasync(testFn, .{}, stack);
for (0..num_bounces) |_| {
libcoro.xresume(test_coro);
}
Expand All @@ -37,7 +37,7 @@ fn contextSwitchBm() !void {

num_bounces = 20_000_000;
{
var test_coro = try libcoro.xasync(testFn, .{}, stack);
const test_coro = try libcoro.xasync(testFn, .{}, stack);

const start = std.time.nanoTimestamp();
for (0..num_bounces) |_| {
Expand Down Expand Up @@ -118,12 +118,12 @@ fn ncorosBm(num_coros: usize) !void {
var coros = try allocator.alloc(libcoro.Frame, num_coros);
defer allocator.free(coros);

var buf = try allocator.alloc(u8, num_coros * 1024 * 4);
const buf = try allocator.alloc(u8, num_coros * 1024 * 4);
var fba = std.heap.FixedBufferAllocator.init(buf);
const alloc2 = fba.allocator();

for (0..num_coros) |i| {
var stack = try libcoro.stackAlloc(alloc2, null);
const stack = try libcoro.stackAlloc(alloc2, null);
const coro = try libcoro.xasync(suspendRepeat, .{}, stack);
coros[i] = coro.frame();
}
Expand Down
14 changes: 7 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub fn build(b: *std.Build) !void {
coro_options.addOption(usize, "debug_log_level", debug_log_level);
const coro_options_module = coro_options.createModule();
const coro = b.addModule("libcoro", .{
.source_file = .{ .path = "src/main.zig" },
.dependencies = &[_]std.Build.ModuleDependency{
.root_source_file = .{ .path = "src/main.zig" },
.imports = &.{
.{ .name = "xev", .module = xev },
.{ .name = "libcoro_options", .module = coro_options_module },
},
Expand All @@ -30,7 +30,7 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
coro_test.addModule("libcoro", coro);
coro_test.root_module.addImport("libcoro", coro);
coro_test.linkLibC();

const internal_test = b.addTest(.{
Expand All @@ -39,7 +39,7 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
internal_test.addModule("libcoro_options", coro_options_module);
internal_test.root_module.addImport("libcoro_options", coro_options_module);
internal_test.linkLibC();

// Test step
Expand All @@ -55,8 +55,8 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
aio_test.addModule("libcoro", coro);
aio_test.addModule("xev", xev);
aio_test.root_module.addImport("libcoro", coro);
aio_test.root_module.addImport("xev", xev);
aio_test.linkLibC();

// Test step
Expand All @@ -72,7 +72,7 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = .ReleaseFast,
});
bench.addModule("libcoro", coro);
bench.root_module.addImport("libcoro", coro);
bench.linkLibC();
const bench_run = b.addRunArtifact(bench);
if (b.args) |args| {
Expand Down
19 changes: 17 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@
.version = "0.0.0",
.dependencies = .{
.libxev = .{
.url = "https://api.github.com/repos/mitchellh/libxev/tarball/f06915",
.hash = "1220622831138efa0105981c34677a254894d33748cc0b2ff9694972ae3ec6408ce1",
.url = "git+https://github.com/mitchellh/libxev#a284cf851fe2f88f8947c01160c39ff216dacea1",
.hash = "12205b8ea5495b812b9a8943535a7af8da556644d2c1599dc01e1a5ea7aaf59bb2c7",
},
},
.paths = .{
"build.zig.zon",
"build.zig",
"benchmark.zig",
"src/coro_base.zig",
"src/asm/coro_x86_64_windows.s",
"src/asm/coro_x86_64.s",
"src/asm/coro_aarch64.s",
"src/main.zig",
"src/coro.zig",
"src/test_aio.zig",
"src/executor.zig",
"src/asyncio.zig",
"src/test.zig",
},
}
16 changes: 8 additions & 8 deletions src/coro.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn xasync(func: anytype, args: anytype, stack: anytype) !FrameT(func, .{ .Ar
.ArgsT = @TypeOf(args),
});
const framet = try FrameType.init(args, stack_info.stack, stack_info.owned);
var frame = framet.frame();
const frame = framet.frame();
xresume(frame);
return FrameType.wrap(frame);
}
Expand Down Expand Up @@ -193,7 +193,7 @@ const Coro = struct {

fn initFromStack(func: *const fn () void, stack: *Stack, owns_stack: bool, storage: ?*anyopaque) !Frame {
try StackOverflow.setMagicNumber(stack.full);
var coro = try stack.push(Coro);
const coro = try stack.push(Coro);
const base_coro = try base.Coro.init(&runcoro, stack.remaining());
coro.* = @This(){
.func = func,
Expand All @@ -211,8 +211,8 @@ const Coro = struct {
}

fn runcoro(from: *base.Coro, this: *base.Coro) callconv(.C) noreturn {
const from_coro = @fieldParentPtr(Coro, "impl", from);
const this_coro = @fieldParentPtr(Coro, "impl", this);
const from_coro: *Coro = @fieldParentPtr("impl", from);
const this_coro: *Coro = @fieldParentPtr("impl", this);
if (debug_log_level >= 3) {
std.debug.print("coro start {any}\n", .{this_coro.id});
}
Expand Down Expand Up @@ -322,7 +322,7 @@ const CoroT = struct {
owns_stack: bool,
) !Self {
var s = Stack.init(stack);
var inner = try s.push(InnerStorage);
const inner = try s.push(InnerStorage);
inner.* = .{
.args = args,
};
Expand Down Expand Up @@ -648,7 +648,7 @@ test "basic suspend and resume" {
defer allocator.free(stack);

testSetIdx(0);
var test_coro = try Coro.init(testFn, stack, false, null);
const test_coro = try Coro.init(testFn, stack, false, null);

testSetIdx(1);
try std.testing.expectEqual(test_coro.status, .Start);
Expand Down Expand Up @@ -690,7 +690,7 @@ test "with values" {
const allocator = std.testing.allocator;
const stack = try stackAlloc(allocator, null);
defer allocator.free(stack);
var coro = try Coro.init(Test.coroWrap, stack, false, @ptrCast(&storage));
const coro = try Coro.init(Test.coroWrap, stack, false, @ptrCast(&storage));

try std.testing.expectEqual(storage.x.*, 0);
xresume(coro);
Expand Down Expand Up @@ -744,7 +744,7 @@ test "iterator" {
const stack = try stackAlloc(allocator, null);
defer allocator.free(stack);

var x: usize = 1;
const x: usize = 1;
var coro = try Iter.init(.{x}, stack, false);
var yielded: usize = undefined;
yielded = Iter.xnextStart(coro); // first resume takes no value
Expand Down
19 changes: 11 additions & 8 deletions src/test_aio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const AioTest = struct {
var tp = try allocator.create(xev.ThreadPool);
var loop = try allocator.create(xev.Loop);
var exec = try allocator.create(aio.Executor);
_ = &tp;
_ = &loop;
_ = &exec;
tp.* = xev.ThreadPool.init(.{});
loop.* = try xev.Loop.init(.{ .thread_pool = tp });
exec.* = aio.Executor.init(loop);
Expand Down Expand Up @@ -98,14 +101,14 @@ fn sleepTask() !void {
null,
);
defer env.allocator.free(stack);
var sleep1 = try aio.xasync(sleep, .{10}, stack);
const sleep1 = try aio.xasync(sleep, .{10}, stack);

const stack2 = try libcoro.stackAlloc(
env.allocator,
null,
);
defer env.allocator.free(stack2);
var sleep2 = try aio.xasync(sleep, .{20}, stack2);
const sleep2 = try aio.xasync(sleep, .{20}, stack2);

const after = try aio.xawait(sleep1);
const after2 = try aio.xawait(sleep2);
Expand Down Expand Up @@ -231,11 +234,11 @@ fn udpMain() !void {

const stack1 = try libcoro.stackAlloc(env.allocator, stack_size);
defer env.allocator.free(stack1);
var server_co = try aio.xasync(udpServer, .{&info}, stack1);
const server_co = try aio.xasync(udpServer, .{&info}, stack1);

const stack2 = try libcoro.stackAlloc(env.allocator, stack_size);
defer env.allocator.free(stack2);
var client_co = try aio.xasync(udpClient, .{&info}, stack2);
const client_co = try aio.xasync(udpClient, .{&info}, stack2);

try aio.xawait(server_co);
try aio.xawait(client_co);
Expand Down Expand Up @@ -272,11 +275,11 @@ fn asyncMain() !void {

const stack = try libcoro.stackAlloc(env.allocator, stack_size);
defer env.allocator.free(stack);
var co = try aio.xasync(asyncTest, .{&nstate}, stack);
const co = try aio.xasync(asyncTest, .{&nstate}, stack);

const stack2 = try libcoro.stackAlloc(env.allocator, stack_size);
defer env.allocator.free(stack2);
var nco = try aio.xasync(asyncNotifier, .{&nstate}, stack2);
const nco = try aio.xasync(asyncNotifier, .{&nstate}, stack2);

try aio.xawait(co);
try aio.xawait(nco);
Expand All @@ -300,7 +303,7 @@ fn tcpServer(info: *ServerInfo) !void {
try xserver.listen(1);

var sock_len = address.getOsSockLen();
try std.os.getsockname(xserver.fd, &address.any, &sock_len);
try std.posix.getsockname(xserver.fd, &address.any, &sock_len);
info.addr = address;

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

var sock_len = address.getOsSockLen();
try std.os.getsockname(xserver.fd, &address.any, &sock_len);
try std.posix.getsockname(xserver.fd, &address.any, &sock_len);
info.addr = address;

const server = aio.UDP.init(env.exec, xserver);
Expand Down

0 comments on commit 7330643

Please sign in to comment.