Message passing between coroutines. More...
Macros | |
#define | BIO_MAILBOX(T) union { bio_handle_t bio__handle; T* bio__message; } |
Define a mailbox type. | |
#define | bio_open_mailbox(ptr, capacity) bio__mailbox_open(&(ptr)->bio__handle, sizeof(*(ptr)->bio__message), capacity) |
Create a new mailbox. | |
#define | bio_close_mailbox(mailbox) bio__mailbox_close((mailbox).bio__handle) |
Close a mailbox. | |
#define | bio_is_mailbox_open(mailbox) bio__mailbox_is_open((mailbox).bio__handle) |
Check whether a mailbox is open. | |
#define | bio_send_message(mailbox, message) |
Attempt to send a message to a mailbox. | |
#define | bio_wait_and_send_message(condition, mailbox, message) |
A more reliable way to send message. | |
#define | bio_recv_message(mailbox, message) |
Attempt to receive from a mailbox. | |
#define | bio_can_recv_message(mailbox) bio__mailbox_can_recv((mailbox).bio__handle) |
Check whether a mailbox can be immediately received from. | |
#define | bio_can_send_message(mailbox) bio__mailbox_can_send((mailbox).bio__handle) |
Check whether a mailbox can be immediately sent to. | |
#define | bio_foreach_message(msg, mailbox) |
Convenient message loop macro. | |
Message passing between coroutines.
Inspired by Erlang, bio provides a way for coroutines to message one another.
This module also makes use of macro to enforce compile-time type checking.
Example:
#define bio_can_recv_message | ( | mailbox | ) | bio__mailbox_can_recv((mailbox).bio__handle) |
Check whether a mailbox can be immediately received from.
Return whether all of the following are true:
#define bio_can_send_message | ( | mailbox | ) | bio__mailbox_can_send((mailbox).bio__handle) |
Check whether a mailbox can be immediately sent to.
Return whether all of the following are true:
#define bio_close_mailbox | ( | mailbox | ) | bio__mailbox_close((mailbox).bio__handle) |
Close a mailbox.
As with all resources in bio, closing an already closed mailbox is not an error. In fact, it is idiomatic to close a mailbox that another coroutine is waiting on to tell it to terminate.
#define bio_foreach_message | ( | msg, | |
mailbox | |||
) |
Convenient message loop macro.
#define bio_is_mailbox_open | ( | mailbox | ) | bio__mailbox_is_open((mailbox).bio__handle) |
Check whether a mailbox is open.
#define BIO_MAILBOX | ( | T | ) | union { bio_handle_t bio__handle; T* bio__message; } |
Define a mailbox type.
T | the type of message for this mailbox |
#define bio_open_mailbox | ( | ptr, | |
capacity | |||
) | bio__mailbox_open(&(ptr)->bio__handle, sizeof(*(ptr)->bio__message), capacity) |
Create a new mailbox.
A mailbox holds all of its messages by copy. When not needed, it should be closed with bio_close_mailbox.
ptr | Pointer to a mailbox handle |
capacity | How many messages can this mailbox hold before senders are blocked |
#define bio_recv_message | ( | mailbox, | |
message | |||
) |
Attempt to receive from a mailbox.
This will dequeue a message from the mailbox.
If the mailbox is empty, the calling coroutine will be suspended. It will be resumed once a message is sent to the mailbox. Otherwise, this would return immediately without context switching.
If the mailbox is already closed, this will immediately return false
.
If a mailbox is closed while a coroutine is waiting on it, the coroutine will be resumed with this returning false
. Closing a mailbox is an idiomatic way to ask a coroutine to terminate.
mailbox | The mailbox to receive from |
message | Pointer to a message |
#define bio_send_message | ( | mailbox, | |
message | |||
) |
Attempt to send a message to a mailbox.
The message will be copied into the mailbox. Upon return, it is safe to invalidate/reuse/free the message
.
A mailbox has limited capacity so when it is full, this will return false
and the message was not actually sent.
A mailbox can also already be closed. In that case, this will also return false
.
mailbox | The mailbox to send to |
message | The message, must be of the same accepted type as the declared mailbox. This will be checked at compile-time. |
#define bio_wait_and_send_message | ( | condition, | |
mailbox, | |||
message | |||
) |
A more reliable way to send message.
This will attempt to send to a mailbox and busy wait if the mailbox is full. If the mailbox is already closed, it will stop trying.
condition | A boolean expression |
mailbox | The mailbox to send to |
message | The message |