2008-09-23 15:04:14 +00:00
|
|
|
/*
|
2013-03-30 10:32:37 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
|
2008-09-23 15:04:14 +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-09-23 15:04:14 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2008-09-23 15:04:14 +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-09-23 15:04:14 +00:00
|
|
|
*/
|
|
|
|
|
2009-12-08 17:37:49 +00:00
|
|
|
#include "ch.h"
|
2008-09-23 15:04:14 +00:00
|
|
|
#include "test.h"
|
|
|
|
|
2009-05-08 14:06:45 +00:00
|
|
|
/**
|
2009-05-16 07:52:12 +00:00
|
|
|
* @page test_dynamic Dynamic APIs test
|
2009-05-08 14:06:45 +00:00
|
|
|
*
|
2010-04-27 11:53:03 +00:00
|
|
|
* File: @ref testdyn.c
|
|
|
|
*
|
2009-05-08 14:06:45 +00:00
|
|
|
* <h2>Description</h2>
|
|
|
|
* This module implements the test sequence for the dynamic thread creation
|
|
|
|
* APIs.
|
|
|
|
*
|
|
|
|
* <h2>Objective</h2>
|
2009-08-30 06:59:43 +00:00
|
|
|
* Objective of the test module is to cover 100% of the dynamic APIs code.
|
2009-05-08 14:06:45 +00:00
|
|
|
*
|
|
|
|
* <h2>Preconditions</h2>
|
|
|
|
* The module requires the following kernel options:
|
2013-07-20 10:12:44 +00:00
|
|
|
* - @p CH_CFG_USE_DYNAMIC
|
|
|
|
* - @p CH_CFG_USE_HEAP
|
|
|
|
* - @p CH_CFG_USE_MEMPOOLS
|
2009-05-08 14:06:45 +00:00
|
|
|
* .
|
|
|
|
* In case some of the required options are not enabled then some or all tests
|
|
|
|
* may be skipped.
|
|
|
|
*
|
|
|
|
* <h2>Test Cases</h2>
|
|
|
|
* - @subpage test_dynamic_001
|
|
|
|
* - @subpage test_dynamic_002
|
2010-05-04 12:31:05 +00:00
|
|
|
* - @subpage test_dynamic_003
|
2009-05-08 14:06:45 +00:00
|
|
|
* .
|
|
|
|
* @file testdyn.c
|
|
|
|
* @brief Dynamic thread APIs test source file
|
|
|
|
* @file testdyn.h
|
|
|
|
* @brief Dynamic thread APIs test header file
|
|
|
|
*/
|
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#if CH_CFG_USE_DYNAMIC || defined(__DOXYGEN__)
|
|
|
|
#if (CH_CFG_USE_HEAP && !CH_CFG_USE_MALLOC_HEAP) || defined(__DOXYGEN__)
|
2013-07-19 14:51:35 +00:00
|
|
|
static memory_heap_t heap1;
|
2010-05-08 10:30:38 +00:00
|
|
|
#endif
|
2013-07-20 10:12:44 +00:00
|
|
|
#if CH_CFG_USE_MEMPOOLS || defined(__DOXYGEN__)
|
2013-07-19 14:51:35 +00:00
|
|
|
static memory_pool_t mp1;
|
2010-05-08 10:30:38 +00:00
|
|
|
#endif
|
2008-09-23 15:04:14 +00:00
|
|
|
|
2009-05-08 14:06:45 +00:00
|
|
|
/**
|
|
|
|
* @page test_dynamic_001 Threads creation from Memory Heap
|
|
|
|
*
|
|
|
|
* <h2>Description</h2>
|
|
|
|
* Two threads are started by allocating the memory from the Memory Heap then
|
|
|
|
* the remaining heap space is arbitrarily allocated and a third tread startup
|
|
|
|
* is attempted.<br>
|
|
|
|
* The test expects the first two threads to successfully start and the last
|
|
|
|
* one to fail.
|
|
|
|
*/
|
|
|
|
|
2008-09-23 15:04:14 +00:00
|
|
|
static msg_t thread(void *p) {
|
|
|
|
|
|
|
|
test_emit_token(*(char *)p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#if (CH_CFG_USE_HEAP && !CH_CFG_USE_MALLOC_HEAP) || defined(__DOXYGEN__)
|
2009-10-16 17:45:19 +00:00
|
|
|
static void dyn1_setup(void) {
|
|
|
|
|
2013-07-20 07:24:12 +00:00
|
|
|
chHeapObjectInit(&heap1, test.buffer, sizeof(union test_buffers));
|
2009-10-16 17:45:19 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 15:04:14 +00:00
|
|
|
static void dyn1_execute(void) {
|
|
|
|
size_t n, sz;
|
2009-05-03 10:49:34 +00:00
|
|
|
void *p1;
|
2013-08-16 17:41:56 +00:00
|
|
|
tprio_t prio = chThdGetPriorityX();
|
2008-09-23 15:04:14 +00:00
|
|
|
|
2009-10-16 17:45:19 +00:00
|
|
|
(void)chHeapStatus(&heap1, &sz);
|
|
|
|
/* Starting threads from the heap. */
|
|
|
|
threads[0] = chThdCreateFromHeap(&heap1, THD_WA_SIZE(THREADS_STACK_SIZE),
|
|
|
|
prio-1, thread, "A");
|
|
|
|
threads[1] = chThdCreateFromHeap(&heap1, THD_WA_SIZE(THREADS_STACK_SIZE),
|
|
|
|
prio-2, thread, "B");
|
|
|
|
/* Allocating the whole heap in order to make the thread creation fail.*/
|
|
|
|
(void)chHeapStatus(&heap1, &n);
|
|
|
|
p1 = chHeapAlloc(&heap1, n);
|
|
|
|
threads[2] = chThdCreateFromHeap(&heap1, THD_WA_SIZE(THREADS_STACK_SIZE),
|
|
|
|
prio-3, thread, "C");
|
|
|
|
chHeapFree(p1);
|
|
|
|
|
|
|
|
test_assert(1, (threads[0] != NULL) &&
|
|
|
|
(threads[1] != NULL) &&
|
|
|
|
(threads[2] == NULL) &&
|
|
|
|
(threads[3] == NULL) &&
|
|
|
|
(threads[4] == NULL),
|
|
|
|
"thread creation failed");
|
|
|
|
|
|
|
|
/* Claiming the memory from terminated threads. */
|
|
|
|
test_wait_threads();
|
|
|
|
test_assert_sequence(2, "AB");
|
|
|
|
|
|
|
|
/* Heap status checked again.*/
|
|
|
|
test_assert(3, chHeapStatus(&heap1, &n) == 1, "heap fragmented");
|
|
|
|
test_assert(4, n == sz, "heap size changed");
|
2008-09-23 15:04:14 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase testdyn1 = {
|
2010-06-15 11:34:16 +00:00
|
|
|
"Dynamic APIs, threads creation from heap",
|
2009-10-16 17:45:19 +00:00
|
|
|
dyn1_setup,
|
2009-02-16 18:22:49 +00:00
|
|
|
NULL,
|
2008-09-23 15:04:14 +00:00
|
|
|
dyn1_execute
|
|
|
|
};
|
2013-07-20 10:12:44 +00:00
|
|
|
#endif /* (CH_CFG_USE_HEAP && !CH_CFG_USE_MALLOC_HEAP) */
|
2008-09-23 15:04:14 +00:00
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#if CH_CFG_USE_MEMPOOLS || defined(__DOXYGEN__)
|
2009-05-08 14:06:45 +00:00
|
|
|
/**
|
|
|
|
* @page test_dynamic_002 Threads creation from Memory Pool
|
|
|
|
*
|
|
|
|
* <h2>Description</h2>
|
|
|
|
* Five thread creation are attempted from a pool containing only four
|
|
|
|
* elements.<br>
|
|
|
|
* The test expects the first four threads to successfully start and the last
|
|
|
|
* one to fail.
|
|
|
|
*/
|
|
|
|
|
2008-09-23 15:04:14 +00:00
|
|
|
static void dyn2_setup(void) {
|
|
|
|
|
2013-07-20 07:24:12 +00:00
|
|
|
chPoolObjectInit(&mp1, THD_WA_SIZE(THREADS_STACK_SIZE), NULL);
|
2008-09-23 15:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void dyn2_execute(void) {
|
|
|
|
int i;
|
2013-08-16 17:41:56 +00:00
|
|
|
tprio_t prio = chThdGetPriorityX();
|
2008-09-23 15:04:14 +00:00
|
|
|
|
|
|
|
/* Adding the WAs to the pool. */
|
2009-04-15 20:37:27 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
2008-09-23 15:04:14 +00:00
|
|
|
chPoolFree(&mp1, wa[i]);
|
|
|
|
|
|
|
|
/* Starting threads from the memory pool. */
|
|
|
|
threads[0] = chThdCreateFromMemoryPool(&mp1, prio-1, thread, "A");
|
|
|
|
threads[1] = chThdCreateFromMemoryPool(&mp1, prio-2, thread, "B");
|
|
|
|
threads[2] = chThdCreateFromMemoryPool(&mp1, prio-3, thread, "C");
|
|
|
|
threads[3] = chThdCreateFromMemoryPool(&mp1, prio-4, thread, "D");
|
|
|
|
threads[4] = chThdCreateFromMemoryPool(&mp1, prio-5, thread, "E");
|
|
|
|
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(1, (threads[0] != NULL) &&
|
|
|
|
(threads[1] != NULL) &&
|
|
|
|
(threads[2] != NULL) &&
|
|
|
|
(threads[3] != NULL) &&
|
|
|
|
(threads[4] == NULL),
|
|
|
|
"thread creation failed");
|
2008-09-23 15:04:14 +00:00
|
|
|
|
|
|
|
/* Claiming the memory from terminated threads. */
|
|
|
|
test_wait_threads();
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_sequence(2, "ABCD");
|
2008-09-23 15:04:14 +00:00
|
|
|
|
2008-09-23 18:34:51 +00:00
|
|
|
/* Now the pool must be full again. */
|
2009-04-15 20:37:27 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(3, chPoolAlloc(&mp1) != NULL, "pool list empty");
|
|
|
|
test_assert(4, chPoolAlloc(&mp1) == NULL, "pool list not empty");
|
2008-09-23 15:04:14 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase testdyn2 = {
|
2010-06-15 11:34:16 +00:00
|
|
|
"Dynamic APIs, threads creation from memory pool",
|
2008-09-23 15:04:14 +00:00
|
|
|
dyn2_setup,
|
2009-02-16 18:22:49 +00:00
|
|
|
NULL,
|
2008-09-23 15:04:14 +00:00
|
|
|
dyn2_execute
|
|
|
|
};
|
2013-07-20 10:12:44 +00:00
|
|
|
#endif /* CH_CFG_USE_MEMPOOLS */
|
2008-09-23 15:04:14 +00:00
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#if (CH_CFG_USE_HEAP && !CH_CFG_USE_MALLOC_HEAP && CH_CFG_USE_REGISTRY) || \
|
2011-05-19 09:13:24 +00:00
|
|
|
defined(__DOXYGEN__)
|
2010-05-04 12:31:05 +00:00
|
|
|
/**
|
|
|
|
* @page test_dynamic_003 Registry and References test
|
|
|
|
*
|
|
|
|
* <h2>Description</h2>
|
|
|
|
* Registry and Thread References APIs are tested for functionality and
|
|
|
|
* coverage.
|
|
|
|
*/
|
|
|
|
|
2013-07-19 12:22:31 +00:00
|
|
|
static bool_t regfind(thread_t *tp) {
|
|
|
|
thread_t *ftp;
|
2010-11-23 20:26:17 +00:00
|
|
|
bool_t found = FALSE;
|
2010-05-04 12:31:05 +00:00
|
|
|
|
2010-11-23 20:26:17 +00:00
|
|
|
ftp = chRegFirstThread();
|
2010-05-04 12:31:05 +00:00
|
|
|
do {
|
2010-11-23 20:26:17 +00:00
|
|
|
found |= ftp == tp;
|
|
|
|
ftp = chRegNextThread(ftp);
|
|
|
|
} while (ftp != NULL);
|
|
|
|
return found;
|
2010-05-04 12:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void dyn3_setup(void) {
|
|
|
|
|
2013-07-20 07:24:12 +00:00
|
|
|
chHeapObjectInit(&heap1, test.buffer, sizeof(union test_buffers));
|
2010-05-04 12:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void dyn3_execute(void) {
|
2013-07-19 12:22:31 +00:00
|
|
|
thread_t *tp;
|
2013-08-16 17:41:56 +00:00
|
|
|
tprio_t prio = chThdGetPriorityX();
|
2010-05-04 12:31:05 +00:00
|
|
|
|
|
|
|
/* Testing references increase/decrease and final detach.*/
|
2010-05-08 10:30:38 +00:00
|
|
|
tp = chThdCreateFromHeap(&heap1, WA_SIZE, prio-1, thread, "A");
|
|
|
|
test_assert(1, tp->p_refs == 1, "wrong initial reference counter");
|
|
|
|
chThdAddRef(tp);
|
|
|
|
test_assert(2, tp->p_refs == 2, "references increase failure");
|
|
|
|
chThdRelease(tp);
|
|
|
|
test_assert(3, tp->p_refs == 1, "references decrease failure");
|
2010-05-04 12:31:05 +00:00
|
|
|
|
|
|
|
/* Verify the new threads count.*/
|
2010-11-23 20:26:17 +00:00
|
|
|
test_assert(4, regfind(tp), "thread missing from registry");
|
|
|
|
test_assert(5, regfind(tp), "thread disappeared");
|
2010-05-04 12:31:05 +00:00
|
|
|
|
|
|
|
/* Detach and let the thread execute and terminate.*/
|
2010-05-08 10:30:38 +00:00
|
|
|
chThdRelease(tp);
|
2010-11-23 20:26:17 +00:00
|
|
|
test_assert(6, tp->p_refs == 0, "detach failure");
|
2013-07-24 14:54:26 +00:00
|
|
|
test_assert(7, tp->p_state == CH_STATE_READY, "invalid state");
|
2010-11-23 20:26:17 +00:00
|
|
|
test_assert(8, regfind(tp), "thread disappeared");
|
|
|
|
test_assert(9, regfind(tp), "thread disappeared");
|
2010-05-04 12:31:05 +00:00
|
|
|
chThdSleepMilliseconds(50); /* The thread just terminates. */
|
2013-07-24 14:54:26 +00:00
|
|
|
test_assert(10, tp->p_state == CH_STATE_FINAL, "invalid state");
|
2010-05-04 12:31:05 +00:00
|
|
|
|
|
|
|
/* Clearing the zombie by scanning the registry.*/
|
2010-11-23 20:26:17 +00:00
|
|
|
test_assert(11, regfind(tp), "thread disappeared");
|
|
|
|
test_assert(12, !regfind(tp), "thread still in registry");
|
2010-05-04 12:31:05 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase testdyn3 = {
|
2010-06-15 11:34:16 +00:00
|
|
|
"Dynamic APIs, registry and references",
|
2010-05-04 12:31:05 +00:00
|
|
|
dyn3_setup,
|
|
|
|
NULL,
|
|
|
|
dyn3_execute
|
|
|
|
};
|
2013-07-20 10:12:44 +00:00
|
|
|
#endif /* CH_CFG_USE_HEAP && CH_CFG_USE_REGISTRY */
|
|
|
|
#endif /* CH_CFG_USE_DYNAMIC */
|
2009-02-16 18:22:49 +00:00
|
|
|
|
2010-04-27 11:53:03 +00:00
|
|
|
/**
|
|
|
|
* @brief Test sequence for dynamic APIs.
|
2009-02-16 18:22:49 +00:00
|
|
|
*/
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase * ROMCONST patterndyn[] = {
|
2013-07-20 10:12:44 +00:00
|
|
|
#if CH_CFG_USE_DYNAMIC || defined(__DOXYGEN__)
|
|
|
|
#if (CH_CFG_USE_HEAP && !CH_CFG_USE_MALLOC_HEAP) || defined(__DOXYGEN__)
|
2009-02-16 18:22:49 +00:00
|
|
|
&testdyn1,
|
|
|
|
#endif
|
2013-07-20 10:12:44 +00:00
|
|
|
#if CH_CFG_USE_MEMPOOLS || defined(__DOXYGEN__)
|
2009-02-16 18:22:49 +00:00
|
|
|
&testdyn2,
|
|
|
|
#endif
|
2013-07-20 10:12:44 +00:00
|
|
|
#if (CH_CFG_USE_HEAP && !CH_CFG_USE_MALLOC_HEAP && CH_CFG_USE_REGISTRY) || \
|
2011-05-19 09:13:24 +00:00
|
|
|
defined(__DOXYGEN__)
|
2010-05-04 12:31:05 +00:00
|
|
|
&testdyn3,
|
|
|
|
#endif
|
2009-02-16 18:22:49 +00:00
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
};
|