diff --git a/tests/mutex_order/Makefile b/tests/mutex_order/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..533ce94204b78294a3806c98af01d50ff9714fa9 --- /dev/null +++ b/tests/mutex_order/Makefile @@ -0,0 +1,8 @@ +APPLICATION = mutex_order +include ../Makefile.tests_common + +BOARD_INSUFFICIENT_MEMORY := stm32f0discovery weio + +USEMODULE += xtimer + +include $(RIOTBASE)/Makefile.include diff --git a/tests/mutex_order/README.md b/tests/mutex_order/README.md new file mode 100644 index 0000000000000000000000000000000000000000..7e85055c13eb283efa546b5f9b2c2ac4c8d51214 --- /dev/null +++ b/tests/mutex_order/README.md @@ -0,0 +1,29 @@ +Expected result +=============== +When successful, you should see 10 different threads printing their PID and +priority. The thread with the lowest priority will print its status first, +followed by the other threads in the order of their priority (highest next). The +output should look like the following: + +``` +main(): This is RIOT! (Version: xxx) +Mutex order test +Please refer to the README.md for more information + +T3 (prio 6): locking mutex now +T4 (prio 0): locking mutex now +T5 (prio 4): locking mutex now +T6 (prio 2): locking mutex now +T7 (prio 1): locking mutex now +T3 (prio 6): unlocking mutex now +T4 (prio 0): unlocking mutex now +T7 (prio 1): unlocking mutex now +T6 (prio 2): unlocking mutex now +T5 (prio 4): unlocking mutex now + +Test END, check the order of priorities above. +``` + +Background +========== +This test application stresses a mutex with a number of threads waiting on it. diff --git a/tests/mutex_order/main.c b/tests/mutex_order/main.c new file mode 100644 index 0000000000000000000000000000000000000000..514379227f179b4a4ddef039506c77639ef13d15 --- /dev/null +++ b/tests/mutex_order/main.c @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2016 Freie 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 Test application for testing mutexes + * + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> + * @} + */ + +#include <stdio.h> + +#include "mutex.h" +#include "thread.h" +#include "xtimer.h" + +#define THREAD_NUMOF (5U) +#define DELAY (10 * 1000U) /* 10ms */ + +extern volatile thread_t *sched_active_thread; + +static char stacks[THREAD_NUMOF][THREAD_STACKSIZE_MAIN]; + +static const char prios[THREAD_NUMOF] = {THREAD_PRIORITY_MAIN - 1, 4, 0, 2, 1}; + +static mutex_t testlock; + +static void *lockme(void *arg) +{ + (void)arg; + volatile thread_t *t = sched_active_thread; + + printf("T%i (prio %i): locking mutex now\n", + (int)t->pid, (int)t->priority); + mutex_lock(&testlock); + + xtimer_usleep(DELAY); + + printf("T%i (prio %i): unlocking mutex now\n", + (int)t->pid, (int)t->priority); + mutex_unlock(&testlock); + + return NULL; +} + +int main(void) +{ + puts("Mutex order test"); + puts("Please refer to the README.md for more information\n"); + + mutex_init(&testlock); + + /* create threads */ + for (unsigned i = 0; i < THREAD_NUMOF; i++) { + thread_create(stacks[i], sizeof(stacks[i]), prios[i], 0, + lockme, NULL, "t"); + } + + mutex_lock(&testlock); + puts("\nTest END, check the order of priorities above."); + + return 0; +}