Get task running in error trap and terminate

anonymous wrote on Sunday, June 10, 2012:

My dsPIC processor supports error trap interrupts such as divide by zero, memory access violation, etc.

If one of these occurs then it is likely the task currently running has caused it. I would like to avoid a complete processor reset and just end the appropriate task. So, I will need to get the task pointer of the currently running task and terminate it. So, the code would look something like this:

void div_zero_exception()
   xTaskHandle tp = xTaskGetCurrentTaskHandle(); 
   _debug_msg("[****] Task terminated: %s due to zero division", pcTaskGetTaskName(tp));

This will run from the task that is being terminated - would this cause any problems? This also only suspends the task - ideally, it would be completely terminated and all memory related to the task would be freed.

richard_damon wrote on Monday, June 11, 2012:

One concern I would have is that you don’t know for sure that the exception is coming from a task, it could also be coming from an interrupt handler. You also need to be careful about is killing a task at an unknown point, as you have no Idea on the status of the resources it uses.

I would also be very nervous about using a “printf” like function at the exception level. Note that in the exception handler, ALL interrupts are disabled.

rtel wrote on Monday, June 11, 2012:

As per richard_damon’s reply, plus:

In this case, the exception is equivalent to an interrupt, so only FreeRTOS API functions that end if “FromISR” should be called.  I can’t remember the exact details of the PIC24 port, so you might get away with it, but keep in mind that you will be using the API outside of its intended scope.

Suspending the currently executing task will probably result in a reschedule, so your exception may never be concluded correctly (you may never clear the exception if another task starts running, and the stacks might be left in an inconsistent state).  You also have to consider how you are going to exit from the exception, if you have deleted or suspended the task that caused the exception does not cause another task to execute - what are you going to return to?