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

wrong return value xTaskGetCurrentTaskHandle

Posted by roadrunner123 on October 4, 2007
Hi folks,

we are using FreeRTOS on a AT91SAM7 system and have encountered a mysterious behavior of the xTaskGetCurrentTaskHandle() function. We understood that this function will give us back the task handle of the currently running task, wich we like to use to identify in which task we are running to implement thread-save code by allowing recursion. The mystery is that this function gives us all the time random values back! We extected only the values as we got back once we created our tasks by calling xTaskCreate()! What is wrong?

Any help would be appreciated!

thanks,
roadrunner123

Here our code:

void funtion()
{
//>>> This part makes the function thread save
static xTaskHandle taskhandle=NULL;
static xSemaphoreHandle threadlock=NULL;
if(taskhandle!=xTaskGetCurrentTaskHandle())
{
if(threadlock==NULL)
{
vSemaphoreCreateBinary( threadlock );
}
// waiting 1 ms for Semaphore
if(xSemaphoreTake(threadlock, 1) != pdTRUE) return ERR_TLOCK;
}
taskhandle=xTaskGetCurrentTaskHandle(); // gives back random values
//<<<

// do something foo bar

//>>> OK release Threadlock here
xSemaphoreGive(threadlock);
//<<<

return;
}

RE: wrong return value xTaskGetCurrentTaskHandle

Posted by Dave on October 4, 2007
What is this code supposed to do? I would have thought using static variables in the function will have the opposite effect of making it not thread safe.

Obtaining the task handle is a very simple function. If you are really getting random values back then it would suggest some stack corruption. This would probably occur prior to calling your function as the function does not use much stack itself.

RE: wrong return value xTaskGetCurrentTaskHan

Posted by roadrunner123 on October 4, 2007
Hi davedoors,

I think it is not a stack problem because in my original code I have a lot of stuff where I put the comment "//do somthing" in this forum to save space. The function is behaving as expected but xTaskGetCurrentTaskHandle returns random values each time my function is called!

This line:
if(taskhandle!=xTaskGetCurrentTaskHandle())
Should bypass the Semaphore if the caller runs in the same task as the one who tooks the semaphore. This will allowing the funtion to run recusionlly!

This lines:
if(threadlock==NULL)
{
vSemaphoreCreateBinary( threadlock );
}
// Eine ms auf auf die Semaphore warten
if(xSemaphoreTake(threadlock, 1) != pdTRUE) return ERR_TLOCK;
Will make the funtion thread-save because the Semaphore can only be taken by one task!

This line:
taskhandle=xTaskGetCurrentTaskHandle();
Will set the taskhandle or the current task if the Semaphore has been taken!

Any help would be appriciated,
thanks, roadrunner123


[ 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