industrialNETworXnetx

Rainer Versteeg

Rainer Versteeg

| 24.07.2008 | 09:36 | 12 replies

Create Task at runtime

HI,

How can i create a Task at runtime !

Give there an example ?

Br,

Rainer

Andreas Jacob

Andreas Jacob

Hilscher Gesellschaft fuer Systemautomation mbH

| 24.07.2008 | 10:05

Hi,

please have a look into the latest rcX API manual (Rev4).
There is exaplained, how to create a dynamic task. The needed function is "rX_SysCreateTask".

Rainer Versteeg

Rainer Versteeg

| 24.07.2008 | 10:18

HI AJ,

I have the linkable object for Ethernet IP.

Now i want to start the task at runtime.

eRslt=rX_SysCreateTask("CL1_TASK",                        // Set Identification 
                         TaskEnter_EipCl1,  // Task function to schedule 
                         (UINT32)&tEipCl1TaskParam,         // Startup Parameter
                         0,                                 // Pointer to Stack
                         RX_TASK_AUTO_START,                // Start task automatically
                         1096,                              // Size of Task Stack
                         
                         0,                                 // Threshold to maximum possible value
                         TSK_PRIO_10, TSK_TOK_10,           // Set Priority,and Token ID 
                         0,                                 // Set Instance to 0 
                         (void (FAR*)(void FAR*))TaskExit_EipCl1);   // Function called when Task will be deleted 
  
  eRslt=rX_SysIdentifyTask("CL1_TASK",0,&hTsk,&eTokTsk,NULL);

The creation of the "CL1_Task" works. But when i want to Identify the task the CPU hangs on that command and dont come back.

Can you help me ?

Br,
Rainer

Andreas Jacob

Andreas Jacob

Hilscher Gesellschaft fuer Systemautomation mbH

| 24.07.2008 | 10:33

Hi Rainer,

:? mh... I miss one parameter in your structure. I only count 10 parameters in your example. If I have a look into the AP_Task header file I can see 11 parameters.

rX_SysCreateTask              (CONST STRING FAR*            pszTskName,
                                                   void (FAR*                   fnTsk)(void FAR* pvInpt),
                                                   void FAR*                    pvInpt, 
                                                   void FAR*                    pvStack,
                                                   UINT                         uStackSize,
                                                   UINT                         uStartMod,
                                                   RX_TASK_PRIORITY             eThreshold,
                                                   RX_TASK_PRIORITY             ePriority,
                                                   RX_TASK_TOKEN                eToken,
                                                   UINT                         uInstance,
                                                   void  (FAR*                  fnTaskLeave)(void FAR* pvInpt));

BTW:Which version of the rcX are you using?

Rainer Versteeg

Rainer Versteeg

| 24.07.2008 | 10:58

Hello AJ,

There are 11 entries. Priority and Token are in the same line.

I use the newest rcx from the netx Ethernet / IP Adapter Linkable Object for rcx CD.

The Task that i call is described in the linkable object library.
For the Ethernet/IP Adapter there must start 4 Task.
1. OBJECT_TASK
2. TCPDP
3. ENCAP_TASK
4. CL1_TASK

The first 3 Task are created correct. When i start 4. Task the Task are started correct too, but in the next line the CPU hangs !

Is there enough to create the Task! What i make wrong ?

PS: There is a error in the rcx API Manual. uStckSize is changed with uSrtMod on page 28 with the example on page30.

What is right ?

Andreas Jacob

Andreas Jacob

Hilscher Gesellschaft fuer Systemautomation mbH

| 24.07.2008 | 14:48

I think it would be the best to start all the tasks of the stack at the same time, like it is shown in the attached code snippet.

Please use instead of the command "RX_TASK_AUTO_START" the "RX_TASK_AUTO_START_INIT_WAIT" command.
In this case are all tasks started, when the rXStartNextTask is started otherwise you could get in resource problems.

