This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TSK_delete

Other Parts Discussed in Thread: SYSBIOS

Hi DSP/BIOS experts,

We have a special application for DSP/BIOS and some questions. We use DSP/BIOS 5.42.1.9.

What we have to do is to make a "non realtime algorithm" realtime capable. We start a thread with an algorithm running. This thread is allocating some memory. We save information about allocation in a structure. Another task controls the algorithm task with a timeout. If a timeout occurs the control task will call TSK_delete() for the algorithm task.


Our idea was to generate a function "algorithm_free" that frees allocated heap memory. It should be called by a system wide delete function like described in the DSP/BIOS manual.

Question: What happens if the algorithm task is in a system function like malloc while deleting? Is it possible? Could the system crash?

Question: Is it common to create realtime behaviour in that way or are there other possibilities to stop the task? The algorithm itself is not able to check the time.


On the other hand we have some global variables in the algorithm class that we have to reinitialize. The variables are in the .cinit section and get initialized at startup time.

Question: Is it possible to reinitialize them by doing the same like the startup code?

Thanks for your answers!

Best regards,

Volker

  • Hi Volker,

    Volker Schnell said:
    If a timeout occurs the control task will call TSK_delete() for the algorithm task.

    Is the call to TSK_delete() happening while the algorithm task is still running?

    If so, that could be a problem.  You may want to use semaphores to signal to/from the control task to the algorithm task as a way to allow the algorithm task to exit before calling TSK_delete().  From the BIOS API Guide:

    "Unless the mode of the deleted task is TSK_TERMINATED, TSK_delete should be
    called with care. For example, if the task has obtained exclusive access to a resource,
    deleting the task makes the resource unavailable."

    Volker Schnell said:
    Our idea was to generate a function "algorithm_free" that frees allocated heap memory. It should be called by a system wide delete function like described in the DSP/BIOS manual.

    Are all of these allocations done in the algorithm task?  If so, you might be able to do this in a task delete hook (called automatically when all tasks are deleted).

    Volker Schnell said:
    Question: Is it possible to reinitialize them by doing the same like the startup code?

    Maybe you need some "startup function" or some such that initializes these variables.  I would say make it an "init" function, but typically an "init" function would only be called once.  Or, maybe you can find a way to make them not be global variables.

    Steve

  • Hey Steven,

    we did some further investigations on that topic.

    Assume we have 2 tasks. A control task (prio: 2) and a working task (prio: 1). The algorithm in the working task is a quite complex lib call, so we do not have the possibility to call TSK_exit().

    If a timeout occurs the task need to be deleted by the control task. Due to RTS lib calls it is possible that the working task hold a RTS resource mutex so after the call to delete the mutex will never be released (as mentioned in BIOS API Guide).

    So the idea is to get the lock on that mutex before deleting the task. I call:

    __TI_resource_lock(__TI_LOCK_MAX);
    __TI_resource_unlock(__TI_LOCK_MAX);

    in the task delete hook. This seems to work hence the working task wont relock the mutex cause task priority is lower. Inside the RTS lib there are more than on resources to be locked. 

    Is it enough to lock/unlock only one of them or should I do the same with all resources?

    So is this is a save way to delete a task while running at all?


    Tobias

  • Deleting a non-terminated task is tricky. Grabbing the RTS lock is a good first step. Can you guarantee that no other resource will be left in a bad state (e.g. Semaphore, Event, System output, Log output, etc.)

    Do you know why the working task would have timed-out or are you just coding it up just in case?

    Todd

  • Hi Todd,

    thanks for reply.

    ToddMullanix said:
    Grabbing the RTS lock is a good first step

    Am I right that behind the 5 different RTS mutex IDs is only one DSP/BIOS LCK?

    ToddMullanix said:
    Do you know why the working task would have timed-out or are you just coding it up just in case?

    We use semaphores to trigger the working task and a queue to pass the data to be processed in the algorithm:

     

    void tskControl(void)
    {
        Object obj;
    
        // fill queue
        QUE_put(objectQueue, &obj);
    
        // start working task
        SEM_postBinary(startWorkingTaskMutex);
    
        TSK_yield();
    
        if (!SEM_pendBinary(stopWorkingTaskMutex, timeout))
        {
            deleteWorkingTask();
        }
    }

    void tskWorking(void)
    {
        while (1)
        {
            SEM_pendBinary(startWorkingTaskMutex, SYS_FOREVER);
    
            Object *p;
    
            if ((QUE_Handle)(p = (Object*)QUE_get(objectQueue)) != objectQueue)
            {
                runAlgorithm();
            }
    
            SEM_postBinary(stopWorkingTaskMutex);
        }
    }


    I suppose that the start/stop semaphore and the queue are not left in a bad state if working task is deleted. Inside the algorithm there are only calls to std C/C++ lib. To make it little bit more interesting the algorithm uses exception handling. I already did some tests where I could caught the exception in control task. I need to think about that, but at the moment it should work in a non exceptional case.

    Do we have a chance to get that save?


    Tobias

  • Tobias Heiss said:

    Am I right that behind the 5 different RTS mutex IDs is only one DSP/BIOS LCK?

    Which codegen (and version) are you using?

    Tobias Heiss said:

    Do we have a chance to get that save?

    ?

    Todd

  • We use CGT 7.4.4

    What else do we have to care about deleting a non-terminated task? Are there further characteristics related to exception handling?

  • SYS/BIOS registers it's own RTS lock function. It is called ti_sysbios_BIOS_rtsLock (and ti_sysbios_BIOS_rtsUnlock) in the large generated .c file (Debug->configPkg->package->cfg-><appname><suffix>.c).

    So if you call __TI_resource_lock(__TI_LOCK_MAX);, it ends up in ti_sysbios_BIOS_rtsLock which manages 1 gate (aka mutex).

    Other concerns really are application specific if you are not making in SYS/BIOS calls in the area that could get stuck.

    Todd