1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

core/msg: make msg_avail() return 0 on no queue

For the caller there should be no difference if there is no message
in the queue and if there can't be a message in the queue.

The current API works as one would expect if there is a message queue,
but once called from a thread that does not have a message queue
configured, code that does

    while (msg_avail())

will end up in an infinite loop.

Remove this foot-gun from the API by making the return value of
msg_avail() independend of the availability of a message queue.
This commit is contained in:
Benjamin Valentin 2021-11-24 13:35:20 +01:00
parent 45add49342
commit b5ea78ad47
4 changed files with 16 additions and 17 deletions

View File

@ -362,9 +362,9 @@ int msg_reply_int(msg_t *m, msg_t *reply);
* @brief Check how many messages are available in the message queue * @brief Check how many messages are available in the message queue
* *
* @return Number of messages available in our queue on success * @return Number of messages available in our queue on success
* @return -1, if no caller's message queue is initialized * @return 0, if no caller's message queue is initialized
*/ */
int msg_avail(void); unsigned msg_avail(void);
/** /**
* @brief Initialize the current thread's message queue. * @brief Initialize the current thread's message queue.

View File

@ -433,20 +433,20 @@ 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) unsigned msg_avail(void)
{ {
DEBUG("msg_available: %" PRIkernel_pid ": msg_available.\n", DEBUG("msg_available: %" PRIkernel_pid ": msg_available.\n",
thread_getpid()); thread_getpid());
thread_t *me = thread_get_active(); thread_t *me = thread_get_active();
int queue_index = -1; unsigned queue_count = 0;
if (thread_has_msg_queue(me)) { if (thread_has_msg_queue(me)) {
queue_index = cib_avail(&(me->msg_queue)); queue_count = cib_avail(&(me->msg_queue));
} }
return queue_index; return queue_count;
} }
void msg_init_queue(msg_t *array, int num) void msg_init_queue(msg_t *array, int num)
@ -462,11 +462,11 @@ void msg_queue_print(void)
unsigned state = irq_disable(); unsigned state = irq_disable();
thread_t *thread = thread_get_active(); thread_t *thread = thread_get_active();
int msg_counter = msg_avail(); unsigned msg_counter = msg_avail();
if (msg_counter <= -1) { if (msg_counter < 1) {
/* no msg queue */ /* no msg queue */
printf("No message queue\n"); printf("No messages or no message queue\n");
return; return;
} }
cib_t *msg_queue = &thread->msg_queue; cib_t *msg_queue = &thread->msg_queue;
@ -474,9 +474,9 @@ void msg_queue_print(void)
int first_msg = cib_peek(msg_queue); int first_msg = cib_peek(msg_queue);
printf("Message queue of thread %" PRIkernel_pid "\n", thread->pid); printf("Message queue of thread %" PRIkernel_pid "\n", thread->pid);
printf(" size: %u (avail: %d)\n", msg_queue->mask + 1, msg_counter); printf(" size: %u (avail: %u)\n", msg_queue->mask + 1, msg_counter);
for (int i = 0; i < msg_counter; i++) { for (unsigned i = 0; i < msg_counter; i++) {
msg_t *m = &msg_array[(first_msg + i) & msg_queue->mask]; msg_t *m = &msg_array[(first_msg + i) & msg_queue->mask];
printf(" * %u: sender: %" PRIkernel_pid ", type: 0x%04" PRIu16 printf(" * %u: sender: %" PRIkernel_pid ", type: 0x%04" PRIu16
", content: %" PRIu32 " (%p)\n", i, m->sender_pid, m->type, ", content: %" PRIu32 " (%p)\n", i, m->sender_pid, m->type,

View File

@ -39,7 +39,7 @@ int main(void)
puts("[START]"); puts("[START]");
/* add message to own queue */ /* add message to own queue */
for (int idx = 0; idx < MSG_QUEUE_LENGTH; ++idx) { for (unsigned idx = 0; idx < MSG_QUEUE_LENGTH; ++idx) {
msges[idx].type = idx; msges[idx].type = idx;
msg_send_to_self(msges + idx); msg_send_to_self(msges + idx);
LOG_INFO("+ add msg: %d\n", idx); LOG_INFO("+ add msg: %d\n", idx);
@ -49,11 +49,11 @@ int main(void)
} }
} }
/* receive available messages in queue */ /* receive available messages in queue */
for (int idx = msg_avail(); idx > 0; --idx) { for (unsigned idx = msg_avail(); idx > 0; --idx) {
msg_t msg; msg_t msg;
msg_receive(&msg); msg_receive(&msg);
LOG_INFO("- got msg: %d\n", (MSG_QUEUE_LENGTH - idx)); LOG_INFO("- got msg: %d\n", (MSG_QUEUE_LENGTH - idx));
if ((int)msg.type != (MSG_QUEUE_LENGTH - idx) if (msg.type != (MSG_QUEUE_LENGTH - idx)
|| msg_avail() != idx - 1) { || msg_avail() != idx - 1) {
puts("[FAILED]"); puts("[FAILED]");
return 1; return 1;

View File

@ -14,9 +14,8 @@ from testrunner import run
def testfunc(child): def testfunc(child):
child.expect("No message queue") child.expect("No messages or no message queue")
child.expect(r"Message queue of thread \d+\r\n") child.expect("No messages or no message queue")
child.expect_exact('size: 8 (avail: 0)')
child.expect(r"Message queue of thread \d+\r\n") child.expect(r"Message queue of thread \d+\r\n")
child.expect_exact('size: 8 (avail: 8)') child.expect_exact('size: 8 (avail: 8)')
if os.environ.get('BOARD') == 'native': if os.environ.get('BOARD') == 'native':