{
                UINT uStartMode = (UINT)patStatTsk->ulSrtMod;
                if(RX_TASK_AUTO_START == uStartMode)
                  uStartMode = RX_TASK_AUTO_START_INIT_WAIT;
                  
                  if((erXRes = rX_SysCreateTask((STRING FAR*)&patStatTsk->szTskNam[0],
                                        patStatTsk->fnTask,
                                        (void FAR*)&patStatTsk->ulInp,
                                        patStatTsk->pvStck,
                                        (UINT)patStatTsk->ulStckSiz,
                                        uStartMode,
                                        (RX_TASK_PRIORITY)patStatTsk->ulThrhld,
                                        (RX_TASK_PRIORITY)patStatTsk->ulPrio,
                                        (RX_TASK_TOKEN)patStatTsk->ulTok,
                                        (UINT)patStatTsk->ulInst,
                                        (void (FAR*)(void FAR*))patStatTsk->fnTskLve ))!=RX_OK)
                  {
                    ptPck->tHead.ulSta = TLR_E_FAIL;
                    ptPck->tHead.ulLen = 0x00000000L;
                    /*return packet*/
                    TLR_QUE_PACKETDONE(ptRsc->tLoc.hPool, ptRsc->tLoc.hQue, ptPck);
                    return;
                  }
              } 
              patStatTsk++;
            }
            /* start now the Task with the highest Priority, this function call will NEVER return */
            rXStartNextTask();

HP

HP

| 25.07.2008 | 11:01

Could it be that the task you start is having a higher priority than the one that is starting it, and you don't go to sleep on the new task? This would look like the identifying would be hanging, when actually the original task is not getting any cpu time.....

Just my 2c....

Rainer Versteeg

Rainer Versteeg

| 25.07.2008 | 12:29

HI HP,

The program hangs on the line:

TLR_TSK_SET_STATE(eRslt);

in the funtion Taskenter();

PS: Have you an example to create a task at runtime ?

Br,
Rainer

HP

HP

| 25.07.2008 | 13:21

Yes, we do that all the time, in fact we even delete and recreate tasks during runtime. Here's an example of the creation:

    erXRes = rX_SysCreateTask("TSK_IF1",
                              (void(FAR*)(void FAR*))TaskEnterIf,
                              (void*)pIf1Config,
                              (void*)&auTskStackIf1[0],
                              TSK_STACK_SIZE_IF,
                              RX_TASK_AUTO_START,
                              0,
                              TSK_PRIO_IF1,
                              TSK_TOK_IF1,
                              0,
                              (void(FAR*)(void FAR*))TaskLeaveIf);
    if (erXRes != RX_OK)
      iConfigError = 6;

This task can be identified and deleted again using the following code segment:

if (rX_SysIdentifyTask("TSK_IF1",0,&hTempTsk,NULL,NULL) == RX_OK)
    rX_SysDeleteTask(hTempTsk,500);

I cannot comment on your use of TLR_TSK_SET_STATE(eRslt); since we do not use the task layer reference modell.

Rainer Versteeg

Rainer Versteeg

| 25.07.2008 | 13:51

HI HP,

Thank you for your answer !

When do you create the task ?

Do you mean that the task that create the task must be higher priority than the creation task.

Br,
Rainer

HP

HP

| 25.07.2008 | 13:56

We have some static tasks which are started by the scheduler. One of these tasks is intializing the other tasks. It does not really matter when you do it.

About the priorities: lets name the creating task the master and the created task the slave (it is less confusing for me :P)

If the master is having a lower priority than the slave, and the slave does not go to wait or sleep somewhere once it is created, then the master task wont ever get time from the scheduler again, leading to a lock of this task. This is what could be happening in your case. Just add a rX_SysSleepTask(RX_INFINITE); to the slave task somewhere, it will block him forever and your master task should continue. This is of course just to see if this is the case, in most cases it does not make sense to let tasks sleep forever :wink:

Rainer Versteeg

Rainer Versteeg

| 25.07.2008 | 14:03

HI HP,

I can not change the source of the slave task, because it is a LIB file. That are 4 task of a Ethernet IP Library.

Br,
Rainer

HP

HP

| 25.07.2008 | 14:28

Oh ok, I did not think of that. Well, just make your own task highest priority, it will have the same effect.

Login