ubinos
mutextest01.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>
extern volatile unsigned int _g_ubik_test_result;
extern volatile unsigned int _g_ubik_test_count1;
extern volatile unsigned int _g_ubik_test_count2;
static void mutextest01_task1func(void * arg) {
int r;
int i;
unsigned int waitvalue = UBINOS__UBIK_TEST__TASKWAITTIMEMS * bsp_getbusywaitcountperms();
unsigned int waittick = ubik_timemstotick(UBINOS__UBIK_TEST__TASKWAITTIMEMS * UBINOS__UBIK_TEST__TASKLOOPCOUNT);
for (i=0; i<UBINOS__UBIK_TEST__TASKLOOPCOUNT * 1; i++) {
printf("1");
bsp_busywait(waitvalue);
}
printf("\r\n");
printf("task 1 tries to lock mutex\r\n");
if (0 != r) {
printf("task 1: fail at mutex_lock(), err=%d\r\n", r);
goto end0;
}
printf("task 1 locks mutex\r\n");
for (i=0; i<UBINOS__UBIK_TEST__TASKLOOPCOUNT * 1; i++) {
printf("1");
bsp_busywait(waitvalue);
}
printf("\r\n");
printf("task 1 unlocks mutex\r\n");
if (0 != r) {
printf("task 1: fail at mutex_unlock(), err=%d\r\n", r);
goto end0;
}
printf("task 1 tries to sleep (for %d ms)\r\n", ubik_ticktotimems(waittick));
task_sleep(waittick);
printf("\r\n");
printf("task 1 wakes up\r\n");
for (i=0; i<UBINOS__UBIK_TEST__TASKLOOPCOUNT * 1; i++) {
printf("1");
bsp_busywait(waitvalue);
}
printf("\r\n");
printf("task 1 tries to lock mutex with timeout (for %d ms)\r\n", ubik_ticktotimems(waittick));
if (UBIK_ERR__TIMEOUT != r) {
printf("task 1: fail at mutex_lock_timed(), err=%d\r\n", r);
goto end0;
}
printf("\r\n");
printf("task 1 wakes up\r\n");
for (i=0; i<UBINOS__UBIK_TEST__TASKLOOPCOUNT * 1; i++) {
printf("1");
bsp_busywait(waitvalue);
}
printf("\r\n");
end0:
printf("task 1 ends\r\n");
}
static void mutextest01_task2func(void * arg) {
int r;
int i;
unsigned int waitvalue = UBINOS__UBIK_TEST__TASKWAITTIMEMS * bsp_getbusywaitcountperms();
printf("task 2 tries to lock mutex\r\n");
if (0 != r) {
printf("task 2: fail at mutex_lock(), err=%d\r\n", r);
goto end0;
}
printf("task 2 locks mutex\r\n");
for (i=0; i<UBINOS__UBIK_TEST__TASKLOOPCOUNT * 2; i++) {
printf("2");
bsp_busywait(waitvalue);
}
printf("\r\n");
printf("task 2 tries to lock mutex again\r\n");
if (0 != r) {
printf("task 2: fail at mutex_lock(), err=%d\r\n", r);
goto end0;
}
printf("task 2 locks mutex again\r\n");
for (i=0; i<UBINOS__UBIK_TEST__TASKLOOPCOUNT * 1; i++) {
printf("2");
bsp_busywait(waitvalue);
}
printf("\r\n");
printf("task 2 unlocks mutex\r\n");
if (0 != r) {
printf("task 2: fail at mutex_unlock(), err=%d\r\n", r);
goto end0;
}
for (i=0; i<UBINOS__UBIK_TEST__TASKLOOPCOUNT * 1; i++) {
printf("2");
bsp_busywait(waitvalue);
}
printf("\r\n");
printf("task 2 unlocks mutex again\r\n");
if (0 != r) {
printf("task 2: fail at mutex_unlock(), err=%d\r\n", r);
goto end0;
}
printf("task 2 tries to lock mutex\r\n");
if (0 != r) {
printf("task 2: fail at mutex_lock(), err=%d\r\n", r);
goto end0;
}
printf("task 2 locks mutex\r\n");
for (i=0; i<UBINOS__UBIK_TEST__TASKLOOPCOUNT * 3; i++) {
printf("2");
bsp_busywait(waitvalue);
}
printf("\r\n");
printf("task 2 unlocks mutex\r\n");
if (0 != r) {
printf("task 2: fail at mutex_unlock(), err=%d\r\n", r);
goto end0;
}
end0:
printf("task 2 ends\r\n");
}
int r;
int r2;
unsigned int count1;
unsigned int count2;
unsigned int sleepvalue = ubik_timemstotick(UBINOS__UBIK_TEST__TASKWAITTIMEMS) * UBINOS__UBIK_TEST__TASKLOOPCOUNT / 3;
printf("\r\n");
printf("<test>\r\n");
printf("<name>ubik_test_mutextest01</name>\r\n");
printf("<description>Test on basic functions of mutex</description>\n\r");
printf("<message>\n\r");
if (0 != r) {
printf("fail at mutex_create(), err=%d\r\n", r);
r = -1;
goto end0;
}
printf("create task 2\r\n");
r = task_create(&_g_ubik_test_task_a[2-1], mutextest01_task2func, NULL, task_getpriority(NULL)-2, 0, "mutextest01 task 2");
if (0 != r) {
printf("fail at task_create(), err=%d\r\n", r);
r = -1;
goto end1;
}
task_sleep(sleepvalue);
printf("\r\n");
printf("checking\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("check point 01: ");
if (count1 != _g_ubik_test_count1 || count2 == _g_ubik_test_count2) {
printf("fail\r\n");
r = -1;
goto end2;
}
printf("pass\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("create task 1\r\n");
r = task_create(&_g_ubik_test_task_a[1-1], mutextest01_task1func, NULL, task_getpriority(NULL)-1, 0, "mutextest01 task 1");
if (0 != r) {
printf("fail at task_create(), err=%d\r\n", r);
r = -1;
goto end2;
}
task_sleep(sleepvalue);
printf("\r\n");
printf("checking\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("check point 02: ");
if (count1 == _g_ubik_test_count1 || count2 != _g_ubik_test_count2) {
printf("fail\r\n");
r = -1;
goto end2;
}
printf("pass\r\n");
task_sleep(sleepvalue * 2);
printf("\r\n");
printf("checking\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("check point 03: ");
if (count1 != _g_ubik_test_count1 || count2 == _g_ubik_test_count2) {
printf("fail\r\n");
r = -1;
goto end2;
}
printf("pass\r\n");
task_sleep(sleepvalue * 2);
printf("\r\n");
printf("checking\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("check point 04: ");
if (count1 != _g_ubik_test_count1 || count2 == _g_ubik_test_count2) {
printf("fail\r\n");
r = -1;
goto end2;
}
printf("pass\r\n");
task_sleep(sleepvalue * 2);
printf("\r\n");
printf("checking\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("check point 05: ");
if (count1 != _g_ubik_test_count1 || count2 == _g_ubik_test_count2) {
printf("fail\r\n");
r = -1;
goto end2;
}
printf("pass\r\n");
task_sleep(sleepvalue * 2);
printf("\r\n");
printf("checking\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("check point 06: ");
if (count1 == _g_ubik_test_count1 || count2 != _g_ubik_test_count2) {
printf("fail\r\n");
r = -1;
goto end2;
}
printf("pass\r\n");
task_sleep(sleepvalue * 2);
printf("\r\n");
printf("checking\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("check point 07: ");
if (count1 != _g_ubik_test_count1 || count2 == _g_ubik_test_count2) {
printf("fail\r\n");
r = -1;
goto end2;
}
printf("pass\r\n");
task_sleep(sleepvalue * 2);
printf("\r\n");
printf("checking\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("check point 08: ");
if (count1 == _g_ubik_test_count1 || count2 != _g_ubik_test_count2) {
printf("fail\r\n");
r = -1;
goto end2;
}
printf("pass\r\n");
task_sleep(sleepvalue * 2);
printf("\r\n");
printf("checking\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("check point 09: ");
if (count1 != _g_ubik_test_count1 || count2 == _g_ubik_test_count2) {
printf("fail\r\n");
r = -1;
goto end2;
}
printf("pass\r\n");
task_sleep(sleepvalue * 2);
printf("\r\n");
printf("checking\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("check point 10: ");
if (count1 == _g_ubik_test_count1 || count2 != _g_ubik_test_count2) {
printf("fail\r\n");
r = -1;
goto end2;
}
printf("pass\r\n");
task_sleep(sleepvalue * 2);
printf("\r\n");
printf("checking\r\n");
task_sleep(sleepvalue);
printf("\r\n");
printf("check point 11: ");
if (count1 != _g_ubik_test_count1 || count2 == _g_ubik_test_count2) {
printf("fail\r\n");
r = -1;
goto end2;
}
printf("pass\r\n");
r = 0;
end2:
if (0 != r2) {
printf("fail at task_join(), err=%d\r\n", r2);
r = -1;
}
end1:
if (0 != r2) {
printf("fail at mutex_delete(), err=%d\r\n", r2);
r = -1;
}
end0:
if (0 != r || 0 > _g_ubik_test_result) {
r = -1;
}
else {
r = 0;
}
printf("</message>\n\r");
printf("<result>");
if (0 == r) {
printf("pass");
}
else {
printf("fail");
}
printf("</result>\r\n");
printf("</test>\r\n");
printf("\r\n");
return r;
}
#endif /* !(UBINOS__UBIK_TEST__EXCLUDE_MUTEXTESTSET == 1) */
_g_ubik_test_task_a
task_pt _g_ubik_test_task_a[4]
시험 용 태스크 배열
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)
UBIK_ERR__TIMEOUT
#define UBIK_ERR__TIMEOUT
Definition: ubik.h:55
_g_ubik_test_count2
volatile unsigned int _g_ubik_test_count2
태스크 별 시험 진행 상황을 저장하는 전역 변수 2
ubik_timemstotick
unsigned int ubik_timemstotick(unsigned int timems)
mutex_delete
int mutex_delete(mutex_pt *mutex_p)
ubik_test.h
ubik test API
bsp_getbusywaitcountperms
unsigned int bsp_getbusywaitcountperms(void)
__task_tip_t
Definition: task.h:361
bsp_busywait
void bsp_busywait(unsigned int count)
__mutex_tip_t
Definition: mutex.h:257
ubik_ticktotimems
unsigned int ubik_ticktotimems(unsigned int tick)
_g_ubik_test_count1
volatile unsigned int _g_ubik_test_count1
태스크 별 시험 진행 상황을 저장하는 전역 변수 1
_g_ubik_test_result
volatile unsigned int _g_ubik_test_result
시험 결과를 저장하는 전역 변수
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)
ubik_test_mutextest01
int ubik_test_mutextest01(void)
Test on basic functions of mutex.
NULL
#define NULL
Definition: type.h:65
mutex_create
int mutex_create(mutex_pt *mutex_p)
stdlib.h
stdlib (Standard Library) API
task_sleep
int task_sleep(unsigned int tick)
mutex_lock_timed
int mutex_lock_timed(mutex_pt mutex, unsigned int tick)
_g_ubik_test_mutex
mutex_pt _g_ubik_test_mutex
시험 용 뮤택스