Hook automatic task deletion?

I want to implement a lightweight actor-framework for RTOS using statically allocated queues and tasks. Usually, like in erlang, tasks will message to an observant task if they fail so the observant task can restart them if necessary. For this I would need to hook the automatic task deletion (when base task function terminates) somehow. Can this be done?

If you want a hook when a task is deleted, you can use traceTASK_DELETE trace macro - FreeRTOS-Kernel/tasks.c at 356fff8028734dc50a46972b2e315e2871b0a12e · FreeRTOS/FreeRTOS-Kernel · GitHub

@aggarg Perfect thank you! I never knew what those macros were for. I see that TaskCreation also invokes a macro traceTASK_CREATE that I can use as well :slight_smile:

@aggarg: After looking into it more closely: Is it possible that the macro is only called on vTaskDelete calls that originate from another thread? In this case, task deletion in two cases would not be covered:

  • When the task deletes itself
  • When the task just runs out of instructions (leaves base callstack)

Is there a way to hook into these cases as well?

The Idle task cleans up the memory and calls the task delete hook in the case of a task deleting itself. See https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/V10.4.6/tasks.c#L3719, which is called from the Idle task.

Which port are you using? Some enable you to run off the end of the implementing function into another function, but only if you configure it to do so. For example, the Cortex-M port enables you to define a return address by setting this macro: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/V10.4.6/portable/GCC/ARM_CM3/port.c#L102 - although it is only intended for use to catch errors where a task does that. If you use it you should probably call vTaskDelete( NULL ) from whichever function you implement for the task to return too - although I’ve not tried it.

@rtel Thank you! I’m using the esp32. Very interesting feature with the return address. Begs the question if it is a good idea to touch it beside of debugging. I think that could very easily lead to spaghetti-code…

I’d recommend to use it only for debugging to catch the tasks which return - this way you can explicitly control the lifetime of tasks.

@aggarg Thank you that makes sense!