From 47f0e8fb7f337b65f848bf4cd59577d3967d0862 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Tue, 11 Feb 2014 09:21:53 +0000 Subject: [PATCH] git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/kernel_3_dev@6699 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/nil/NIL-STM32F303-DISCOVERY/main.c | 3 +- demos/nil/NIL-STM32F303-DISCOVERY/nilconf.h | 2 +- os/nil/src/nil.c | 1 - test/lib/ch_test.h | 6 + test/nil/test_root.c | 27 ++++ test/nil/test_root.h | 3 + test/nil/test_sequence_002.c | 135 ++++++++++++++++---- 7 files changed, 150 insertions(+), 27 deletions(-) diff --git a/demos/nil/NIL-STM32F303-DISCOVERY/main.c b/demos/nil/NIL-STM32F303-DISCOVERY/main.c index db842415f..2b648ff29 100644 --- a/demos/nil/NIL-STM32F303-DISCOVERY/main.c +++ b/demos/nil/NIL-STM32F303-DISCOVERY/main.c @@ -78,7 +78,7 @@ static THD_FUNCTION(Thread2, arg) { } /* - * Hello thread. + * Tester thread. */ THD_WORKING_AREA(waThread3, 128); THD_FUNCTION(Thread3, arg) { @@ -111,6 +111,7 @@ THD_FUNCTION(Thread3, arg) { THD_TABLE_BEGIN THD_TABLE_ENTRY(waThread1, "blinker1", Thread1, NULL) THD_TABLE_ENTRY(waThread2, "blinker2", Thread2, NULL) + THD_TABLE_ENTRY(wa_test_support, "test_support", test_support, NULL) THD_TABLE_ENTRY(waThread3, "tester", Thread3, NULL) THD_TABLE_END diff --git a/demos/nil/NIL-STM32F303-DISCOVERY/nilconf.h b/demos/nil/NIL-STM32F303-DISCOVERY/nilconf.h index 35504197e..76028b31d 100644 --- a/demos/nil/NIL-STM32F303-DISCOVERY/nilconf.h +++ b/demos/nil/NIL-STM32F303-DISCOVERY/nilconf.h @@ -40,7 +40,7 @@ * @note This number is not inclusive of the idle thread which is * Implicitly handled. */ -#define NIL_CFG_NUM_THREADS 3 +#define NIL_CFG_NUM_THREADS 4 /** @} */ diff --git a/os/nil/src/nil.c b/os/nil/src/nil.c index bafcb63cd..9141129cb 100644 --- a/os/nil/src/nil.c +++ b/os/nil/src/nil.c @@ -566,7 +566,6 @@ void chSemSignalI(semaphore_t *sp) { while (true) { /* Is this thread waiting on this semaphore?*/ if (tr->u1.semp == sp) { - sp->cnt++; chDbgAssert(NIL_THD_IS_WTSEM(tr), "not waiting"); diff --git a/test/lib/ch_test.h b/test/lib/ch_test.h index c8e03490c..2795f48dc 100644 --- a/test/lib/ch_test.h +++ b/test/lib/ch_test.h @@ -181,6 +181,12 @@ extern "C" { /* Module inline functions. */ /*===========================================================================*/ +/*===========================================================================*/ +/* Late inclusions. */ +/*===========================================================================*/ + +#include "test_root.h" + #endif /* _CH_TEST_H_ */ /** @} */ diff --git a/test/nil/test_root.c b/test/nil/test_root.c index fb835b88d..cd3c8b681 100644 --- a/test/nil/test_root.c +++ b/test/nil/test_root.c @@ -43,4 +43,31 @@ const testcase_t * const *test_suite[] = { /* Shared code. */ /*===========================================================================*/ +semaphore_t gsem1, gsem2; + +/* + * Support thread. + */ +THD_WORKING_AREA(wa_test_support, 128); +THD_FUNCTION(test_support, arg) { + + (void)arg; + + /* Initializing global resources.*/ + chSemObjectInit(&gsem1, 0); + chSemObjectInit(&gsem2, 0); + + /* Waiting for button push and activation of the test suite.*/ + while (true) { + chSysLock(); + if (chSemGetCounterI(&gsem1) < 0) + chSemSignalI(&gsem1); + chSemResetI(&gsem2, 0); + chSchRescheduleS(); + chSysUnlock(); + + chThdSleepMilliseconds(500); + } +} + /** @} */ diff --git a/test/nil/test_root.h b/test/nil/test_root.h index 0d000c415..51a100922 100644 --- a/test/nil/test_root.h +++ b/test/nil/test_root.h @@ -47,6 +47,9 @@ extern const testcase_t * const *test_suite[]; #ifdef __cplusplus extern "C" { #endif + extern semaphore_t gsem1, gsem2; + extern THD_WORKING_AREA(wa_test_support, 128); + THD_FUNCTION(test_support, arg); #ifdef __cplusplus } #endif diff --git a/test/nil/test_sequence_002.c b/test/nil/test_sequence_002.c index cc9f03b08..9fa425c18 100644 --- a/test/nil/test_sequence_002.c +++ b/test/nil/test_sequence_002.c @@ -19,12 +19,13 @@ #include "test_root.h" /** - * @page test_sequence_002 Semaphores functionality + * @page test_sequence_002 Synchronization primitives * * File: @ref test_sequence_002.c * *

Description

- * This sequence tests the ChibiOS/NIL functionalities related to semaphores. + * This sequence tests the ChibiOS/NIL functionalities related to + * threads synchronization. * *

Test Cases

* - @subpage test_002_001 @@ -54,12 +55,12 @@ static semaphore_t sem1; * None. * *

Test Steps

- * - The function chSemWait() is invoked, the Semaphore counter is tested - * for correct value after the call. - * - The function chSemSignal() is invoked, the Semaphore counter is tested - * for correct value after the call. - * - The function chSemReset() is invoked, the Semaphore counter is tested - * for correct value after the call. + * - The function chSemWait() is invoked, after return the counter and + * the returned message are tested. + * - The function chSemSignal() is invoked, after return the counter + * is tested. + * - The function chSemReset() is invoked, after return the counter + * is tested. * . */ @@ -75,17 +76,21 @@ static void test_002_001_teardown(void) { static void test_002_001_execute(void) { - /* The function chSemWait() is invoked, the Semaphore counter is tested - for correct value after the call.*/ + /* The function chSemWait() is invoked, after return the counter and + the returned message are tested.*/ test_set_step(1); { - chSemWait(&sem1); + msg_t msg; + + msg = chSemWait(&sem1); test_assert_lock(chSemGetCounterI(&sem1) == 0, "wrong counter value"); + test_assert(MSG_OK == msg, + "wrong timeout message"); } - /* The function chSemSignal() is invoked, the Semaphore counter is tested - for correct value after the call.*/ + /* The function chSemSignal() is invoked, after return the counter + is tested.*/ test_set_step(2); { chSemSignal(&sem1); @@ -93,8 +98,8 @@ static void test_002_001_execute(void) { "wrong counter value"); } - /* The function chSemReset() is invoked, the Semaphore counter is tested - for correct value after the call.*/ + /* The function chSemReset() is invoked, after return the counter + is tested.*/ test_set_step(3); { chSemReset(&sem1, 2); @@ -113,17 +118,16 @@ static const testcase_t test_002_001 = { #if TRUE || defined(__DOXYGEN__) /** - * @page test_002_002 Semaphore timeouts + * @page test_002_002 Semaphore primitives, with state change * *

Description

- * Timeouts on semaphores are tested. + * Wait, Signal and Reset primitives are tested. The testing thread + * triggers a state change. * *

Conditions

* None. * *

Test Steps

- * - The function chSemWaitTimeout() is invoked, after return the system - * time, the counter and the returned message are tested. * . */ @@ -138,6 +142,71 @@ static void test_002_002_teardown(void) { } static void test_002_002_execute(void) { + + /* The function chSemWait() is invoked, after return the counter and + the returned message are tested. The semaphore is signaled by another + thread.*/ + test_set_step(1); + { + msg_t msg; + + msg = chSemWait(&gsem1); + test_assert_lock(chSemGetCounterI(&gsem1) == 0, + "wrong counter value"); + test_assert(MSG_OK == msg, + "wrong timeout message"); + } + + /* The function chSemWait() is invoked, after return the counter and + the returned message are tested. The semaphore is reset by another + thread.*/ + test_set_step(2); + { + msg_t msg; + + msg = chSemWait(&gsem2); + test_assert_lock(chSemGetCounterI(&gsem2) == 0, + "wrong counter value"); + test_assert(MSG_RESET == msg, + "wrong timeout message"); + } +} + +static const testcase_t test_002_002 = { + "semaphore primitives, with state change", + test_002_002_setup, + test_002_002_teardown, + test_002_002_execute +}; +#endif /* TRUE */ + +#if TRUE || defined(__DOXYGEN__) +/** + * @page test_002_003 Semaphores timeout + * + *

Description

+ * Timeout on semaphores is tested. + * + *

Conditions

+ * None. + * + *

Test Steps

+ * - The function chSemWaitTimeout() is invoked, after return the system + * time, the counter and the returned message are tested. + * . + */ + +static void test_002_003_setup(void) { + + chSemObjectInit(&sem1, 0); +} + +static void test_002_003_teardown(void) { + + chSemReset(&sem1, 0); +} + +static void test_002_003_execute(void) { systime_t time; msg_t msg; @@ -155,13 +224,28 @@ static void test_002_002_execute(void) { test_assert(MSG_TIMEOUT == msg, "wrong timeout message"); } + + /* The function chSemWait() is invoked, after return the system + time, the counter and the returned message are tested.*/ + test_set_step(1); + { + time = chVTGetSystemTimeX(); + msg = chSemWaitTimeout(&sem1, 100); + test_assert_time_window(time + 100, + time + 100 + 1, + "out of time window"); + test_assert_lock(chSemGetCounterI(&sem1) == 0, + "wrong counter value"); + test_assert(MSG_TIMEOUT == msg, + "wrong timeout message"); + } } -static const testcase_t test_002_002 = { - "semaphore timeouts", - test_002_002_setup, - test_002_002_teardown, - test_002_002_execute +static const testcase_t test_002_003 = { + "semaphores timeout", + test_002_003_setup, + test_002_003_teardown, + test_002_003_execute }; #endif /* TRUE */ @@ -178,6 +262,9 @@ const testcase_t * const test_sequence_002[] = { #endif #if TRUE || defined(__DOXYGEN__) &test_002_002, +#endif +#if TRUE || defined(__DOXYGEN__) + &test_002_003, #endif NULL };