Module mailbox
Inter-runtime communication mechanism using FIFOs and completions.
This module provides a way for different Lunatik runtimes (Lua states) to send and receive messages to/from each other. It uses a FIFO queue for message storage and a completion object for synchronization.
Mailboxes are unidirectional (inbox for receiving only, outbox for sending only). Messages are serialized as strings.
See also:
Tables
mailbox | The main mailbox table. |
Class MailBox
MailBox:receive ([timeout]) | Receives a message from the mailbox. |
MailBox:send (message) | Sends a message to the mailbox. |
mailbox:inbox (q, e) | Creates a new inbox (receive-only mailbox). |
mailbox:outbox (q, e) | Creates a new outbox (send-only mailbox). |
Tables
Class MailBox
Metatable for MailBox objects.
This table defines the methods available on mailbox instances.
- MailBox:receive ([timeout])
-
Receives a message from the mailbox.
This function will block until a message is available or the timeout expires.
Not available on outboxes.
Parameters:
- timeout number The maximum time to wait in jiffies. If omitted or negative, waits indefinitely. If 0, returns immediately. (optional)
Returns:
- string The received message.
- nil If no message is received (e.g., FIFO is empty after event or on timeout).
Or
-
string
Error message if the wait times out or another error occurs.
Raises:
Error if called on an outbox, or if the underlying event wait fails, or if a malformed message is encountered. - MailBox:send (message)
-
Sends a message to the mailbox.
Not available on inboxes.
Parameters:
- message string The message to send.
Raises:
Error if called on an inbox. - mailbox:inbox (q, e)
-
Creates a new inbox (receive-only mailbox).
Parameters:
- q (fifo|number) Either an existing FIFO object or a capacity for a new FIFO. If a number, a new FIFO with this capacity will be created.
- e
(completion) [optional] An existing completion object. If nil and
q
is a number, a new completion object will be created.
Returns:
-
(MailBox) A new inbox object.
Usage:
local my_inbox = mailbox.inbox(10) -- Inbox with capacity for 10 messages local msg = my_inbox:receive()
- mailbox:outbox (q, e)
-
Creates a new outbox (send-only mailbox).
Parameters:
- q (fifo|number) Either an existing FIFO object or a capacity for a new FIFO. If a number, a new FIFO with this capacity will be created.
- e
(completion) [optional] An existing completion object. If nil and
q
is a number, a new completion object will be created.
Returns:
-
(MailBox) A new outbox object.
Usage:
local my_outbox = mailbox.outbox(10) -- Outbox with capacity for 10 messages my_outbox:send("hello")