Skip to content

Commit 3ff5ada

Browse files
committed
mailbox: add function that remove and return message from mailbox
Return the message without disposing it. This can be useful for having full control on received messages. Signed-off-by: Davide Bettio <[email protected]>
1 parent c1eb499 commit 3ff5ada

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/libAtomVM/mailbox.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,14 @@ bool mailbox_peek(Context *c, term *out)
367367
return true;
368368
}
369369

370-
void mailbox_remove_message(Mailbox *mbox, Heap *heap)
370+
MailboxMessage *mailbox_take_message(Mailbox *mbox)
371371
{
372372
// This is called from OP_REMOVE_MESSAGE opcode, so we cannot make any
373373
// assumption about the state and should perform a nop if the mailbox
374374
// is empty.
375375
if (UNLIKELY(mbox->receive_pointer == NULL)) {
376376
fprintf(stderr, "OP_REMOVE_MESSAGE on empty mailbox\n");
377-
return;
377+
return NULL;
378378
}
379379
MailboxMessage *removed = mbox->receive_pointer;
380380
if (mbox->receive_pointer_prev) {
@@ -393,9 +393,18 @@ void mailbox_remove_message(Mailbox *mbox, Heap *heap)
393393
}
394394
}
395395

396-
mailbox_message_dispose(removed, heap);
397396
// Reset receive pointers
398397
mailbox_reset(mbox);
398+
399+
return removed;
400+
}
401+
402+
void mailbox_remove_message(Mailbox *mbox, Heap *heap)
403+
{
404+
MailboxMessage *removed = mailbox_take_message(mbox);
405+
if (LIKELY(removed != NULL)) {
406+
mailbox_message_dispose(removed, heap);
407+
}
399408
}
400409

401410
Message *mailbox_first(Mailbox *mbox)

src/libAtomVM/mailbox.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,16 @@ static inline bool mailbox_has_next(Mailbox *mbox)
263263
*/
264264
bool mailbox_peek(Context *ctx, term *out);
265265

266+
/**
267+
* @brief Take next message from mailbox.
268+
*
269+
* @details Remove the first message from the mailbox and return it.
270+
* To be called from the process only.
271+
* This function is intended for some corner cases, such as certain port drivers.
272+
* @param mbox the mailbox to take the next message from.
273+
*/
274+
MailboxMessage *mailbox_take_message(Mailbox *mbox);
275+
266276
/**
267277
* @brief Remove next message from mailbox.
268278
*

0 commit comments

Comments
 (0)