/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
2011 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 .
*/
#include "ch.h"
#include "test.h"
/**
* @page test_dynamic Dynamic APIs test
*
* File: @ref testdyn.c
*
*
Description
* This module implements the test sequence for the dynamic thread creation
* APIs.
*
* Objective
* Objective of the test module is to cover 100% of the dynamic APIs code.
*
* Preconditions
* The module requires the following kernel options:
* - @p CH_USE_DYNAMIC
* - @p CH_USE_HEAP
* - @p CH_USE_MEMPOOLS
* .
* In case some of the required options are not enabled then some or all tests
* may be skipped.
*
* Test Cases
* - @subpage test_dynamic_001
* - @subpage test_dynamic_002
* - @subpage test_dynamic_003
* .
* @file testdyn.c
* @brief Dynamic thread APIs test source file
* @file testdyn.h
* @brief Dynamic thread APIs test header file
*/
#if CH_USE_DYNAMIC || defined(__DOXYGEN__)
#if (CH_USE_HEAP && !CH_USE_MALLOC_HEAP) || defined(__DOXYGEN__)
static MemoryHeap heap1;
#endif
#if CH_USE_MEMPOOLS || defined(__DOXYGEN__)
static MemoryPool mp1;
#endif
/**
* @page test_dynamic_001 Threads creation from Memory Heap
*
* Description
* 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.
* The test expects the first two threads to successfully start and the last
* one to fail.
*/
static msg_t thread(void *p) {
test_emit_token(*(char *)p);
return 0;
}
#if (CH_USE_HEAP && !CH_USE_MALLOC_HEAP) || defined(__DOXYGEN__)
static void dyn1_setup(void) {
chHeapInit(&heap1, test.buffer, sizeof(union test_buffers));
}
static void dyn1_execute(void) {
size_t n, sz;
void *p1;
tprio_t prio = chThdGetPriority();
(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");
}
ROMCONST struct testcase testdyn1 = {
"Dynamic APIs, threads creation from heap",
dyn1_setup,
NULL,
d@REM Build for Visual Studio compiler. Run your copy of vcvars32.bat or vcvarsall.bat to setup command-line compiler.
mkdir Debug
cl /nologo /Zi /MD /I .. /I ..\.. /I "%WindowsSdkDir%Include\um" /I "%WindowsSdkDir%Include\shared" /I "%DXSDK_DIR%Include" /D UNICODE /D _UNICODE *.cpp ..\imgui_impl_dx11.cpp ..\imgui_impl_win32.cpp ..\..\imgui*.cpp /FeDebug/example_win32_directx11.exe /FoDebug/ /link /LIBPATH:"%DXSDK_DIR%/Lib/x86" d3d11.lib d3dcompiler.lib