Skip to content

Commit 520fc22

Browse files
authored
Add SMP task delete on target test (FreeRTOS#1176)
Add SMP task delete on target test
1 parent 6ed67f5 commit 520fc22

File tree

4 files changed

+424
-0
lines changed

4 files changed

+424
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
project(example C CXX ASM)
4+
set(CMAKE_C_STANDARD 11)
5+
set(CMAKE_CXX_STANDARD 17)
6+
7+
set(TEST_INCLUDE_PATHS ${CMAKE_CURRENT_LIST_DIR}/../../../../../tests/smp/task_delete)
8+
set(TEST_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../../../tests/smp/task_delete)
9+
10+
add_library(task_delete INTERFACE)
11+
target_sources(task_delete INTERFACE
12+
${BOARD_LIBRARY_DIR}/main.c
13+
${CMAKE_CURRENT_LIST_DIR}/task_delete_test_runner.c
14+
${TEST_SOURCE_DIR}/task_delete.c)
15+
16+
target_include_directories(task_delete INTERFACE
17+
${CMAKE_CURRENT_LIST_DIR}/../../..
18+
${TEST_INCLUDE_PATHS}
19+
)
20+
21+
target_link_libraries(task_delete INTERFACE
22+
FreeRTOS-Kernel
23+
FreeRTOS-Kernel-Heap4
24+
${BOARD_LINK_LIBRARIES})
25+
26+
add_executable(test_task_delete)
27+
enable_board_functions(test_task_delete)
28+
target_link_libraries(test_task_delete task_delete)
29+
target_include_directories(test_task_delete PUBLIC
30+
${BOARD_INCLUDE_PATHS})
31+
target_compile_definitions(test_task_delete PRIVATE
32+
${BOARD_DEFINES}
33+
)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* FreeRTOS V202212.00
3+
* Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
* this software and associated documentation files (the "Software"), to deal in
7+
* the Software without restriction, including without limitation the rights to
8+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
* the Software, and to permit persons to whom the Software is furnished to do so,
10+
* subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*
22+
* https://www.FreeRTOS.org
23+
* https://github.com/FreeRTOS
24+
*
25+
*/
26+
27+
/**
28+
* @file task_delete_test_runner.c
29+
* @brief The implementation of main function to start test runner task.
30+
*
31+
* Procedure:
32+
* - Initialize environment.
33+
* - Run the test case.
34+
*/
35+
36+
/* Kernel includes. */
37+
#include "FreeRTOS.h"
38+
#include "task.h"
39+
40+
/* Unit testing support functions. */
41+
#include "unity.h"
42+
43+
/* Pico includes. */
44+
#include "pico/multicore.h"
45+
#include "pico/stdlib.h"
46+
47+
/*-----------------------------------------------------------*/
48+
49+
static void prvTestRunnerTask( void * pvParameters );
50+
51+
/*-----------------------------------------------------------*/
52+
53+
static void prvTestRunnerTask( void * pvParameters )
54+
{
55+
( void ) pvParameters;
56+
57+
/* Run test case. */
58+
vRunTaskDeleteTest();
59+
60+
vTaskDelete( NULL );
61+
}
62+
/*-----------------------------------------------------------*/
63+
64+
void vRunTest( void )
65+
{
66+
xTaskCreate( prvTestRunnerTask,
67+
"testRunner",
68+
configMINIMAL_STACK_SIZE,
69+
NULL,
70+
configMAX_PRIORITIES - 1,
71+
NULL );
72+
}
73+
/*-----------------------------------------------------------*/
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
/*
2+
* FreeRTOS V202212.00
3+
* Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
* this software and associated documentation files (the "Software"), to deal in
7+
* the Software without restriction, including without limitation the rights to
8+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
* the Software, and to permit persons to whom the Software is furnished to do so,
10+
* subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*
22+
* https://www.FreeRTOS.org
23+
* https://github.com/FreeRTOS
24+
*
25+
*/
26+
27+
/**
28+
* @file tasks_delete.c
29+
* @brief When a n RTOS object is deleted, the associated resources shall be freed.
30+
*
31+
* Procedure:
32+
* - TestRunner records original memory size.
33+
* - Create ( num of cores ) tasks T0 ~ T(n - 1).
34+
* - Tasks T0 ~ T(n - 1) delete themselves.
35+
* - TestRunner checks if memory is freed.
36+
* - Create ( num of cores ) tasks T0 ~ T(n - 1).
37+
* - Task T0 ~ T(n - 1) delay in loop.
38+
* - TestRunner deletes T0 ~ T(n - 1).
39+
* - TestRunner checks if memory freed.
40+
* Expected:
41+
* - Have same remaining memory before creating task and after deleting task.
42+
*/
43+
44+
/* Standard includes. */
45+
#include <stdint.h>
46+
47+
/* Kernel includes. */
48+
#include "FreeRTOS.h"
49+
#include "task.h"
50+
51+
/* Unit testing support functions. */
52+
#include "unity.h"
53+
54+
/*-----------------------------------------------------------*/
55+
56+
/**
57+
* @brief Timeout value to stop test.
58+
*/
59+
#define TEST_TIMEOUT_MS ( 1000 )
60+
61+
/*-----------------------------------------------------------*/
62+
63+
#if ( configNUMBER_OF_CORES < 2 )
64+
#error This test is for FreeRTOS SMP and therefore, requires at least 2 cores.
65+
#endif /* if ( configNUMBER_OF_CORES < 2 ) */
66+
67+
#if ( configMAX_PRIORITIES <= 2 )
68+
#error configMAX_PRIORITIES must be larger than 2 to avoid scheduling idle tasks unexpectedly.
69+
#endif /* if ( configMAX_PRIORITIES <= 2 ) */
70+
71+
/*-----------------------------------------------------------*/
72+
73+
/**
74+
* @brief Test case "Task Delete".
75+
*/
76+
void Test_TaskDelete( void );
77+
78+
/**
79+
* @brief Partial test case in "Task Delete" for "Self deletion".
80+
*/
81+
static void prvTestTaskSelfDelete( void );
82+
83+
/**
84+
* @brief Partial test case in "Task Delete" for "Remote deletion".
85+
*/
86+
static void prvTestTaskRemoteDelete( void );
87+
88+
/**
89+
* @brief Task entry to delete itself.
90+
*/
91+
static void prvSelfDeleteTask( void * pvParameters );
92+
93+
/**
94+
* @brief Task entry to loop in delay.
95+
*/
96+
static void prvDelayTask( void * pvParameters );
97+
/*-----------------------------------------------------------*/
98+
99+
/**
100+
* @brief Handles of the tasks created in this test.
101+
*/
102+
static TaskHandle_t xTaskHandles[ configNUMBER_OF_CORES ];
103+
104+
/**
105+
* @brief Flag to indicate task run status.
106+
*/
107+
static BaseType_t xTaskRunStatus[ configNUMBER_OF_CORES ];
108+
109+
/**
110+
* @brief The heap size before creating tasks T0 ~ T(n - 1).
111+
*/
112+
static uint32_t ulOriginalFreeHeapSize;
113+
/*-----------------------------------------------------------*/
114+
115+
static void prvSelfDeleteTask( void * pvParameters )
116+
{
117+
BaseType_t * pxTaskRunStatus = ( BaseType_t * ) pvParameters;
118+
119+
/* Set the flag to indicate the task has run. */
120+
*pxTaskRunStatus = pdTRUE;
121+
122+
vTaskDelete( NULL );
123+
124+
/* The task delete itself. This line should not be run. */
125+
*pxTaskRunStatus = pdFALSE;
126+
}
127+
/*-----------------------------------------------------------*/
128+
129+
static void prvDelayTask( void * pvParameters )
130+
{
131+
BaseType_t * pxTaskRunStatus = ( BaseType_t * ) pvParameters;
132+
133+
/* Set the flag to indicate the task has run. */
134+
*pxTaskRunStatus = pdTRUE;
135+
136+
/* Block this task forever. */
137+
vTaskDelay( portMAX_DELAY );
138+
}
139+
/*-----------------------------------------------------------*/
140+
141+
static void prvTestTaskSelfDelete( void )
142+
{
143+
uint32_t i;
144+
BaseType_t xTaskCreationResult;
145+
uint32_t ulFreeHeapSize;
146+
147+
/* Create configNUMBER_OF_CORES low priority tasks. */
148+
for( i = 0; i < configNUMBER_OF_CORES; i++ )
149+
{
150+
xTaskRunStatus[ i ] = pdFALSE;
151+
xTaskCreationResult = xTaskCreate( prvSelfDeleteTask,
152+
"SelfDel",
153+
configMINIMAL_STACK_SIZE,
154+
( void * ) ( &( xTaskRunStatus[ i ] ) ),
155+
configMAX_PRIORITIES - 2,
156+
&( xTaskHandles[ i ] ) );
157+
158+
TEST_ASSERT_EQUAL_MESSAGE( pdPASS, xTaskCreationResult, "Task creation failed." );
159+
}
160+
161+
/* Wait task to delete itself. */
162+
vTaskDelay( pdMS_TO_TICKS( TEST_TIMEOUT_MS ) );
163+
164+
/* Verify the task run status. */
165+
for( i = 0; i < configNUMBER_OF_CORES; i++ )
166+
{
167+
TEST_ASSERT_EQUAL_MESSAGE( pdTRUE, xTaskRunStatus[ i ], "Task hasn't been run." );
168+
xTaskHandles[ i ] = NULL;
169+
}
170+
171+
/* Verify the memory used for task TCB and stack is freed. */
172+
ulFreeHeapSize = xPortGetFreeHeapSize();
173+
TEST_ASSERT_EQUAL_INT_MESSAGE( ulOriginalFreeHeapSize, ulFreeHeapSize, "Self deleted task test failed." );
174+
}
175+
/*-----------------------------------------------------------*/
176+
177+
static void prvTestTaskRemoteDelete( void )
178+
{
179+
uint32_t i;
180+
BaseType_t xTaskCreationResult;
181+
uint32_t ulFreeHeapSize;
182+
183+
/* Create configNUMBER_OF_CORES low priority tasks. */
184+
for( i = 0; i < configNUMBER_OF_CORES; i++ )
185+
{
186+
xTaskRunStatus[ i ] = pdFALSE;
187+
xTaskCreationResult = xTaskCreate( prvDelayTask,
188+
"KeepDelay",
189+
configMINIMAL_STACK_SIZE,
190+
( void * ) ( &( xTaskRunStatus[ i ] ) ),
191+
configMAX_PRIORITIES - 2,
192+
&( xTaskHandles[ i ] ) );
193+
194+
TEST_ASSERT_EQUAL_MESSAGE( pdPASS, xTaskCreationResult, "Task creation failed." );
195+
}
196+
197+
/* Delay a while for tasks just created to run. */
198+
vTaskDelay( pdMS_TO_TICKS( TEST_TIMEOUT_MS ) );
199+
200+
/* Verify the task run status. */
201+
for( i = 0; i < configNUMBER_OF_CORES; i++ )
202+
{
203+
TEST_ASSERT_EQUAL_MESSAGE( pdTRUE, xTaskRunStatus[ i ], "Task hasn't been run." );
204+
}
205+
206+
/* Delete tasks remotely. */
207+
for( i = 0; i < configNUMBER_OF_CORES; i++ )
208+
{
209+
vTaskDelete( xTaskHandles[ i ] );
210+
xTaskHandles[ i ] = NULL;
211+
}
212+
213+
ulFreeHeapSize = xPortGetFreeHeapSize();
214+
TEST_ASSERT_EQUAL_INT_MESSAGE( ulOriginalFreeHeapSize, ulFreeHeapSize, "Remote deleted task test failed." );
215+
}
216+
/*-----------------------------------------------------------*/
217+
218+
void Test_TaskDelete( void )
219+
{
220+
prvTestTaskSelfDelete();
221+
prvTestTaskRemoteDelete();
222+
}
223+
/*-----------------------------------------------------------*/
224+
225+
/* Runs before every test, put init calls here. */
226+
void setUp( void )
227+
{
228+
/* Get the heap size before creating tasks. */
229+
ulOriginalFreeHeapSize = xPortGetFreeHeapSize();
230+
}
231+
/*-----------------------------------------------------------*/
232+
233+
/* Runs after every test, put clean-up calls here. */
234+
void tearDown( void )
235+
{
236+
uint32_t i;
237+
238+
for( i = 0; i < configNUMBER_OF_CORES; i++ )
239+
{
240+
if( xTaskHandles[ i ] != NULL )
241+
{
242+
vTaskDelete( xTaskHandles[ i ] );
243+
xTaskHandles[ i ] = NULL;
244+
}
245+
}
246+
}
247+
/*-----------------------------------------------------------*/
248+
249+
/**
250+
* @brief Entry point for test runner to task delete test.
251+
*/
252+
void vRunTaskDeleteTest( void )
253+
{
254+
UNITY_BEGIN();
255+
256+
RUN_TEST( Test_TaskDelete );
257+
258+
UNITY_END();
259+
}
260+
/*-----------------------------------------------------------*/

0 commit comments

Comments
 (0)