2008-06-26 10:54:55 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.
|
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ch.h>
|
|
|
|
|
|
|
|
#include "test.h"
|
|
|
|
|
2009-02-07 19:14:15 +00:00
|
|
|
#if CH_USE_SEMAPHORES
|
2008-11-09 09:31:17 +00:00
|
|
|
|
2008-10-15 18:26:16 +00:00
|
|
|
#define ALLOWED_DELAY MS2ST(5)
|
2008-06-26 10:54:55 +00:00
|
|
|
|
|
|
|
static Semaphore sem1;
|
|
|
|
|
|
|
|
static char *sem1_gettest(void) {
|
|
|
|
|
2009-03-13 20:29:04 +00:00
|
|
|
return "Semaphores, enqueuing test";
|
2008-06-26 10:54:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sem1_setup(void) {
|
|
|
|
|
|
|
|
chSemInit(&sem1, 0);
|
|
|
|
}
|
|
|
|
|
2009-04-12 08:38:51 +00:00
|
|
|
static msg_t thread1(void *p) {
|
2008-06-26 10:54:55 +00:00
|
|
|
|
|
|
|
chSemWait(&sem1);
|
|
|
|
test_emit_token(*(char *)p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sem1_execute(void) {
|
|
|
|
|
2009-04-12 08:38:51 +00:00
|
|
|
threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriority()+5, thread1, "A");
|
|
|
|
threads[1] = chThdCreateStatic(wa[1], WA_SIZE, chThdGetPriority()+1, thread1, "B");
|
|
|
|
threads[2] = chThdCreateStatic(wa[2], WA_SIZE, chThdGetPriority()+3, thread1, "C");
|
|
|
|
threads[3] = chThdCreateStatic(wa[3], WA_SIZE, chThdGetPriority()+4, thread1, "D");
|
|
|
|
threads[4] = chThdCreateStatic(wa[4], WA_SIZE, chThdGetPriority()+2, thread1, "E");
|
2008-06-26 10:54:55 +00:00
|
|
|
chSemSignal(&sem1);
|
|
|
|
chSemSignal(&sem1);
|
|
|
|
chSemSignal(&sem1);
|
|
|
|
chSemSignal(&sem1);
|
|
|
|
chSemSignal(&sem1);
|
|
|
|
test_wait_threads();
|
2009-03-13 20:29:04 +00:00
|
|
|
#if CH_USE_SEMAPHORES_PRIORITY
|
|
|
|
test_assert_sequence("ADCEB");
|
|
|
|
#else
|
2008-06-26 10:54:55 +00:00
|
|
|
test_assert_sequence("ABCDE");
|
2009-03-13 20:29:04 +00:00
|
|
|
#endif
|
2008-06-26 10:54:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct testcase testsem1 = {
|
|
|
|
sem1_gettest,
|
|
|
|
sem1_setup,
|
2009-02-16 18:22:49 +00:00
|
|
|
NULL,
|
2008-06-26 10:54:55 +00:00
|
|
|
sem1_execute
|
|
|
|
};
|
|
|
|
|
2009-02-16 18:22:49 +00:00
|
|
|
#if CH_USE_SEMAPHORES_TIMEOUT
|
2008-06-26 10:54:55 +00:00
|
|
|
static char *sem2_gettest(void) {
|
|
|
|
|
|
|
|
return "Semaphores, timeout test";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sem2_setup(void) {
|
|
|
|
|
|
|
|
chSemInit(&sem1, 0);
|
|
|
|
}
|
|
|
|
|
2009-04-12 08:38:51 +00:00
|
|
|
static msg_t thread2(void *p) {
|
|
|
|
|
|
|
|
chThdSleepMilliseconds(50);
|
|
|
|
chSemSignal(&sem1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-26 10:54:55 +00:00
|
|
|
static void sem2_execute(void) {
|
|
|
|
int i;
|
|
|
|
systime_t target_time;
|
2009-03-07 15:32:40 +00:00
|
|
|
msg_t msg;
|
|
|
|
|
2009-04-12 08:38:51 +00:00
|
|
|
/*
|
|
|
|
* Testing special case TIME_IMMEDIATE.
|
|
|
|
*/
|
|
|
|
msg = chSemWaitTimeout(&sem1, TIME_IMMEDIATE);
|
2009-03-07 15:32:40 +00:00
|
|
|
test_assert(msg == RDY_TIMEOUT, "#1");
|
2009-04-12 08:38:51 +00:00
|
|
|
test_assert(isempty(&sem1.s_queue), "#2"); /* Queue not empty */
|
|
|
|
test_assert(sem1.s_cnt == 0, "#3"); /* Counter not zero */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Testing not timeout condition.
|
|
|
|
*/
|
|
|
|
threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriority()-1, thread2, "A");
|
|
|
|
msg = chSemWaitTimeout(&sem1, MS2ST(500));
|
|
|
|
test_wait_threads();
|
|
|
|
test_assert(msg == RDY_OK, "#4");
|
|
|
|
test_assert(isempty(&sem1.s_queue), "#5"); /* Queue not empty */
|
|
|
|
test_assert(sem1.s_cnt == 0, "#6"); /* Counter not zero */
|
2008-06-26 10:54:55 +00:00
|
|
|
|
2009-04-12 08:38:51 +00:00
|
|
|
/*
|
|
|
|
* Testing timeout condition.
|
|
|
|
*/
|
2009-03-10 15:31:58 +00:00
|
|
|
target_time = chTimeNow() + MS2ST(5 * 500);
|
2008-06-26 10:54:55 +00:00
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
test_emit_token('A' + i);
|
2009-03-07 15:32:40 +00:00
|
|
|
msg = chSemWaitTimeout(&sem1, MS2ST(500));
|
2009-04-12 08:38:51 +00:00
|
|
|
test_assert(msg == RDY_TIMEOUT, "#7");
|
|
|
|
test_assert(isempty(&sem1.s_queue), "#8"); /* Queue not empty */
|
|
|
|
test_assert(sem1.s_cnt == 0, "#9"); /* Counter not zero */
|
2008-06-26 10:54:55 +00:00
|
|
|
}
|
|
|
|
test_assert_sequence("ABCDE");
|
|
|
|
test_assert_time_window(target_time, target_time + ALLOWED_DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct testcase testsem2 = {
|
|
|
|
sem2_gettest,
|
|
|
|
sem2_setup,
|
2009-02-16 18:22:49 +00:00
|
|
|
NULL,
|
2008-06-26 10:54:55 +00:00
|
|
|
sem2_execute
|
|
|
|
};
|
2009-02-16 18:22:49 +00:00
|
|
|
#endif /* CH_USE_SEMAPHORES_TIMEOUT */
|
2008-11-09 09:31:17 +00:00
|
|
|
#endif /* CH_USE_SEMAPHORES */
|
2009-02-16 18:22:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test sequence for semaphores pattern.
|
|
|
|
*/
|
2009-02-28 10:38:32 +00:00
|
|
|
const struct testcase * const patternsem[] = {
|
2009-02-16 18:22:49 +00:00
|
|
|
#if CH_USE_SEMAPHORES
|
|
|
|
&testsem1,
|
|
|
|
#if CH_USE_SEMAPHORES_TIMEOUT
|
|
|
|
&testsem2,
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
};
|