wrong return value xTaskGetCurrentTaskHandle

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; }

wrong return value xTaskGetCurrentTaskHandle

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.

wrong return value xTaskGetCurrentTaskHandle

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