Thread self reference

nobody wrote on Thursday, October 28, 2004:

Hi,

How can I get the xTaskHandle for the current running thread?

Regards
Henric

rtel wrote on Thursday, October 28, 2004:

Why do you want to do this?  Functions such as vTaskSuspend( xTaskHandle) will suspend the currently executing task if NULL is passed in as the parameter.  There may be a way around what you are wanting to do like this.

The xTaskHandle is just a pointer to the task control block.  It’s type is hidden outside of tasks.c though (it is void* outside of tasks.c).  You can obtain it by adding the following function to tasks.c (I haven’t tried compiling this so watch out for typos):

xTaskHandle xGetCurrentTaskHandle( void )
{
xTaskHandle xReturn;

  portENTER_CRITICAL();
    xReturn = ( xTaskHandle ) pxCurrentTCB;
  portEXIT_CRITICAL();

  return xReturn;
}

An alternative and not very nice hack would be to simply declare:

extern volatile void * volatile pxCurrentTCB;

at the top of your application file.  pxCurrentTCB will then be the handle to the currently executing task.  This may cause a linker warning though and access to the extern variable would have to be only within critical regions so the first method is preferable.

nobody wrote on Friday, October 29, 2004:

Hi,

Thanks for a great RTOS. I’ll try the function xGetCurrentTaskHandle().

The first reason for I need this is that our software is message based, and each message has a sender and a receiver (UML state machines). Thus, I need to know the sender’s task handle when creating the message. The second is that the passive objects often use the task handle as an unique reference.

Also, the software is not all new code. It contains lots of old code written for pSOS, OSE, and eCos. Those OSes has a self reference function, and since this functions is used… well, sigh…

Best Regards
Henric

nobody wrote on Tuesday, November 30, 2004:

Hello,
Are u aware of Dr. Miro Samek QF (quantum frame work) or have you tried replacing UML state machines with QF. If so please share u’r experience.