how to clean up deleted task 's resource

vicui wrote on Tuesday, February 19, 2013:


i know that IDLE task is in charge of cleaning up deleted task’s resource. however, IDLE task’s priority is lower. it is possible that high priority task creates new task before IDLE task does it, if the FreeRTOS 's memory is lower, it may be fail to create new task. so, i wonder to know that  there is a way to wake up IDLE task in running mode once one task is deleted ?


rtel wrote on Tuesday, February 19, 2013:

First - it is very rare in small embedded systems that it is necessary to delete tasks.  Generally in small dedicated systems all the resources (including tasks) are created during initialisation and then persist in the system until the system is turned off.  You may therefore like to consider if there is an alternative to deleting the tasks in the first place.

Resources have to be cleaned by a task other than the task being deleted, hence it is done in the idle task.  You could call the function used by the idle task to clean resources directly from another task, but that would require hacking the tasks.c file a bit as the function only has file scope.

If you really needed to you could temporarily raise the priority of the idle task (there is a function that allows you to get its handle) to the same priority as the task that wants to recreate a task - but be very careful doing that as the idle task never blocks so if you raise its priority too high you will not be able to recover and your application tasks will never run again.

Also, the function that creates a task returns a value that tells you if the task was created successfully or not, you could sit in a loop calling the xTaskCreate() function until it is successful if the only reason it is not successful is because a task that has been deleted has not yet been cleaned.  Make sure to add a delay between each call to ensure the idle task has a chance to run though.