2014-02-24 16:09:37 +01:00
|
|
|
/*
|
2017-12-06 21:20:10 +01:00
|
|
|
* Copyright (C) 2014-2017 HAW Hamburg
|
2014-02-24 16:09:37 +01:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* 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.
|
2014-02-24 16:09:37 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup tests
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief simple test application for atomic mutex unlocking and sleeping
|
|
|
|
*
|
|
|
|
* @author Martin Landsmann <martin.landsmann@haw-hamburg.de>
|
2017-12-06 21:20:10 +01:00
|
|
|
* @author Sebastian Meiling <s@mlng.net>
|
2014-02-24 16:09:37 +01:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "thread.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
|
2014-07-17 12:27:29 +02:00
|
|
|
static mutex_t mutex = MUTEX_INIT;
|
2017-12-06 21:20:10 +01:00
|
|
|
static volatile int indicator;
|
2014-10-01 03:57:54 +02:00
|
|
|
static kernel_pid_t main_pid;
|
2017-12-06 21:20:10 +01:00
|
|
|
static char stack[THREAD_STACKSIZE_DEFAULT];
|
2014-02-24 16:09:37 +01:00
|
|
|
|
2018-05-08 11:37:00 +02:00
|
|
|
#ifdef BOARD_NATIVE
|
2018-05-08 11:34:42 +02:00
|
|
|
static const unsigned KITERATIONS = 100;
|
2018-05-08 11:37:00 +02:00
|
|
|
#else
|
|
|
|
static const unsigned KITERATIONS = 10;
|
|
|
|
#endif
|
2018-05-08 11:34:42 +02:00
|
|
|
|
2014-03-04 20:20:01 +01:00
|
|
|
static void *second_thread(void *arg)
|
2014-02-24 16:09:37 +01:00
|
|
|
{
|
2014-03-04 20:20:01 +01:00
|
|
|
(void) arg;
|
2014-02-24 16:09:37 +01:00
|
|
|
while (1) {
|
|
|
|
mutex_lock(&mutex);
|
2014-10-01 03:57:54 +02:00
|
|
|
thread_wakeup(main_pid);
|
2014-02-24 16:09:37 +01:00
|
|
|
indicator--;
|
|
|
|
mutex_unlock_and_sleep(&mutex);
|
|
|
|
}
|
2014-03-04 20:20:01 +01:00
|
|
|
return NULL;
|
2014-02-24 16:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2017-12-06 21:20:10 +01:00
|
|
|
uint32_t count = 0;
|
2018-05-08 11:34:42 +02:00
|
|
|
uint32_t kcount = 0;
|
2014-02-24 16:09:37 +01:00
|
|
|
|
2017-12-06 21:20:10 +01:00
|
|
|
indicator = 0;
|
2014-10-01 03:57:54 +02:00
|
|
|
main_pid = thread_getpid();
|
|
|
|
|
|
|
|
kernel_pid_t second_pid = thread_create(stack,
|
2014-07-09 07:13:56 +02:00
|
|
|
sizeof(stack),
|
2015-04-28 20:02:05 +02:00
|
|
|
THREAD_PRIORITY_MAIN - 1,
|
2015-12-02 12:00:37 +01:00
|
|
|
THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
|
2014-02-24 16:09:37 +01:00
|
|
|
second_thread,
|
2014-03-04 20:20:01 +01:00
|
|
|
NULL,
|
2014-02-24 16:09:37 +01:00
|
|
|
"second_thread");
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
mutex_lock(&mutex);
|
2014-10-01 03:57:54 +02:00
|
|
|
thread_wakeup(second_pid);
|
2014-02-24 16:09:37 +01:00
|
|
|
indicator++;
|
|
|
|
count++;
|
|
|
|
|
2017-12-06 21:20:10 +01:00
|
|
|
if ((indicator > 1) || (indicator < -1)) {
|
|
|
|
printf("[ERROR] threads did not sleep properly (%d).\n", indicator);
|
|
|
|
return 1;
|
2014-02-24 16:09:37 +01:00
|
|
|
}
|
2018-05-08 11:34:42 +02:00
|
|
|
if (count == (KITERATIONS * 1000)) {
|
|
|
|
count = 0;
|
|
|
|
kcount += KITERATIONS;
|
|
|
|
printf("[ALIVE] alternated %"PRIu32"k times.\n", kcount);
|
2014-02-24 16:09:37 +01:00
|
|
|
}
|
|
|
|
mutex_unlock_and_sleep(&mutex);
|
|
|
|
}
|
|
|
|
}
|