Skip to content

Commit

Permalink
fixup! tests: add tests to reproduce RIOT-OS#10881
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed Jan 30, 2019
1 parent c3f9c60 commit 88af14b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
1 change: 1 addition & 0 deletions tests/thread_msg_block_race/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ BOARD_INSUFFICIENT_MEMORY := nucleo-f031k6

DISABLE_MODULE += auto_init
FEATURES_REQUIRED += periph_timer
USEMODULE += random

TEST_ON_CI_WHITELIST += all

Expand Down
53 changes: 21 additions & 32 deletions tests/thread_msg_block_race/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,45 @@

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "random.h"
#include "thread.h"
#include "msg.h"
#include "xtimer.h"

#define CANARY_TYPE (0x21fd)
#define CANARY_TYPE (0x21fd)

#define TIMER_FREQ (1000000LU)
#define TIMER_TIMEOUT (1U)
#define TIMER_FREQ (1000000LU)
#define TIMER_TIMEOUT_MIN (1U)
#define TIMER_TIMEOUT_MAX (100U)

static char _t1_stack[THREAD_STACKSIZE_DEFAULT];
static char _t2_stack[THREAD_STACKSIZE_DEFAULT];

static mutex_t _mutex = MUTEX_INIT_LOCKED;
static char _stack[THREAD_STACKSIZE_DEFAULT];

static kernel_pid_t _pid_main = KERNEL_PID_UNDEF;

static void _timer(void *arg, int channel)
static void _sched_next(void)
{
(void)arg;
(void)channel;
/* do context switch */
mutex_unlock(&_mutex);
timer_set(TIMER_DEV(0), 0, TIMER_TIMEOUT);
timer_set(TIMER_DEV(0), 0, random_uint32_range(TIMER_TIMEOUT_MIN,
TIMER_TIMEOUT_MAX));
}

static void *_thread1(void *arg)
static void _timer(void *arg, int channel)
{
(void) arg;

while (1) {
mutex_lock(&_mutex);
puts("nr1");
}

return NULL;
(void)arg;
(void)channel;
_sched_next();
}

static void *_thread2(void *arg)
static void *_thread(void *arg)
{
(void) arg;

while (1) {
msg_t msg = { .type = 0U };

msg_try_send(&msg, _pid_main);
puts("nr2");
write(STDOUT_FILENO, ".", 1U);
msg_send(&msg, _pid_main);
}

return NULL;
Expand All @@ -78,22 +70,19 @@ int main(void)
kernel_pid_t pid;

timer_init(TIMER_DEV(0), TIMER_FREQ, _timer, NULL);
random_init(timer_read(TIMER_DEV(0)));
puts("Test is \"successful\" if it runs forever without crashing");
_pid_main = sched_active_pid;
timer_set(TIMER_DEV(0), 0, TIMER_TIMEOUT);
pid = thread_create(_t1_stack, sizeof(_t1_stack), THREAD_PRIORITY_MAIN - 2,
THREAD_CREATE_STACKTEST, _thread1, NULL, "nr1");
assert(pid != KERNEL_PID_UNDEF);
pid = thread_create(_t2_stack, sizeof(_t2_stack), THREAD_PRIORITY_MAIN - 1,
THREAD_CREATE_STACKTEST, _thread2, NULL, "nr2");
_sched_next();
pid = thread_create(_stack, sizeof(_stack), THREAD_PRIORITY_MAIN - 1,
THREAD_CREATE_STACKTEST, _thread, NULL, "nr2");
assert(pid != KERNEL_PID_UNDEF);

while (1) {
msg_t msg = { .type = CANARY_TYPE };

msg_receive(&msg);
assert(msg.type != CANARY_TYPE);
puts("main");
}
return 0;
}

0 comments on commit 88af14b

Please sign in to comment.