mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
core: add support to see if there are messages available for the current thread
This commit is contained in:
parent
d18063cbe1
commit
15e8f4e3d1
@ -224,6 +224,14 @@ int msg_reply(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.
|
||||
*
|
||||
|
16
core/msg.c
16
core/msg.c
@ -370,6 +370,22 @@ static int _msg_receive(msg_t *m, int block)
|
||||
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)
|
||||
{
|
||||
/* check if num is a power of two by comparing to its complement */
|
||||
|
6
tests/thread_msg_avail/Makefile
Normal file
6
tests/thread_msg_avail/Makefile
Normal file
@ -0,0 +1,6 @@
|
||||
APPLICATION = thread_msg_avail
|
||||
include ../Makefile.tests_common
|
||||
|
||||
DISABLE_MODULE += auto_init
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
61
tests/thread_msg_avail/main.c
Normal file
61
tests/thread_msg_avail/main.c
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user