Deleting the Running task

nobody wrote on Saturday, April 01, 2006:

If a task calls vTaskDelete() passing its own task
handle rather than NULL, then there can be a problem.
This is because the final taskYIELD() is not called.
I fixed it by putting the following within  the
critical section of the main body of vTaskDelete()
after it gets the pointer to the TCB to delete:

    if ((tskTCB *)pxTaskToDelete==pxCurrentTCB)
        pxTaskToDelete = NULL;

(Testing pxCurrentTCB must be protected.)

Although it may seem strange to call vTaskDelete() with
the running task’s handle, my situation was that it
was being called from some common task cleanup code that was
unaware if who was calling it. Of course, the fix could be at
the point of call, but my feeling is that it best belongs
in the vTaskDelete() function.

A point of interest - I found that the problem can
manifest itself as intermittent. If an interrupt reschedules
another task a just the right moment, then the taskYIELD()
is, of course, unnecessary. It made for interesting
debugging, because when I single stepped through vTaskDelete()
everything worked fine, but normally it it didn’t.

… I use the ARM7 port.

rtel wrote on Saturday, April 01, 2006:

The original thinking on this was that a task should not delete itself by passing in its own handle, but as you point out there is nothing stopping it from doing this.  Passing in NULL forces the application designer to specifically say "I really mean to do this".

In your case, does the generic clean up code know it is deleting itself.  If it is deleting all the tasks in the system then it will have to delete itself last.

I think you are right that this needs to be more specific, the question is should:

1) Passing in the current task handle cause the function to return without doing anything.  The rationale being that you have passed in an invalid input as NULL is the only way of guaranteeing the current task gets deleted.

2) Passing in the current task handle causes the current task to be deleted reliably.

I’m with you on my preference for 2) so have added the following code as per your suggestion:

/* Ensure a context switch will occur if the task being deleted
is the calling task but NULL was not used as the parameter. */
if( pxTaskToDelete == pxCurrentTCB )
____pxTaskToDelete = NULL;

Thanks for your contribution.


anonymous wrote on Friday, April 28, 2006:

I have just integrated FreeRTOS 4.0.1 into my project, and have discovered that this fix is not in.

Richard, was this an oversight, or did you change your thinking on it?

rtel wrote on Saturday, April 29, 2006:

Sorry - oversight.  I have added it to the list (again).