Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Deleting the Running task

Posted by Nobody/Anonymous on April 1, 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.

RE: Deleting the Running task

Posted by Richard on April 1, 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.


RE: Deleting the Running task

Posted by Glen B. on 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?

RE: Deleting the Running task

Posted by Richard on April 29, 2006
Sorry - oversight. I have added it to the list (again).


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Version 10.1.0 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner