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

tests: mutex_order: updated test and fixed README [2016.10-backport] #6059

Closed
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
2 changes: 0 additions & 2 deletions tests/mutex_order/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ include ../Makefile.tests_common

BOARD_INSUFFICIENT_MEMORY := stm32f0discovery weio nucleo-f030

USEMODULE += xtimer

include $(RIOTBASE)/Makefile.include
24 changes: 12 additions & 12 deletions tests/mutex_order/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
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:
When successful, you should see 5 different threads printing their PID and
priority. The thread with the lowest priority should be able to lock (and
unlock) the mutex 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
T3 (prio 6): trying to lock mutex now
T4 (prio 4): trying to lock mutex now
T5 (prio 0): trying to lock mutex now
T6 (prio 2): trying to lock mutex now
T7 (prio 1): trying to lock mutex now
T5 (prio 0): unlocking mutex now
T7 (prio 1): unlocking mutex now
T6 (prio 2): unlocking mutex now
T5 (prio 4): unlocking mutex now
T4 (prio 4): unlocking mutex now
T3 (prio 6): unlocking mutex now

Test END, check the order of priorities above.
```
Expand Down
14 changes: 8 additions & 6 deletions tests/mutex_order/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@

#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;

Expand All @@ -39,14 +37,14 @@ static void *lockme(void *arg)
(void)arg;
volatile thread_t *t = sched_active_thread;

printf("T%i (prio %i): locking mutex now\n",
printf("T%i (prio %i): trying to lock mutex now\n",
(int)t->pid, (int)t->priority);
mutex_lock(&testlock);
printf("T%i (prio %i): locked mutex now\n",
(int)t->pid, (int)t->priority);

xtimer_usleep(DELAY);
thread_yield();

printf("T%i (prio %i): unlocking mutex now\n",
(int)t->pid, (int)t->priority);
mutex_unlock(&testlock);

return NULL;
Expand All @@ -59,11 +57,15 @@ int main(void)

mutex_init(&testlock);

/* lock mutex, so that spawned threads have to wait */
mutex_lock(&testlock);
/* create threads */
for (unsigned i = 0; i < THREAD_NUMOF; i++) {
thread_create(stacks[i], sizeof(stacks[i]), prios[i], 0,
lockme, NULL, "t");
}
/* allow threads to lock the mutex */
mutex_unlock(&testlock);

mutex_lock(&testlock);
puts("\nTest END, check the order of priorities above.");
Expand Down
35 changes: 35 additions & 0 deletions tests/mutex_order/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3

# Copyright (C) 2016 Kaspar Schleiser <[email protected]>
# Copyright (C) 2016 Oliver Hahm <[email protected]>
#
# 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 os
import sys

sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
import testrunner

thread_prio = {
3: 6,
4: 4,
5: 0,
6: 2,
7: 1
}

def testfunc(child):
for k in thread_prio.keys():
child.expect(u"T%i \(prio %i\): trying to lock mutex now" % (k, thread_prio[k]))

last = -1
for i in range(len(thread_prio)):
child.expect(u"T\d+ \(prio (\d+)\): locked mutex now")
assert(int(child.match.group(1)) > last)
last = int(child.match.group(1))

if __name__ == "__main__":
sys.exit(testrunner.run(testfunc))