#Channel Blocking On Put

1 messages · Page 1 of 1 (latest)

fierce minnow
#

Given that actor is defined thusly ```pub const Actor = struct {
status: ActorStatus,
system_mailbox: Channel(SystemMessage),
user_mailbox: Channel(*const Message),
handler: fn (*Actor, *const Message, *Future(bool)) callconv(.Async) void,
allocator: mem.Allocator,
schedule: bool,

pub fn init(allocator: mem.Allocator) !Actor {
    const sys_buff = try allocator.alloc(SystemMessage,1024);
    errdefer allocator.free(sys_buff);
    var sys_chan: Channel(SystemMessage) = undefined;
    sys_chan.init(sys_buff);
    const user_buff = try allocator.alloc(*const Message,1024);
    var user_chan: Channel(*const Message) = undefined;
    user_chan.init(user_buff);
    return Actor {
        .status = ActorStatus.Idle,
        .system_mailbox = sys_chan,
        .user_mailbox = user_chan,
        .handler = undefined,
        .allocator = allocator,
        .schedule = false,
    };
}

pub fn postUserMessage(self: *Actor, msg: *const Message) void {
    self.user_mailbox.put(msg);
    self.schedule = true;
}```

when I try to execute actor.postUserMessage(&Message{ .message_type = MessageType.Hello, .data = "World"}); actor.postUserMessage(&Message{ .message_type = MessageType.Hello, .data = "World"}); actor.execute();

the program blocks on the second call to postUserMessage. Any ideas why this might be?

drifting bear
#
  1. async function pointers are hairy. Async functions are actually stack-machines, not necessarily pointers to code like normal functions
  2. if this is std.event.Channel, I think there's known bugs with it. ATM i think its best to opt for a different channel impl
  3. f(&T{}), where the pointer outlives f() is kind-of an anti-pattern; Not sure if it's item is valid after the call completes like it would be if it was in its own decl