Skip to content

Commit c73a877

Browse files
committed
Merge pull request atomvm#602 from bettio/add-take-message-function
Add mailbox_take_message function Return the message without disposing it. This can be useful for having full control on received messages. These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents c1eb499 + 7315355 commit c73a877

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/libAtomVM/mailbox.c

Lines changed: 4 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,10 @@ 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;
399400
}
400401

401402
Message *mailbox_first(Mailbox *mbox)

src/libAtomVM/mailbox.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern "C" {
3838
#include "list.h"
3939
#include "smp.h"
4040
#include "term_typedef.h"
41+
#include "utils.h"
4142

4243
struct Context;
4344

@@ -264,14 +265,14 @@ static inline bool mailbox_has_next(Mailbox *mbox)
264265
bool mailbox_peek(Context *ctx, term *out);
265266

266267
/**
267-
* @brief Remove next message from mailbox.
268+
* @brief Take next message from mailbox.
268269
*
269-
* @details Discard a term that has been previously queued on a certain process
270-
* or driver mailbox. To be called from the process only. Term messages are
271-
* actually added as fragments to the heap and will be gone at next GC.
272-
* @param mbx the mailbox to remove next message from.
270+
* @details Remove the first message from the mailbox and return it.
271+
* To be called from the process only.
272+
* This function is intended for some corner cases, such as certain port drivers.
273+
* @param mbox the mailbox to take the next message from.
273274
*/
274-
void mailbox_remove_message(Mailbox *mbox, Heap *ctx);
275+
MailboxMessage *mailbox_take_message(Mailbox *mbox);
275276

276277
/**
277278
* @brief Get first message from mailbox.
@@ -304,6 +305,22 @@ void mailbox_destroy(Mailbox *mbox, Heap *heap);
304305
*/
305306
void mailbox_message_dispose(MailboxMessage *m, Heap *heap);
306307

308+
/**
309+
* @brief Remove next message from mailbox.
310+
*
311+
* @details Discard a term that has been previously queued on a certain process
312+
* or driver mailbox. To be called from the process only. Term messages are
313+
* actually added as fragments to the heap and will be gone at next GC.
314+
* @param mbx the mailbox to remove next message from.
315+
*/
316+
static inline void mailbox_remove_message(Mailbox *mbox, Heap *heap)
317+
{
318+
MailboxMessage *removed = mailbox_take_message(mbox);
319+
if (LIKELY(removed != NULL)) {
320+
mailbox_message_dispose(removed, heap);
321+
}
322+
}
323+
307324
/**
308325
* @brief Output mailbox to stderr for crashdump reporting.
309326
*

0 commit comments

Comments
 (0)