Skip to content
Snippets Groups Projects
Commit 1ebecc77 authored by Thomas Geithner's avatar Thomas Geithner
Browse files

tests: add program for priority inversion

parent 61552892
No related branches found
No related tags found
No related merge requests found
# name of your application
APPLICATION = thread_priority_inversion
# If no BOARD is found in the environment, use this default:
BOARD ?= native
# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../../
# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
CFLAGS += -DDEVELHELP
# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1
# Modules to include:
USEMODULE += xtimer
BOARD_INSUFFICIENT_MEMORY := nucleo32-f031
include $(RIOTBASE)/Makefile.include
# thread_priority_inversion test application
This application uses three threads for demonstrating the
priority inversion problem. In theory, the highest priority thread (**t_high**)
should be scheduled periodically and produce some output:
```
2017-07-17 17:00:29,337 - INFO # t_high: got resource.
...
2017-07-17 17:00:30,343 - INFO # t_high: freeing resource...
```
During this phase of 1s, **t_high** lockes the mutex **res_mtx** which
represents a shared ressource. After unlocking the mutex, **t_high** waits 1s
before restaring its cycle again.
A low priority thread **t_low** is doing the same. Since both threads sharing
the same resource **res_mtx**, they have to wait for each other until the mutex
is unlocked. On startup, **t_low** starts immediately, while **t_high** waits
0.5s before so that **t_low** allocates the resource first. Together, the output
looks like this:
```
2017-07-17 17:00:28,339 - INFO # t_low: allocating resource...
2017-07-17 17:00:28,339 - INFO # t_low: got resource.
2017-07-17 17:00:28,340 - INFO # t_high: allocating resource...
2017-07-17 17:00:29,337 - INFO # t_low: freeing resource...
2017-07-17 17:00:29,337 - INFO # t_high: got resource.
2017-07-17 17:00:29,338 - INFO # t_low: freed resource.
2017-07-17 17:00:30,343 - INFO # t_high: freeing resource...
2017-07-17 17:00:30,344 - INFO # t_high: freed resource.
2017-07-17 17:00:30,345 - INFO # t_low: allocating resource...
2017-07-17 17:00:30,346 - INFO # t_low: got resource.
...
```
After 3s, a third thread with medium priority (**t_mid**) is started. This
thread does not touch **res_mtx**, but it runs an infinite loop without leaving
some CPU time to lower priority tasks. This prevents **t_low** from freeing the
resource and thus, **t_high** from running (**Priority Inversion**). In this
situation, the test program output stops with the following lines:
```
2017-07-17 17:00:31,335 - INFO # t_mid: doing some stupid stuff...
2017-07-17 17:00:31,340 - INFO # t_high: allocating resource...
```
If the scheduler contains a mechanism for handling this problem, the program
should continue with output from **t_high**.
/*
* Copyright (C) 2017 Technische Universität Berlin
*
* 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 for priority inversion problem
*
* @author Thomas Geithner <thomas.geithner@dai-labor.de>
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "thread.h"
#include "mutex.h"
#include "xtimer.h"
mutex_t res_mtx;
char stack_high[THREAD_STACKSIZE_DEFAULT];
char stack_mid[THREAD_STACKSIZE_DEFAULT];
char stack_low[THREAD_STACKSIZE_DEFAULT];
void *t_low_handler(void *arg)
{
(void) arg;
/* starting working loop immediately */
while (1) {
puts("t_low: allocating resource...");
mutex_lock(&res_mtx);
puts("t_low: got resource.");
xtimer_sleep(1);
puts("t_low: freeing resource...");
mutex_unlock(&res_mtx);
puts("t_low: freed resource.");
xtimer_sleep(1);
}
return NULL;
}
void *t_mid_handler(void *arg)
{
(void) arg;
/* starting working loop after 3s */
xtimer_sleep(3);
puts("t_mid: doing some stupid stuff...");
while (1) {
thread_yield_higher();
}
}
void *t_high_handler(void *arg)
{
(void) arg;
/* starting working loop after 500 ms */
xtimer_usleep(500U * US_PER_MS);
while (1) {
puts("t_high: allocating resource...");
mutex_lock(&res_mtx);
puts("t_high: got resource.");
xtimer_sleep(1);
puts("t_high: freeing resource...");
mutex_unlock(&res_mtx);
puts("t_high: freed resource.");
xtimer_sleep(1);
}
return NULL;
}
kernel_pid_t pid_low;
kernel_pid_t pid_mid;
kernel_pid_t pid_high;
int main(void)
{
xtimer_init();
mutex_init(&res_mtx);
puts("This is a scheduling test for Priority Inversion");
pid_low = thread_create(stack_low, sizeof(stack_low),
THREAD_PRIORITY_MAIN - 1,
THREAD_CREATE_STACKTEST,
t_low_handler, NULL,
"t_low");
pid_mid = thread_create(stack_mid, sizeof(stack_mid),
THREAD_PRIORITY_MAIN - 2,
THREAD_CREATE_STACKTEST,
t_mid_handler, NULL,
"t_mid");
pid_high = thread_create(stack_high, sizeof(stack_high),
THREAD_PRIORITY_MAIN - 3,
THREAD_CREATE_STACKTEST,
t_high_handler, NULL,
"t_high");
thread_sleep();
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