Skip to content
Snippets Groups Projects
Commit 349d333e authored by Oleg Hahm's avatar Oleg Hahm
Browse files

Merge pull request #3997 from DipSwitch/core_msg_avail_support

core: add support to see if there are messages available for the curr…
parents f0e5413f 15e8f4e3
No related branches found
No related tags found
No related merge requests found
...@@ -221,6 +221,14 @@ int msg_reply(msg_t *m, msg_t *reply); ...@@ -221,6 +221,14 @@ int msg_reply(msg_t *m, msg_t *reply);
*/ */
int msg_reply_int(msg_t *m, msg_t *reply); int msg_reply_int(msg_t *m, msg_t *reply);
/**
* @brief Check how many messages are available in the message queue
*
* @return Number of messages available in our queue on success
* @return -1, if no caller's message queue is initialized
*/
int msg_avail(void);
/** /**
* @brief Initialize the current thread's message queue. * @brief Initialize the current thread's message queue.
* *
......
...@@ -370,6 +370,22 @@ static int _msg_receive(msg_t *m, int block) ...@@ -370,6 +370,22 @@ static int _msg_receive(msg_t *m, int block)
DEBUG("This should have never been reached!\n"); DEBUG("This should have never been reached!\n");
} }
int msg_avail(void)
{
DEBUG("msg_available: %" PRIkernel_pid ": msg_available.\n",
sched_active_thread->pid);
tcb_t *me = (tcb_t*) sched_active_thread;
int queue_index = -1;
if (me->msg_array) {
queue_index = cib_avail(&(me->msg_queue));
}
return queue_index;
}
int msg_init_queue(msg_t *array, int num) int msg_init_queue(msg_t *array, int num)
{ {
/* check if num is a power of two by comparing to its complement */ /* check if num is a power of two by comparing to its complement */
......
APPLICATION = thread_msg_avail
include ../Makefile.tests_common
DISABLE_MODULE += auto_init
include $(RIOTBASE)/Makefile.include
/*
* Copyright (C) 2015 Nick van IJzendoorn <nijzendoorn@engineering-spirit.nl>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief Thread test application
*
* @author Nick van IJzendoorn <nijzendoorn@engineering-spirit.nl>
*
* @}
*/
#include <stdio.h>
#include <inttypes.h>
#include "thread.h"
#include "msg.h"
#define MSG_QUEUE_LENGTH (8)
msg_t msg_queue[MSG_QUEUE_LENGTH];
int main(void)
{
msg_t msges[MSG_QUEUE_LENGTH];
msg_init_queue(msg_queue, MSG_QUEUE_LENGTH);
for (int idx = 0; idx < MSG_QUEUE_LENGTH; ++idx) {
msges[idx].sender_pid = thread_getpid();
msges[idx].type = idx;
msg_send_to_self(msges + idx);
printf("Add message %d\n", idx);
while (msg_avail() != (idx) + 1)
; /* spin forever if we don't have the result we expect */
}
for (int idx = msg_avail(); idx > 0; --idx) {
msg_t msg;
msg_receive(&msg);
printf("Receive message: %d\n", (MSG_QUEUE_LENGTH - idx));
while (msg.type != (MSG_QUEUE_LENGTH - idx) || msg_avail() != idx - 1)
; /* spin forever if we don't have the result we expect */
}
puts("TEST PASSED\n");
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment