Quality RTOS & Embedded Software

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


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) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS kernel V10.0.1 is available for immediate download. Now MIT licensed.

Video: Watch James Gosling & Richard Barry at re:Invent, Las Vegas 2017.

New FAQ page about the FreeRTOS kernel and Amazon FreeRTOS.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

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