2008-06-26 10:54:55 +00:00
|
|
|
/*
|
2013-03-30 10:32:37 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
|
2008-06-26 10:54:55 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2008-06-26 10:54:55 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2008-06-26 10:54:55 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2008-06-26 10:54:55 +00:00
|
|
|
*/
|
|
|
|
|
2009-12-08 17:37:49 +00:00
|
|
|
#include "ch.h"
|
2008-06-26 10:54:55 +00:00
|
|
|
#include "test.h"
|
|
|
|
|
2009-05-07 15:24:47 +00:00
|
|
|
/**
|
|
|
|
* @page test_threads Threads and Scheduler test
|
|
|
|
*
|
2010-04-27 11:53:03 +00:00
|
|
|
* File: @ref testthd.c
|
|
|
|
*
|
2009-05-07 15:24:47 +00:00
|
|
|
* <h2>Description</h2>
|
2009-08-30 06:59:43 +00:00
|
|
|
* This module implements the test sequence for the @ref scheduler,
|
|
|
|
* @ref threads and @ref time subsystems.<br>
|
2009-05-07 15:24:47 +00:00
|
|
|
* Note that the tests on those subsystems are formally required but most of
|
2009-05-09 13:06:30 +00:00
|
|
|
* their functionality is already demonstrated because the test suite itself
|
|
|
|
* depends on them, anyway double check is good.
|
2009-05-07 15:24:47 +00:00
|
|
|
*
|
|
|
|
* <h2>Objective</h2>
|
2009-08-30 06:59:43 +00:00
|
|
|
* Objective of the test module is to cover 100% of the subsystems code.
|
2009-05-07 15:24:47 +00:00
|
|
|
*
|
|
|
|
* <h2>Preconditions</h2>
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* <h2>Test Cases</h2>
|
|
|
|
* - @subpage test_threads_001
|
|
|
|
* - @subpage test_threads_002
|
|
|
|
* - @subpage test_threads_003
|
|
|
|
* - @subpage test_threads_004
|
|
|
|
* .
|
|
|
|
* @file testthd.c
|
2009-05-09 14:42:26 +00:00
|
|
|
* @brief Threads and Scheduler test source file
|
2009-05-07 15:24:47 +00:00
|
|
|
* @file testthd.h
|
2009-05-09 14:42:26 +00:00
|
|
|
* @brief Threads and Scheduler test header file
|
2009-05-07 15:24:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @page test_threads_001 Ready List functionality #1
|
|
|
|
*
|
|
|
|
* <h2>Description</h2>
|
|
|
|
* Five threads, with increasing priority, are enqueued in the ready list
|
|
|
|
* and atomically executed.<br>
|
|
|
|
* The test expects the threads to perform their operations in increasing
|
2009-05-07 20:36:26 +00:00
|
|
|
* priority order regardless of the initial order.
|
2009-05-07 15:24:47 +00:00
|
|
|
*/
|
|
|
|
|
2008-06-26 10:54:55 +00:00
|
|
|
static msg_t thread(void *p) {
|
|
|
|
|
|
|
|
test_emit_token(*(char *)p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-13 08:15:51 +00:00
|
|
|
static void thd1_execute(void) {
|
2008-06-26 10:54:55 +00:00
|
|
|
|
2013-08-16 17:41:56 +00:00
|
|
|
threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX()-5, thread, "E");
|
|
|
|
threads[1] = chThdCreateStatic(wa[1], WA_SIZE, chThdGetPriorityX()-4, thread, "D");
|
|
|
|
threads[2] = chThdCreateStatic(wa[2], WA_SIZE, chThdGetPriorityX()-3, thread, "C");
|
|
|
|
threads[3] = chThdCreateStatic(wa[3], WA_SIZE, chThdGetPriorityX()-2, thread, "B");
|
|
|
|
threads[4] = chThdCreateStatic(wa[4], WA_SIZE, chThdGetPriorityX()-1, thread, "A");
|
2008-06-26 10:54:55 +00:00
|
|
|
test_wait_threads();
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_sequence(1, "ABCDE");
|
2008-06-26 10:54:55 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase testthd1 = {
|
2010-06-15 11:34:16 +00:00
|
|
|
"Threads, enqueuing test #1",
|
2009-02-16 18:22:49 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2009-04-13 08:15:51 +00:00
|
|
|
thd1_execute
|
2008-06-26 10:54:55 +00:00
|
|
|
};
|
|
|
|
|
2009-05-07 15:24:47 +00:00
|
|
|
/**
|
|
|
|
* @page test_threads_002 Ready List functionality #2
|
|
|
|
*
|
|
|
|
* <h2>Description</h2>
|
|
|
|
* Five threads, with pseudo-random priority, are enqueued in the ready list
|
|
|
|
* and atomically executed.<br>
|
|
|
|
* The test expects the threads to perform their operations in increasing
|
2009-05-07 20:36:26 +00:00
|
|
|
* priority order regardless of the initial order.
|
2009-05-07 15:24:47 +00:00
|
|
|
*/
|
|
|
|
|
2009-04-13 08:15:51 +00:00
|
|
|
static void thd2_execute(void) {
|
2008-06-26 10:54:55 +00:00
|
|
|
|
2013-08-16 17:41:56 +00:00
|
|
|
threads[1] = chThdCreateStatic(wa[1], WA_SIZE, chThdGetPriorityX()-4, thread, "D");
|
|
|
|
threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX()-5, thread, "E");
|
|
|
|
threads[4] = chThdCreateStatic(wa[4], WA_SIZE, chThdGetPriorityX()-1, thread, "A");
|
|
|
|
threads[3] = chThdCreateStatic(wa[3], WA_SIZE, chThdGetPriorityX()-2, thread, "B");
|
2013-08-17 11:04:57 +00:00
|
|
|
/* Done this way for coverage of chThdCreateI() and chThdStart().*/
|
2010-10-05 14:05:25 +00:00
|
|
|
chSysLock();
|
2013-08-16 17:41:56 +00:00
|
|
|
threads[2] = chThdCreateI(wa[2], WA_SIZE, chThdGetPriorityX()-3, thread, "C");
|
2010-10-05 14:05:25 +00:00
|
|
|
chSysUnlock();
|
2013-08-17 11:04:57 +00:00
|
|
|
chThdStart(threads[2]);
|
2008-06-26 10:54:55 +00:00
|
|
|
test_wait_threads();
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_sequence(1, "ABCDE");
|
2008-06-26 10:54:55 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase testthd2 = {
|
2010-06-15 11:34:16 +00:00
|
|
|
"Threads, enqueuing test #2",
|
2009-02-16 18:22:49 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2009-04-13 08:15:51 +00:00
|
|
|
thd2_execute
|
|
|
|
};
|
|
|
|
|
2009-05-07 15:24:47 +00:00
|
|
|
/**
|
|
|
|
* @page test_threads_003 Threads priority change test
|
|
|
|
*
|
|
|
|
* <h2>Description</h2>
|
|
|
|
* A series of priority changes are performed on the current thread in order
|
|
|
|
* to verify that the priority change happens as expected.<br>
|
2013-07-20 10:12:44 +00:00
|
|
|
* If the @p CH_CFG_USE_MUTEXES option is enabled then the priority changes are
|
2009-05-07 15:24:47 +00:00
|
|
|
* also tested under priority inheritance boosted priority state.
|
|
|
|
*/
|
|
|
|
|
2009-04-13 08:15:51 +00:00
|
|
|
static void thd3_execute(void) {
|
|
|
|
tprio_t prio, p1;
|
|
|
|
|
2013-08-16 17:41:56 +00:00
|
|
|
prio = chThdGetPriorityX();
|
2009-04-13 08:15:51 +00:00
|
|
|
p1 = chThdSetPriority(prio + 1);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(1, p1 == prio,
|
|
|
|
"unexpected returned priority level");
|
2013-08-16 17:41:56 +00:00
|
|
|
test_assert(2, chThdGetPriorityX() == prio + 1,
|
2009-04-25 11:12:10 +00:00
|
|
|
"unexpected priority level");
|
2009-04-13 08:15:51 +00:00
|
|
|
p1 = chThdSetPriority(p1);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(3, p1 == prio + 1,
|
|
|
|
"unexpected returned priority level");
|
2013-08-16 17:41:56 +00:00
|
|
|
test_assert(4, chThdGetPriorityX() == prio,
|
2009-04-25 11:12:10 +00:00
|
|
|
"unexpected priority level");
|
2009-04-16 15:06:49 +00:00
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#if CH_CFG_USE_MUTEXES || defined(__DOXYGEN__)
|
2009-04-16 15:06:49 +00:00
|
|
|
/* Simulates a priority boost situation (p_prio > p_realprio).*/
|
|
|
|
chSysLock();
|
2013-08-16 17:41:56 +00:00
|
|
|
chThdGetSelfX()->p_prio += 2;
|
2009-04-16 15:06:49 +00:00
|
|
|
chSysUnlock();
|
2013-08-16 17:41:56 +00:00
|
|
|
test_assert(5, chThdGetPriorityX() == prio + 2,
|
2009-04-25 11:12:10 +00:00
|
|
|
"unexpected priority level");
|
|
|
|
|
2009-04-16 15:06:49 +00:00
|
|
|
/* Tries to raise but below the boost level. */
|
|
|
|
p1 = chThdSetPriority(prio + 1);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(6, p1 == prio,
|
|
|
|
"unexpected returned priority level");
|
2013-08-16 17:41:56 +00:00
|
|
|
test_assert(7, chThdGetSelfX()->p_prio == prio + 2,
|
2009-04-25 11:12:10 +00:00
|
|
|
"unexpected priority level");
|
2013-08-16 17:41:56 +00:00
|
|
|
test_assert(8, chThdGetSelfX()->p_realprio == prio + 1,
|
2009-04-25 11:12:10 +00:00
|
|
|
"unexpected returned real priority level");
|
2009-04-16 15:06:49 +00:00
|
|
|
|
|
|
|
/* Tries to raise above the boost level. */
|
|
|
|
p1 = chThdSetPriority(prio + 3);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(9, p1 == prio + 1,
|
|
|
|
"unexpected returned priority level");
|
2013-08-16 17:41:56 +00:00
|
|
|
test_assert(10, chThdGetSelfX()->p_prio == prio + 3,
|
2009-04-25 11:12:10 +00:00
|
|
|
"unexpected priority level");
|
2013-08-16 17:41:56 +00:00
|
|
|
test_assert(11, chThdGetSelfX()->p_realprio == prio + 3,
|
2009-04-25 11:12:10 +00:00
|
|
|
"unexpected real priority level");
|
2009-04-16 15:06:49 +00:00
|
|
|
|
|
|
|
chSysLock();
|
2013-08-16 17:41:56 +00:00
|
|
|
chThdGetSelfX()->p_prio = prio;
|
|
|
|
chThdGetSelfX()->p_realprio = prio;
|
2009-04-16 15:06:49 +00:00
|
|
|
chSysUnlock();
|
|
|
|
#endif
|
2009-04-13 08:15:51 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase testthd3 = {
|
2010-06-15 11:34:16 +00:00
|
|
|
"Threads, priority change",
|
2009-04-13 08:15:51 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
thd3_execute
|
|
|
|
};
|
|
|
|
|
2009-05-07 15:24:47 +00:00
|
|
|
/**
|
|
|
|
* @page test_threads_004 Threads delays test
|
|
|
|
*
|
|
|
|
* <h2>Description</h2>
|
|
|
|
* Delay APIs and associated macros are tested, the invoking thread is verified
|
|
|
|
* to wake up at the exact expected time.
|
|
|
|
*/
|
|
|
|
|
2009-04-13 08:15:51 +00:00
|
|
|
static void thd4_execute(void) {
|
|
|
|
systime_t time;
|
|
|
|
|
2009-04-17 14:53:04 +00:00
|
|
|
test_wait_tick();
|
|
|
|
|
2009-04-13 08:15:51 +00:00
|
|
|
/* Timeouts in microseconds.*/
|
2013-06-16 12:21:30 +00:00
|
|
|
time = chVTGetSystemTime();
|
2009-04-13 08:15:51 +00:00
|
|
|
chThdSleepMicroseconds(100000);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_time_window(1, time + US2ST(100000), time + US2ST(100000) + 1);
|
2009-04-13 08:15:51 +00:00
|
|
|
|
|
|
|
/* Timeouts in milliseconds.*/
|
2013-06-16 12:21:30 +00:00
|
|
|
time = chVTGetSystemTime();
|
2009-04-13 08:15:51 +00:00
|
|
|
chThdSleepMilliseconds(100);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_time_window(2, time + MS2ST(100), time + MS2ST(100) + 1);
|
2009-04-13 08:15:51 +00:00
|
|
|
|
|
|
|
/* Timeouts in seconds.*/
|
2013-06-16 12:21:30 +00:00
|
|
|
time = chVTGetSystemTime();
|
2009-04-13 08:15:51 +00:00
|
|
|
chThdSleepSeconds(1);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_time_window(3, time + S2ST(1), time + S2ST(1) + 1);
|
2009-04-13 08:15:51 +00:00
|
|
|
|
|
|
|
/* Absolute timelines.*/
|
2013-06-16 12:21:30 +00:00
|
|
|
time = chVTGetSystemTime() + MS2ST(100);
|
2009-04-13 08:15:51 +00:00
|
|
|
chThdSleepUntil(time);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_time_window(4, time, time + 1);
|
2009-04-13 08:15:51 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase testthd4 = {
|
2010-06-15 11:34:16 +00:00
|
|
|
"Threads, delays",
|
2009-04-13 08:15:51 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
thd4_execute
|
2008-06-26 10:54:55 +00:00
|
|
|
};
|
2009-02-16 18:22:49 +00:00
|
|
|
|
2010-04-27 11:53:03 +00:00
|
|
|
/**
|
|
|
|
* @brief Test sequence for threads.
|
2009-02-16 18:22:49 +00:00
|
|
|
*/
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase * ROMCONST patternthd[] = {
|
2009-04-13 08:15:51 +00:00
|
|
|
&testthd1,
|
|
|
|
&testthd2,
|
|
|
|
&testthd3,
|
|
|
|
&testthd4,
|
2009-02-16 18:22:49 +00:00
|
|
|
NULL
|
|
|
|
};
|