From 1c798b0eab9803b95ed3805618bf8861ac195d05 Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Fri, 16 Jun 2017 11:49:48 +0800 Subject: [PATCH] BugFix: uxTaskGetSystemState test case update Updated test case to include configASSERT cases (+1 squashed commits) Squashed commits: [871ec26f] Freertos:Bugfix uxTaskGetSystemState Bug (github #12142) with uxTaskGetSystemState where if called immediately after creating a bunch of tasks, those tasks would be added twice into the TaskStatusArray. Bug caused due to use old implementation using vTaskSuspendAll which did not stop newly created task on other core from accessing the read/waiting task lists whilst the list were being read by uxTaskGetSystemState. Fixed bug by replacing vTaskSuspendAll with taskENTER_CRITICAL and added test case for the bugfix --- components/freertos/tasks.c | 5 +- .../test/test_uxGetSystemState_Bugfix.c | 67 +++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 components/freertos/test/test_uxGetSystemState_Bugfix.c diff --git a/components/freertos/tasks.c b/components/freertos/tasks.c index ca73ba0a67..ac82c5c8c8 100644 --- a/components/freertos/tasks.c +++ b/components/freertos/tasks.c @@ -2280,7 +2280,7 @@ UBaseType_t uxTaskGetNumberOfTasks( void ) UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES; UNTESTED_FUNCTION(); - vTaskSuspendAll(); //WARNING: This only suspends one CPU. ToDo: suspend others as well. Mux using taskQueueMutex maybe? + taskENTER_CRITICAL(&xTaskQueueMutex); { /* Is there a space in the array for each task in the system? */ if( uxArraySize >= uxCurrentNumberOfTasks ) @@ -2340,8 +2340,7 @@ UBaseType_t uxTaskGetNumberOfTasks( void ) mtCOVERAGE_TEST_MARKER(); } } - ( void ) xTaskResumeAll(); - + taskEXIT_CRITICAL(&xTaskQueueMutex); return uxTask; } diff --git a/components/freertos/test/test_uxGetSystemState_Bugfix.c b/components/freertos/test/test_uxGetSystemState_Bugfix.c new file mode 100644 index 0000000000..e7e447f68e --- /dev/null +++ b/components/freertos/test/test_uxGetSystemState_Bugfix.c @@ -0,0 +1,67 @@ +/* + Test Bugfix for uxTaskGetSystemState where getting system state immediately after creating + new tasks would lead them being include twice in the TaskStatusArray. Changed suspendScheduler + in function to taskENTER_CRITICAL +*/ + + +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "unity.h" + +#define CreatedTaskDelayTicks 1 +#define NumberOfTasksToCreate 4 +#define CreatedTaskPriority 10 +#define CoreToUse 1 + +#if (configUSE_TRACE_FACILITY) + +void TaskCallback (void* pxParam){ + int counter = 0; + while(1){ + counter++; + vTaskDelay(CreatedTaskDelayTicks); + } +} + +TEST_CASE("uxTaskGetSystemState Bugfix Test", "[freertos]") +{ + TaskStatus_t *TaskStatusArray; + TaskHandle_t *TaskHandles; + UBaseType_t NumberOfTasks = 0; + UBaseType_t StartingNumberOfTasks = 0; + + //Give Time for OS to remove dport tasks + vTaskDelay(1000); + + //Allocate TaskStatusArray + StartingNumberOfTasks = uxTaskGetNumberOfTasks(); + + + TaskStatusArray = pvPortMalloc((StartingNumberOfTasks+NumberOfTasksToCreate) * sizeof(TaskStatus_t)); + TaskHandles = pvPortMalloc((StartingNumberOfTasks+NumberOfTasksToCreate) * sizeof(TaskHandle_t)); + + + + //Create Tasks + for(int i = 0; i < NumberOfTasksToCreate; i++){ + xTaskCreatePinnedToCore(&TaskCallback, "Task" , 2048, NULL, CreatedTaskPriority, (TaskHandle_t*) &TaskHandles[i], CoreToUse); + } + NumberOfTasks = uxTaskGetSystemState(TaskStatusArray, (StartingNumberOfTasks+NumberOfTasksToCreate), NULL); + + //Check if any taska have been repeated in TaskStatusArray + if(NumberOfTasks != 0){ + printf("Tasks Created, Checking for Repeated Additions \n"); + for(int i = 0; i