2014-01-10 16:21:35 +01:00
|
|
|
/*
|
2014-01-27 20:43:54 +01:00
|
|
|
* Copyright (C) 2013 Christian Mehlis <mehlis@inf.fu-berlin.de>
|
2014-01-10 16:21:35 +01:00
|
|
|
* Copyright (C) 2013 René Kijewski <rene.kijewski@fu-berlin.de>
|
|
|
|
*
|
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-01-10 16:21:35 +01:00
|
|
|
*/
|
|
|
|
|
2014-02-03 23:03:13 +01:00
|
|
|
/**
|
|
|
|
* @ingroup tests
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Semaphore test application
|
|
|
|
*
|
|
|
|
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
|
|
|
* @author René Kijewski <rene.kijewski@fu-berlin.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "thread.h"
|
|
|
|
#include "semaphore.h"
|
|
|
|
|
|
|
|
#define SEMAPHORE_TEST_THREADS 10
|
2014-07-04 15:39:43 +02:00
|
|
|
char test1_thread_stack[KERNEL_CONF_STACKSIZE_MAIN];
|
|
|
|
char test2_thread_stack[SEMAPHORE_TEST_THREADS][KERNEL_CONF_STACKSIZE_MAIN];
|
2014-01-10 16:21:35 +01:00
|
|
|
|
|
|
|
sem_t s;
|
|
|
|
|
2014-03-04 20:20:01 +01:00
|
|
|
static void *test1_second_thread(void *arg)
|
2014-01-10 16:21:35 +01:00
|
|
|
{
|
2014-03-04 20:20:01 +01:00
|
|
|
(void) arg;
|
2014-01-10 16:21:35 +01:00
|
|
|
puts("second: sem_trywait");
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
if (sem_trywait(&s) == 0) {
|
|
|
|
puts("second: sem_trywait failed");
|
|
|
|
}
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
puts("second: sem_trywait done with == 0");
|
|
|
|
|
|
|
|
puts("second: wait for post");
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
if (sem_wait(&s) != 1) {
|
|
|
|
puts("second: sem_wait failed");
|
|
|
|
}
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
puts("second: sem was posted");
|
|
|
|
|
|
|
|
puts("second: end");
|
2014-03-04 20:20:01 +01:00
|
|
|
return NULL;
|
2014-01-10 16:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test1(void)
|
|
|
|
{
|
|
|
|
puts("first: sem_init");
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
if (sem_init(&s, 0, 0) != 0) {
|
|
|
|
puts("first: sem_init failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
puts("first: thread create");
|
2014-07-06 22:57:56 +02:00
|
|
|
kernel_pid_t pid = thread_create(test1_thread_stack,
|
|
|
|
sizeof(test1_thread_stack),
|
|
|
|
PRIORITY_MAIN - 1,
|
|
|
|
CREATE_STACKTEST | CREATE_WOUT_YIELD,
|
|
|
|
test1_second_thread,
|
|
|
|
NULL,
|
|
|
|
"second");
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
if (pid < 0) {
|
|
|
|
puts("first: thread create failed");
|
|
|
|
}
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
puts("first: thread created");
|
|
|
|
|
|
|
|
puts("first: sem_getvalue");
|
|
|
|
int val;
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
if (sem_getvalue(&s, &val) != 0 || val != 0) {
|
|
|
|
puts("first: sem_getvalue failed");
|
|
|
|
}
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
puts("first: sem_getvalue != 0");
|
|
|
|
|
|
|
|
puts("first: do yield");
|
|
|
|
thread_yield();
|
|
|
|
puts("first: done yield");
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
puts("first: sem_trywait");
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
if (sem_trywait(&s) != -1) {
|
|
|
|
puts("first: sem_trywait failed");
|
|
|
|
}
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
puts("first: sem_trywait done");
|
|
|
|
|
|
|
|
puts("first: sem_post");
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
if (sem_post(&s) != 1) {
|
|
|
|
puts("first: sem_post failed");
|
|
|
|
}
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
puts("first: sem_post done");
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
puts("first: sem_destroy");
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
if (sem_destroy(&s) != 0) {
|
|
|
|
puts("first: sem_destroy failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
puts("first: end");
|
|
|
|
}
|
|
|
|
|
2014-03-04 20:20:01 +01:00
|
|
|
static void *priority_sema_thread(void *arg)
|
2014-01-10 16:21:35 +01:00
|
|
|
{
|
2014-03-04 20:20:01 +01:00
|
|
|
(void) arg;
|
2014-01-10 16:21:35 +01:00
|
|
|
sem_wait(&s);
|
|
|
|
printf("Thread '%s' woke up.\n", thread_getname(thread_getpid()));
|
2014-03-04 20:20:01 +01:00
|
|
|
return NULL;
|
2014-01-10 16:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char names[SEMAPHORE_TEST_THREADS][16];
|
|
|
|
void test2(void)
|
|
|
|
{
|
|
|
|
puts("first: sem_init");
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
if (sem_init(&s, 0, 0) != 0) {
|
|
|
|
puts("first: sem_init failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < SEMAPHORE_TEST_THREADS; i++) {
|
|
|
|
int priority = PRIORITY_MAIN - (i + 3) % 10 + 1;
|
|
|
|
|
|
|
|
snprintf(names[i], sizeof(names[i]), "priority %d", priority);
|
|
|
|
printf("first: thread create: %d\n", priority);
|
2014-07-06 22:57:56 +02:00
|
|
|
kernel_pid_t pid = thread_create(test2_thread_stack[i],
|
|
|
|
sizeof(test2_thread_stack[i]),
|
|
|
|
priority,
|
|
|
|
CREATE_STACKTEST,
|
|
|
|
priority_sema_thread,
|
|
|
|
NULL,
|
|
|
|
names[i]);
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-08-06 09:44:31 +02:00
|
|
|
if (pid == KERNEL_PID_UNDEF) {
|
2014-01-10 16:21:35 +01:00
|
|
|
puts("first: thread create failed");
|
|
|
|
}
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
printf("first: thread created: %s (%d/%d)\n", names[i], i + 1, SEMAPHORE_TEST_THREADS);
|
|
|
|
}
|
|
|
|
|
|
|
|
puts("------------------------------------------");
|
2014-01-25 11:29:35 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
for (int i = 0; i < SEMAPHORE_TEST_THREADS; i++) {
|
|
|
|
printf("post no. %d\n", i);
|
|
|
|
sem_post(&s);
|
|
|
|
puts("Back in main thread.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
puts("#########################");
|
|
|
|
test1();
|
|
|
|
puts("#########################");
|
|
|
|
test2();
|
|
|
|
puts("#########################");
|
2014-06-20 20:00:20 +02:00
|
|
|
return 0;
|
2014-01-10 16:21:35 +01:00
|
|
|
}
|