1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

test/sys_sched_round_robin: use sleep instead of mutex

to avoid priority-inversion screwing up the test
This commit is contained in:
Karl Fessel 2021-11-01 21:22:06 +01:00
parent 64b783b9fa
commit 2594032163
2 changed files with 16 additions and 20 deletions

View File

@ -19,17 +19,18 @@
#include <stdint.h>
#include "thread.h"
#include "mutex.h"
static mutex_t _shared_mutex;
static kernel_pid_t main_pid;
void * thread_mutex_unlock(void *d)
void * thread_wakeup_main(void *d)
{
(void) d;
puts("mutex_thread yield");
puts("wakup_thread yield");
thread_yield();
puts("unlock mutex");
mutex_unlock(&_shared_mutex);
while (puts("wakeup main"), thread_wakeup(main_pid) == (int)STATUS_NOT_FOUND) {
thread_yield();
};
return NULL;
}
@ -45,26 +46,21 @@ void * thread_bad(void *d)
/* each thread gets a stack */
static char stack[2][THREAD_STACKSIZE_DEFAULT];
/* with priority inversion this should be set to THREAD_PRIORITY_MAIN
* until then a lower priority (higher number) is the better choice */
const uint8_t shared_prio = THREAD_PRIORITY_MAIN + 1;
/* shared priority of the threads - lower than main waiting for it to sleep */
static const uint8_t shared_prio = THREAD_PRIORITY_MAIN + 1;
int main(void)
{
puts("starting threads");
mutex_init(&_shared_mutex);
main_pid = thread_getpid();
thread_create(stack[0], sizeof(stack[0]), shared_prio, THREAD_CREATE_STACKTEST,
thread_mutex_unlock, NULL, "TMutex");
thread_wakeup_main, NULL, "TWakeup");
thread_create(stack[1], sizeof(stack[1]), shared_prio, THREAD_CREATE_STACKTEST,
thread_bad, NULL, "TBad");
puts("main is going to sleep");
thread_sleep();
puts("double locking mutex");
mutex_lock(&_shared_mutex);
mutex_lock(&_shared_mutex);
/* success: mutex got unlocked, which means thread "TMutex" got cpu time
/* success: main got woken up again which means "TWakup" got cpu time
* even though "TBad" was trying to hog the whole CPU */
puts("[SUCCESS]");
}

View File

@ -12,8 +12,8 @@ from testrunner import run
def testfunc(child):
child.expect_exact("starting threads")
child.expect_exact("double locking mutex")
child.expect_exact("unlock mutex")
child.expect_exact("main is going to sleep")
child.expect_exact("wakeup main")
child.expect_exact("[SUCCESS]")