Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The channel looks to be only spsc. #30

Open
needon1997 opened this issue Jan 13, 2025 · 0 comments
Open

The channel looks to be only spsc. #30

needon1997 opened this issue Jan 13, 2025 · 0 comments

Comments

@needon1997
Copy link

needon1997 commented Jan 13, 2025

Try to implement a xselect like how it works on go/ may in Rust, and find that the current channel only support spsc.

pub fn xselect(tuples: anytype) !void {
    var chan = SelectChan.init(null);
    defer chan.close();
    comptime var idx: usize = 0;

    inline for (tuples) |tuple| {
        const Gen = struct {
            const Self = @This();
            func: @TypeOf(tuple[0]),
            args: @TypeOf(tuple[1]),
            cb: @TypeOf(tuple[2]),
            fn run(self: Self, _chan: *SelectChan, _idx: usize) anyerror!void {
                const ret =
                    if (@typeInfo(@TypeOf(self.func)).Fn.return_type == std.builtin.Type.ErrorUnion)
                    try @call(.always_inline, self.func, self.args)
                else
                    @call(.always_inline, self.func, self.args);
                try _chan.send(_idx);
                @call(.always_inline, self.cb, .{ret});
            }
        };
        const _gen: Gen = .{ .func = tuple[0], .args = tuple[1], .cb = tuple[2] };
        _ = try xasync(Gen.run, .{ _gen, &chan, idx }, tuple[3]);

        idx += 1;
    }

    _ = chan.recv();
    return;
}
fn selecMain() !void {
    const stack_size: usize = 1024 * 16;
    const stack1 = try libcoro.stackAlloc(env.allocator, stack_size);
    defer env.allocator.free(stack1);
    const stack2 = try libcoro.stackAlloc(env.allocator, stack_size);
    defer env.allocator.free(stack2);
    try aio.xselect(.{ .{ add, .{ @as(u32, 1), @as(u32, 2) }, report_add, stack1 }, .{ sub, .{ @as(u32, 1), @as(u32, 1) }, report_sub, stack2 } });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant