Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: add functionality to check queue state of another thread #16174

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/include/cib.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ static inline void cib_init(cib_t *__restrict cib, unsigned int size)
*cib = c;
}

/**
* @brief Returns the total capacity (`size` parameter of @ref cib_init()) of
* a cib_t
*
* @param[in] cib the cib_t to check.
* Must not be NULL.
* @return The total size of @p cib.
*/
static inline unsigned int cib_size(const cib_t *cib)
{
return cib->mask + 1;
}

/**
* @brief Calculates difference between cib_put() and cib_get() accesses.
*
Expand Down
21 changes: 20 additions & 1 deletion core/include/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,32 @@ 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
* @brief Check how many messages are available (waiting) in the message queue
* of a specific thread
*
* @param[in] pid a PID
*
* @return Number of messages available in queue of @p pid on success
* @return 0, if no caller's message queue is initialized
*/
unsigned msg_avail_thread(kernel_pid_t pid);

/**
* @brief Check how many messages are available (waiting) in the message queue
*
* @return Number of messages available in our queue on success
* @return 0, if no caller's message queue is initialized
*/
unsigned msg_avail(void);

/**
miri64 marked this conversation as resolved.
Show resolved Hide resolved
* @brief Get maximum capacity of a thread's queue length
*
* @return Number of total messages that fit in the queue of @p pid on success
* @return 0, if no caller's message queue is initialized
*/
unsigned msg_queue_capacity(kernel_pid_t pid);

/**
* @brief Initialize the current thread's message queue.
*
Expand Down
35 changes: 29 additions & 6 deletions core/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,22 +433,45 @@ static int _msg_receive(msg_t *m, int block)
DEBUG("This should have never been reached!\n");
}

unsigned msg_avail(void)
static unsigned _msg_avail(thread_t *thread)
{
DEBUG("msg_available: %" PRIkernel_pid ": msg_available.\n",
thread_getpid());

thread_t *me = thread_get_active();
thread->pid);

unsigned queue_count = 0;

if (thread_has_msg_queue(me)) {
queue_count = cib_avail(&(me->msg_queue));
if (thread_has_msg_queue(thread)) {
queue_count = cib_avail(&(thread->msg_queue));
}

return queue_count;
}

unsigned msg_avail_thread(kernel_pid_t pid)
{
return _msg_avail(thread_get(pid));
}

unsigned msg_avail(void)
{
return _msg_avail(thread_get_active());
}

unsigned msg_queue_capacity(kernel_pid_t pid)
{
DEBUG("msg_queue_capacity: %" PRIkernel_pid ": msg_queue_capacity.\n",
pid);

thread_t *thread = thread_get(pid);
int queue_cap = 0;

if (thread_has_msg_queue(thread)) {
queue_cap = cib_size(&(thread->msg_queue));
}

return queue_cap;
}

void msg_init_queue(msg_t *array, int num)
{
thread_t *me = thread_get_active();
Expand Down
5 changes: 5 additions & 0 deletions tests/msg_queue_capacity/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include ../Makefile.tests_common

USEMODULE += embunit

include $(RIOTBASE)/Makefile.include
59 changes: 59 additions & 0 deletions tests/msg_queue_capacity/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2015 Nick van IJzendoorn <nijzendoorn@engineering-spirit.nl>
* 2017 HAW Hamburg
*
* 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>
* @author Sebastian Meiling <s@mlng.net>
*
* @}
*/

#include <stdio.h>
#include <inttypes.h>

#include "embUnit/embUnit.h"
#include "embUnit.h"
#include "msg.h"
#include "thread.h"

#define MSG_QUEUE_LENGTH (8)

static msg_t msg_queue[MSG_QUEUE_LENGTH];

static void test_msg_queue_capacity(void)
{
TEST_ASSERT_EQUAL_INT(0, msg_queue_capacity(thread_getpid()));
msg_init_queue(msg_queue, MSG_QUEUE_LENGTH);
TEST_ASSERT_EQUAL_INT(MSG_QUEUE_LENGTH, msg_queue_capacity(thread_getpid()));
}

static Test *tests_msg_queue_capacity_suite(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_msg_queue_capacity),
};

EMB_UNIT_TESTCALLER(tests, NULL, NULL, fixtures);

return (Test *)&tests;
}

int main(void)
{
TESTS_START();
TESTS_RUN(tests_msg_queue_capacity_suite());
TESTS_END();
return 0;
}
15 changes: 15 additions & 0 deletions tests/msg_queue_capacity/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python3

# Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
# 2017 Sebastian Meiling <s@mlng.net>
#
# 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.

import sys
from testrunner import run_check_unittests


if __name__ == "__main__":
sys.exit(run_check_unittests())