test_freertos_task_delete.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "unity.h"
  20. #include "test_utils.h"
  21. #define NO_OF_TSKS 3
  22. #define DELAY_TICKS 2
  23. /* Caps of all memory which is allocated from when a task is created */
  24. #define HEAP_CAPS (portTcbMemoryCaps | portStackMemoryCaps)
  25. #define DELAY_US_ITERATIONS 1000
  26. static void tsk_self_del(void *param)
  27. {
  28. vTaskDelete(NULL); //Deleting self means deleting currently running task
  29. }
  30. static void tsk_extern_del(void *param)
  31. {
  32. vTaskDelay(portMAX_DELAY); //Await external deletion
  33. }
  34. static void tsk_self_del_us_delay(void *param)
  35. {
  36. uint32_t delay = (uint32_t)param;
  37. ets_delay_us(delay);
  38. vTaskDelete(NULL);
  39. }
  40. TEST_CASE("FreeRTOS Delete Tasks", "[freertos]")
  41. {
  42. /* -------------- Test vTaskDelete() on currently running tasks ----------------*/
  43. uint32_t before_count = uxTaskGetNumberOfTasks();
  44. uint32_t before_heap = heap_caps_get_free_size(HEAP_CAPS);
  45. for(int i = 0; i < portNUM_PROCESSORS; i++){
  46. for(int j = 0; j < NO_OF_TSKS; j++){
  47. TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(tsk_self_del, "tsk_self", 1024, NULL, configMAX_PRIORITIES - 1, NULL, i));
  48. }
  49. }
  50. vTaskDelay(DELAY_TICKS); //Minimal delay to see if Idle task cleans up all tasks awaiting deletion in a single tick
  51. TEST_ASSERT_EQUAL(before_count, uxTaskGetNumberOfTasks());
  52. TEST_ASSERT_EQUAL(before_heap, heap_caps_get_free_size(HEAP_CAPS));
  53. /* ------------- Test vTaskDelete() on not currently running tasks ------------ */
  54. TaskHandle_t handles[NO_OF_TSKS];
  55. before_heap = heap_caps_get_free_size(HEAP_CAPS);
  56. //Create task pinned to the same core that will not run during task deletion
  57. for(int j = 0 ; j < NO_OF_TSKS; j++){
  58. TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(tsk_extern_del, "tsk_extern", 4096, NULL, configMAX_PRIORITIES - 1, &handles[j], xPortGetCoreID()));
  59. }
  60. TEST_ASSERT_NOT_EQUAL(before_heap, heap_caps_get_free_size(HEAP_CAPS)); //Check tasks have been created
  61. //Delete the tasks, memory should be freed immediately
  62. for(int j = 0; j < NO_OF_TSKS; j++){
  63. vTaskDelete(handles[j]);
  64. }
  65. TEST_ASSERT_EQUAL(before_heap, heap_caps_get_free_size(HEAP_CAPS));
  66. /* Test self deleting no affinity task is not removed by idle task of other core before context switch */
  67. for(int i = 0; i < DELAY_US_ITERATIONS; i+= 10){
  68. vTaskDelay(1); //Sync to next tick interrupt
  69. xTaskCreatePinnedToCore(tsk_self_del_us_delay, "delay", 1024, (void *)i, UNITY_FREERTOS_PRIORITY - 1, NULL, tskNO_AFFINITY);
  70. ets_delay_us(10); //Busy wait to ensure no affinity task runs on opposite core
  71. }
  72. }