test_freertos_task_delete.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Test backported deletion behavior by creating tasks of various affinities and
  3. * check if the task memory is freed immediately under the correct conditions.
  4. *
  5. * The behavior of vTaskDelete() has been backported form FreeRTOS v9.0.0. This
  6. * results in the immediate freeing of task memory and the immediate execution
  7. * of deletion callbacks under the following conditions...
  8. * - When deleting a task that is not currently running on either core
  9. * - When deleting a task that is pinned to the same core (with respect to
  10. * the core that calls vTaskDelete()
  11. *
  12. * If the two conditions are not met, freeing of task memory and execution of
  13. * deletion callbacks will still be carried out by the Idle Task.
  14. */
  15. #include <stdio.h>
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "esp_heap_caps.h"
  19. #include "rom/ets_sys.h"
  20. #include "unity.h"
  21. #define NO_OF_TSKS 3
  22. #define DELAY_TICKS 2
  23. #define HEAP_CAPS (MALLOC_CAP_INTERNAL|MALLOC_CAP_DEFAULT)
  24. #define DELAY_US_ITERATIONS 1000
  25. static void tsk_self_del(void *param)
  26. {
  27. vTaskDelete(NULL); //Deleting self means deleting currently running task
  28. }
  29. static void tsk_extern_del(void *param)
  30. {
  31. vTaskDelay(portMAX_DELAY); //Await external deletion
  32. }
  33. static void tsk_self_del_us_delay(void *param)
  34. {
  35. uint32_t delay = (uint32_t)param;
  36. ets_delay_us(delay);
  37. vTaskDelete(NULL);
  38. }
  39. TEST_CASE("FreeRTOS Delete Tasks", "[freertos]")
  40. {
  41. /* -------------- Test vTaskDelete() on currently running tasks ----------------*/
  42. uint32_t before_count = uxTaskGetNumberOfTasks();
  43. uint32_t before_heap = heap_caps_get_free_size(HEAP_CAPS);
  44. for(int i = 0; i < portNUM_PROCESSORS; i++){
  45. for(int j = 0; j < NO_OF_TSKS; j++){
  46. TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(tsk_self_del, "tsk_self", 1024, NULL, configMAX_PRIORITIES - 1, NULL, i));
  47. }
  48. }
  49. vTaskDelay(DELAY_TICKS); //Minimal delay to see if Idle task cleans up all tasks awaiting deletion in a single tick
  50. TEST_ASSERT_EQUAL(before_count, uxTaskGetNumberOfTasks());
  51. TEST_ASSERT_EQUAL(before_heap, heap_caps_get_free_size(HEAP_CAPS));
  52. /* ------------- Test vTaskDelete() on not currently running tasks ------------ */
  53. TaskHandle_t handles[NO_OF_TSKS];
  54. before_heap = heap_caps_get_free_size(HEAP_CAPS);
  55. //Create task pinned to the same core that will not run during task deletion
  56. for(int j = 0 ; j < NO_OF_TSKS; j++){
  57. TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(tsk_extern_del, "tsk_extern", 4096, NULL, configMAX_PRIORITIES - 1, &handles[j], xPortGetCoreID()));
  58. }
  59. TEST_ASSERT_NOT_EQUAL(before_heap, heap_caps_get_free_size(HEAP_CAPS)); //Check tasks have been created
  60. //Delete the tasks, memory should be freed immediately
  61. for(int j = 0; j < NO_OF_TSKS; j++){
  62. vTaskDelete(handles[j]);
  63. }
  64. TEST_ASSERT_EQUAL(before_heap, heap_caps_get_free_size(HEAP_CAPS));
  65. /* Test self deleting no affinity task is not removed by idle task of other core before context switch */
  66. for(int i = 0; i < DELAY_US_ITERATIONS; i+= 10){
  67. vTaskDelay(1); //Sync to next tick interrupt
  68. xTaskCreatePinnedToCore(tsk_self_del_us_delay, "delay", 1024, (void *)i, UNITY_FREERTOS_PRIORITY - 1, NULL, tskNO_AFFINITY);
  69. ets_delay_us(10); //Busy wait to ensure no affinity task runs on opposite core
  70. }
  71. }