Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Deleting a task and erasing it's handler

Posted by chaabanemalki on October 26, 2015

Hello,

I'm trying to figure out a way on how to know if a task was created or not, and I did the following :

I have taskA and it's handler taskAHandler. I have 2 places to delete taskA : the first one inside the task itself, and the second place is in another task.

for both cases, I use this function :

TaskA_Delete() { if ( taskAHandler != NULL) vTaskDelete(taskAHandler); }

This works just fine inside the task itself or outside it. I want to chek if the task exists or not before deleting it, but it seems that the taskAHandler isn't set to NULL when its task is deleted !! Does the kernel erase the value on the task handler once the task is deleted ?

so I tried erasing the handler myself like this :

TaskA_Delete() { if ( taskAHandler != NULL) { vTaskDelete(taskAHandler); ** taskAHandler = NULL;** }
}

When I call the above fonction within taskA, the taskAHandler doesn't get erased. I assume that once the task is deleted, the execution of what follow is ignored ? am I right ?

Now, I can't really find a way to know if the task has been created or not ?

Any idea how to do that ?

Thank you.


Deleting a task and erasing it's handler

Posted by rtel on October 28, 2015

Sorry for the delay in replying, it appears several emails have got stuck in moderation over the last few days, even though the forum is set to automatically approve all posts from users who have logged in.

The task handles are variables like any other variable. They allow your application code to reference a task, but there is no link back the other way and FreeRTOS doesn't know anything about the variables, where the variable is allocated (stack, global, static, etc.) or how many copies of the variable you have made, or even if the variable still exists. So FreeRTOS cannot automatically set the variable back to NULL for you. If you need the variable to be set to NULL then it should be set manually after the task has been deleted - however that cannot be done if the task deletes itself because nothing after the call to vTaskDelete() will run (the task no longer exists so cannot be executing anything).

While a task cannot automatically clear a variable that points to it to NULL, it would be possible for the handle to be invalidated in some other way - for example for the data structures that describe the task to be updated to say the task is no longer valid. However that is not a practical option because every function that used a task handle would have to have code added to it to check the handle was valid or not, and once the task has been deleted the memory that was used by the task can be immediately re-used for any other purpose (including to hold data on a task that was subsequently created) so could hold any value anyway.

I would suggest your code is changed to something like the following - not the scheduler lock is used to ensure no other tasks try to use the task handle in the mean time:

~~~~ TaskADelete() { /* Remember the value of the variable as it will be set to NULL. */ TaskHandlet xTask = taskAHandler;

vTaskSuspendAll();

if( taskAHandler != NULL )
{
    /* The task is going to be deleted.
    Set the handle to NULL. */
    taskAHandler = NULL;

    /* Delete using the copy of the handle. */
    vTaskDelete( xTask );
}
xTaskResumeAll();

} ~~~~

A task can delete itself by calling vTaskDelete( NULL );


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




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists