|
| 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