Skip to content

The channel looks to be only spsc. #30

@edenw97

Description

@edenw97

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 } });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions