diff --git a/core/mutex.c b/core/mutex.c index 297c8513a5ac..a79426ed1b4d 100644 --- a/core/mutex.c +++ b/core/mutex.c @@ -120,7 +120,7 @@ void mutex_unlock(struct mutex_t *mutex) void mutex_unlock_and_sleep(struct mutex_t *mutex) { - DEBUG("%s: unlocking mutex. val: %u pid: %u, and take a nap\n", active_thread->name, mutex->val, thread_pid); + DEBUG("%s: unlocking mutex. val: %u pid: %u, and taking a nap\n", active_thread->name, mutex->val, thread_pid); int irqstate = disableIRQ(); if (mutex->val != 0) { diff --git a/tests/test_mutex_unlock_and_sleep/Makefile b/tests/test_mutex_unlock_and_sleep/Makefile new file mode 100644 index 000000000000..2a99c8dfc8d7 --- /dev/null +++ b/tests/test_mutex_unlock_and_sleep/Makefile @@ -0,0 +1,4 @@ +export PROJECT = test_mutex_unlock_and_sleep +include ../Makefile.tests_common + +include $(RIOTBASE)/Makefile.include diff --git a/tests/test_mutex_unlock_and_sleep/main.c b/tests/test_mutex_unlock_and_sleep/main.c new file mode 100644 index 000000000000..fe946c9c375c --- /dev/null +++ b/tests/test_mutex_unlock_and_sleep/main.c @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2014 Hamburg University of Applied Siences (HAW) + * + * This file is subject to the terms and conditions of the GNU Lesser General + * Public License. See the file LICENSE in the top level directory for more + * details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief simple test application for atomic mutex unlocking and sleeping + * + * @author Martin Landsmann + * + * @} + */ + +#include +#include "thread.h" +#include "mutex.h" + +static mutex_t mutex; +static volatile int indicator, count; + +static char stack[KERNEL_CONF_STACKSIZE_MAIN]; +static void second_thread(void) +{ + while (1) { + mutex_lock(&mutex); + thread_wakeup(1); + indicator--; + mutex_unlock_and_sleep(&mutex); + } +} + +int main(void) +{ + indicator = 0; + count = 0; + mutex_init(&mutex); + + thread_create(stack, + KERNEL_CONF_STACKSIZE_MAIN, + PRIORITY_MAIN - 1, + CREATE_WOUT_YIELD | CREATE_STACKTEST, + second_thread, + "second_thread"); + + while (1) { + mutex_lock(&mutex); + thread_wakeup(2); + indicator++; + count++; + + if (indicator > 1 || indicator < -1) { + printf("Error, threads did not sleep properly. [indicator: %d]\n", indicator); + return -1; + } + + if ((count % 100000) == 0) { + printf("Still alive alternated [count: %dk] times.\n", count / 1000); + } + + mutex_unlock_and_sleep(&mutex); + } +}