ubinos
mutextest00.c
/*
Copyright (C) 2009 Sung Ho Park
Contact: ubinos.org@gmail.com
This file is part of the lib_ubik_test component of the Ubinos.
GNU General Public License Usage
This file may be used under the terms of the GNU
General Public License version 3.0 as published by the Free Software
Foundation and appearing in the file license_gpl3.txt included in the
packaging of this file. Please review the following information to
ensure the GNU General Public License version 3.0 requirements will be
met: http://www.gnu.org/copyleft/gpl.html.
GNU Lesser General Public License Usage
Alternatively, this file may be used under the terms of the GNU Lesser
General Public License version 2.1 as published by the Free Software
Foundation and appearing in the file license_lgpl.txt included in the
packaging of this file. Please review the following information to
ensure the GNU Lesser General Public License version 2.1 requirements
will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Commercial Usage
Alternatively, licensees holding valid commercial licenses may
use this file in accordance with the commercial license agreement
provided with the software or, alternatively, in accordance with the
terms contained in a written agreement between you and rightful owner.
*/
#if !(UBINOS__UBIK_TEST__EXCLUDE_MUTEXTESTSET == 1)
#include <stdio.h>
#include <stdlib.h>
#define TASKBUSYWAITCOUNT 1
#define TASKLOOPCOUNT 10000
static mutex_pt _g_mutex = NULL;
static int _g_data = 0;
static void mutextest00_subtest1_task1func(void * arg) {
unsigned int i;
int temp;
for (i=0; i<TASKLOOPCOUNT; i++) {
temp = _g_data;
temp = temp + 1;
bsp_busywait(TASKBUSYWAITCOUNT);
_g_data = temp;
}
}
static void mutextest00_subtest1_task2func(void * arg) {
unsigned int i;
int temp;
for (i=0; i<TASKLOOPCOUNT; i++) {
temp = _g_data;
temp = temp - 1;
bsp_busywait(TASKBUSYWAITCOUNT);
_g_data = temp;
}
}
static int ubik_test_mutextest00_subtest1(void) {
int r;
int r2;
task_pt task_a[2];
_g_data = 0;
r = task_create(&task_a[0], mutextest00_subtest1_task1func, NULL, task_getpriority(NULL)-1, 0, "task 1");
if (0 != r) {
printf("fail at task_create(), err=%d\r\n", r);
r = -1;
goto end0;
}
r = task_create(&task_a[1], mutextest00_subtest1_task2func, NULL, task_getpriority(NULL)-1, 0, "task 2");
if (0 != r) {
printf("fail at task_create(), err=%d\r\n", r);
r = -1;
goto end1;
}
r = 0;
end1:
r2 = task_join(task_a, NULL, 2);
if (0 != r2) {
printf("fail at task_join(), err=%d\r\n", r2);
r = -1;
}
end0:
printf("data (none mutual exclusive use) : %d\r\n", _g_data);
return r;
}
static void mutextest00_subtest2_task1func(void * arg) {
unsigned int i;
int temp;
for (i=0; i<TASKLOOPCOUNT; i++) {
mutex_lock(_g_mutex);
temp = _g_data;
temp = temp + 1;
bsp_busywait(TASKBUSYWAITCOUNT);
_g_data = temp;
mutex_unlock(_g_mutex);
}
}
static void mutextest00_subtest2_task2func(void * arg) {
unsigned int i;
int temp;
for (i=0; i<TASKLOOPCOUNT; i++) {
mutex_lock(_g_mutex);
temp = _g_data;
temp = temp - 1;
bsp_busywait(TASKBUSYWAITCOUNT);
_g_data = temp;
mutex_unlock(_g_mutex);
}
}
static int ubik_test_mutextest00_subtest2(void) {
int r;
int r2;
task_pt task_a[2];
r = mutex_create(&_g_mutex);
if (0 != r) {
printf("fail at mutex_create(), err=%d\r\n", r);
r = -1;
goto end0;
}
_g_data = 0;
r = task_create(&task_a[0], mutextest00_subtest2_task1func, NULL, task_getpriority(NULL)-1, 0, "task 1");
if (0 != r) {
printf("fail at task_create(), err=%d\r\n", r);
r = -1;
goto end1;
}
r = task_create(&task_a[1], mutextest00_subtest2_task2func, NULL, task_getpriority(NULL)-1, 0, "task 2");
if (0 != r) {
printf("fail at task_create(), err=%d\r\n", r);
r = -1;
goto end2;
}
r = 0;
end2:
r2 = task_join(task_a, NULL, 2);
if (0 != r2) {
printf("fail at task_join(), err=%d\r\n", r2);
r = -1;
}
end1:
r2 = mutex_delete(&_g_mutex);
if (0 != r2) {
printf("fail at mutex_delete(), err=%d\r\n", r2);
r = -1;
}
end0:
printf("data (mutual exclusive use) : %d\r\n", _g_data);
return r;
}
int r;
r = ubik_test_mutextest00_subtest1();
if (0 != r) {
printf("fail at ubik_test_mutextest00_subtest1(), err=%d\r\n", r);
r = -1;
goto end0;
}
r = ubik_test_mutextest00_subtest2();
if (0 != r) {
printf("fail at ubik_test_mutextest00_subtest2(), err=%d\r\n", r);
r = -1;
goto end0;
}
r = 0;
end0:
return r;
}
#endif /* !(UBINOS__UBIK_TEST__EXCLUDE_MUTEXTESTSET == 1) */
ubik_test_mutextest00
int ubik_test_mutextest00(void)
Simple mutex example.
mutex_unlock
int mutex_unlock(mutex_pt mutex)
task_join
int task_join(task_pt *task_p, int *result_p, int count)
mutex_lock
int mutex_lock(mutex_pt mutex)
mutex_delete
int mutex_delete(mutex_pt *mutex_p)
ubik_test.h
ubik test API
__task_tip_t
Definition: task.h:361
bsp_busywait
void bsp_busywait(unsigned int count)
__mutex_tip_t
Definition: mutex.h:257
task_create
int task_create(task_pt *task_p, taskfunc_ft func, void *arg, int priority, unsigned int stackdepth, const char *name)
task_getpriority
int task_getpriority(task_pt task)
NULL
#define NULL
Definition: type.h:65
mutex_create
int mutex_create(mutex_pt *mutex_p)
stdlib.h
stdlib (Standard Library) API