How Mailbox became an Io Owner

Mailbox - inter thread comminication mechanism.

I’ve used a similar approach since 1989 (iRMX86).

Zig version was migrated to 0.16.0

It’s internal implementation is based on Mutex and Condition.
In 0.16.0 Mutex&Condition methods require io:Io as addditional argument.

As result, Mailbox now is happy owner of io - added init function for each Mailbox type:

        pub fn init(io: Io) Self {
            return .{
                .io = io,
                .closed = std.atomic.Value(bool).init(false),
            };
        }

The rest of the API remains unchanged.

Because 0.16.0 does not contain condition.waitTimeout , it was borrowed from Zig 0.17.dev.

For the curious Mailbox on Odin - part of Matryoshka

5 Likes

I first encountered MailBox in the late 80s while working on a real-time system

Are you still working on real time systems? (If you don’t mind me asking)

real-time - since 1977
since mid 90s - enterprise boring systems (networking/infra)
since 2023 - open source
not real time , but i hope real projects

2 Likes

Nice – I’ll try this in a few weeks. Have you ever worked in or looked at Erlang or Elixir? This makes me want Zig “processes” in the Erlang style.

1 Like

Maybe - after Odin

1 Like

I see pointers being used with next and last, are these mailboxes unbounded?

Would bounded mailboxes (with upper limits) be desirable to circumvent a situation when the consumer is too slow and the mailboxes just grows ad infinitum?

This way you could ring buffers could be used with linear memory layout.

1 Like

yes, because main purpose is inter-thread communication
if i should send message i’ll send message

regarding limitations/backpressure and similar requirements i use different mechanism - pool:
before send message you allocate it via pool and will get error if pool is empty
(and as result - nothing to send)

If Pool is pool of jobs - you will not submit new job , because all possible jobs are in processing

Example of internal pool implementation and description

Shortly - I don’t combine two functions within one “object”

1 Like