Quality RTOS & Embedded Software

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


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!


Here our code:

void funtion()
//>>> This part makes the function thread save
static xTaskHandle taskhandle=NULL;
static xSemaphoreHandle 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


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:
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:
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:
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 ]    [ Privacy ]    [ Sitemap ]    [ ]

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

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Version 10.1.1 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