|
| 1 | +/* |
| 2 | + * FreeRTOS V202212.00 |
| 3 | + * Copyright (C) 2020 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 only_one_task_enter_critical.c |
| 29 | + * @brief Only one task/ISR shall be able to enter critical section at a time. |
| 30 | + * |
| 31 | + * Procedure: |
| 32 | + * - Create ( num of cores ) tasks. |
| 33 | + * - All tasks increase the counter for TASK_INCREASE_COUNTER_TIMES times in |
| 34 | + * critical section. |
| 35 | + * Expected: |
| 36 | + * - All tasks have correct value of counter after increasing. |
| 37 | + */ |
| 38 | + |
| 39 | +/* Standard includes. */ |
| 40 | +#include <stdint.h> |
| 41 | + |
| 42 | +/* Kernel includes. */ |
| 43 | +#include "FreeRTOS.h" |
| 44 | +#include "task.h" |
| 45 | + |
| 46 | +/* Unit testing support functions. */ |
| 47 | +#include "unity.h" |
| 48 | +/*-----------------------------------------------------------*/ |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief As time of loop for task to increase counter. |
| 52 | + */ |
| 53 | +#define TASK_INCREASE_COUNTER_TIMES ( 1000000 ) |
| 54 | + |
| 55 | +/** |
| 56 | + * @brief Timeout value to stop test. |
| 57 | + */ |
| 58 | +#define TEST_TIMEOUT_MS ( 1000 ) |
| 59 | +/*-----------------------------------------------------------*/ |
| 60 | + |
| 61 | +/** |
| 62 | + * @brief Test case "Only One Task Enter Critical". |
| 63 | + */ |
| 64 | +void Test_OnlyOneTaskEnterCritical( void ); |
| 65 | + |
| 66 | +/** |
| 67 | + * @brief Task function to increase counter then keep delaying. |
| 68 | + */ |
| 69 | +static void prvTaskIncCounter( void * pvParameters ); |
| 70 | +/*-----------------------------------------------------------*/ |
| 71 | + |
| 72 | +#if ( configNUMBER_OF_CORES < 2 ) |
| 73 | + #error This test is for FreeRTOS SMP and therefore, requires at least 2 cores. |
| 74 | +#endif /* if ( configNUMBER_OF_CORES < 2 ) */ |
| 75 | + |
| 76 | +#if ( configMAX_PRIORITIES <= 2 ) |
| 77 | + #error configMAX_PRIORITIES must be larger than 2 to avoid scheduling idle tasks unexpectedly. |
| 78 | +#endif /* if ( configMAX_PRIORITIES <= 2 ) */ |
| 79 | +/*-----------------------------------------------------------*/ |
| 80 | + |
| 81 | +/** |
| 82 | + * @brief Handles of the tasks created in this test. |
| 83 | + */ |
| 84 | +static TaskHandle_t xTaskHandles[ configNUMBER_OF_CORES ]; |
| 85 | + |
| 86 | +/** |
| 87 | + * @brief Indexes of the tasks created in this test. |
| 88 | + */ |
| 89 | +static uint32_t xTaskIndexes[ configNUMBER_OF_CORES ]; |
| 90 | + |
| 91 | +/** |
| 92 | + * @brief Flags to indicate if task T0 ~ T(n - 1) finish or not. |
| 93 | + */ |
| 94 | +static BaseType_t xTaskTestResults[ configNUMBER_OF_CORES ]; |
| 95 | + |
| 96 | +/** |
| 97 | + * @brief Variables to indicate task is ready for testing. |
| 98 | + */ |
| 99 | +static volatile BaseType_t xTaskReady[ configNUMBER_OF_CORES ]; |
| 100 | + |
| 101 | +/** |
| 102 | + * @brief Counter for all tasks to increase. |
| 103 | + */ |
| 104 | +static volatile uint32_t xTaskCounter; |
| 105 | +/*-----------------------------------------------------------*/ |
| 106 | + |
| 107 | +static void prvTaskIncCounter( void * pvParameters ) |
| 108 | +{ |
| 109 | + uint32_t currentTaskIdx = *( ( uint32_t * ) pvParameters ); |
| 110 | + BaseType_t xAllTaskReady = pdFALSE; |
| 111 | + BaseType_t xTestResult = pdPASS; |
| 112 | + uint32_t xTempTaskCounter = 0; |
| 113 | + uint32_t i; |
| 114 | + |
| 115 | + /* Ensure all test tasks are running in the task function. */ |
| 116 | + xTaskReady[ currentTaskIdx ] = pdTRUE; |
| 117 | + |
| 118 | + while( xAllTaskReady == pdFALSE ) |
| 119 | + { |
| 120 | + xAllTaskReady = pdTRUE; |
| 121 | + |
| 122 | + for( i = 0; i < configNUMBER_OF_CORES; i++ ) |
| 123 | + { |
| 124 | + if( xTaskReady[ i ] != pdTRUE ) |
| 125 | + { |
| 126 | + xAllTaskReady = pdFALSE; |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /* Increase the test counter in the loop. The test expects only one task |
| 133 | + * can increase the shared variable xTaskCounter protected by the critical |
| 134 | + * section at the same time. */ |
| 135 | + taskENTER_CRITICAL(); |
| 136 | + { |
| 137 | + xTempTaskCounter = xTaskCounter; |
| 138 | + |
| 139 | + for( i = 0; i < TASK_INCREASE_COUNTER_TIMES; i++ ) |
| 140 | + { |
| 141 | + /* Increase the local variable xTempTaskCounter and shared variable |
| 142 | + * xTaskCounter. They must have the same value in the critical |
| 143 | + * section. */ |
| 144 | + xTaskCounter++; |
| 145 | + xTempTaskCounter++; |
| 146 | + |
| 147 | + /* If multiple tasks enter the critical section, the shared variable |
| 148 | + * xTaskCounter will be increased by multiple tasks.As a result, the |
| 149 | + * local variable xTempTaskCounter won't be equal to the shared |
| 150 | + * variable xTaskCounter. */ |
| 151 | + if( xTaskCounter != xTempTaskCounter ) |
| 152 | + { |
| 153 | + xTestResult = pdFAIL; |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + taskEXIT_CRITICAL(); |
| 159 | + |
| 160 | + xTaskTestResults[ currentTaskIdx ] = xTestResult; |
| 161 | + |
| 162 | + /* Blocking the test task. */ |
| 163 | + vTaskDelay( portMAX_DELAY ); |
| 164 | +} |
| 165 | +/*-----------------------------------------------------------*/ |
| 166 | + |
| 167 | +void Test_OnlyOneTaskEnterCritical( void ) |
| 168 | +{ |
| 169 | + uint32_t i; |
| 170 | + BaseType_t xTaskCreationResult; |
| 171 | + |
| 172 | + /* Create configNUMBER_OF_CORES low priority tasks. */ |
| 173 | + for( i = 0; i < configNUMBER_OF_CORES; i++ ) |
| 174 | + { |
| 175 | + xTaskCreationResult = xTaskCreate( prvTaskIncCounter, |
| 176 | + "IncCounter", |
| 177 | + configMINIMAL_STACK_SIZE, |
| 178 | + &( xTaskIndexes[ i ] ), |
| 179 | + configMAX_PRIORITIES - 2, |
| 180 | + &( xTaskHandles[ i ] ) ); |
| 181 | + |
| 182 | + TEST_ASSERT_EQUAL_MESSAGE( pdPASS, xTaskCreationResult, "Task creation failed." ); |
| 183 | + } |
| 184 | + |
| 185 | + /* Delay for other cores to run tasks. */ |
| 186 | + vTaskDelay( pdMS_TO_TICKS( TEST_TIMEOUT_MS ) ); |
| 187 | + |
| 188 | + /* Verify each test task result. */ |
| 189 | + for( i = 0; i < configNUMBER_OF_CORES; i++ ) |
| 190 | + { |
| 191 | + TEST_ASSERT_EQUAL_MESSAGE( pdPASS, xTaskTestResults[ i ], "Critical section test task failed." ); |
| 192 | + } |
| 193 | + |
| 194 | + /* Verify the shared variable counter value. */ |
| 195 | + TEST_ASSERT_EQUAL_UINT32( configNUMBER_OF_CORES * TASK_INCREASE_COUNTER_TIMES, xTaskCounter ); |
| 196 | +} |
| 197 | +/*-----------------------------------------------------------*/ |
| 198 | + |
| 199 | +/* Runs before every test, put init calls here. */ |
| 200 | +void setUp( void ) |
| 201 | +{ |
| 202 | + uint32_t i; |
| 203 | + |
| 204 | + xTaskCounter = 0; |
| 205 | + |
| 206 | + for( i = 0; i < configNUMBER_OF_CORES; i++ ) |
| 207 | + { |
| 208 | + xTaskIndexes[ i ] = i; |
| 209 | + xTaskHandles[ i ] = NULL; |
| 210 | + xTaskTestResults[ i ] = pdFALSE; |
| 211 | + xTaskReady[ i ] = pdFALSE; |
| 212 | + } |
| 213 | +} |
| 214 | +/*-----------------------------------------------------------*/ |
| 215 | + |
| 216 | +/* Runs after every test, put clean-up calls here. */ |
| 217 | +void tearDown( void ) |
| 218 | +{ |
| 219 | + uint32_t i; |
| 220 | + |
| 221 | + /* Delete all the tasks. */ |
| 222 | + for( i = 0; i < configNUMBER_OF_CORES; i++ ) |
| 223 | + { |
| 224 | + if( xTaskHandles[ i ] != NULL ) |
| 225 | + { |
| 226 | + vTaskDelete( xTaskHandles[ i ] ); |
| 227 | + xTaskHandles[ i ] = NULL; |
| 228 | + } |
| 229 | + } |
| 230 | +} |
| 231 | +/*-----------------------------------------------------------*/ |
| 232 | + |
| 233 | +/** |
| 234 | + * @brief Entry point for test runner to run critical section test. |
| 235 | + */ |
| 236 | +void vRunOnlyOneTaskEnterCriticalTest( void ) |
| 237 | +{ |
| 238 | + UNITY_BEGIN(); |
| 239 | + |
| 240 | + RUN_TEST( Test_OnlyOneTaskEnterCritical ); |
| 241 | + |
| 242 | + UNITY_END(); |
| 243 | +} |
| 244 | +/*-----------------------------------------------------------*/ |
0 commit comments