Pumpkin, Inc.

Pumpkin User Forums

How to Replace a Task on-the-fly?

If you can't make Salvo do what you want it to do, post it here.

How to Replace a Task on-the-fly?

Postby Unregistered User » Mon Oct 01, 2001 8:44 am

I'm wondering if it's possible to replace a task on-the-fly. By that I mean that I want to have two different tasks use the same tcb (i.e. the same RAM), and I want the "switch" to happen in one of the tasks.
Unregistered User
 
Posts: 36
Joined: Thu Aug 09, 2001 11:00 pm

Re: How to Replace a Task on-the-fly?

Postby aek » Mon Oct 01, 2001 8:55 am

That's an interesting question ...

On the PICmicros, here's how you can do it:

code:
void TaskA( void )
{
...
OS_Delay(delay, labelA);
...
OSDestroy();
OSCreateTask(TaskB, OScTcbP, 7);
}


void TaskB( void )
{
while (TRUE) {
..
OS_Yield(labelB);
}
}


main()
{
OS_Init();

OSCreateTask(TaskA, OSTCBP(1), 5);

while (TRUE)
OSSched();
}


This little app will start with TaskA() running with priority five, but when TaskA() reaches that call to OSDestroy() (not OS_Destroy()), it will replace itself with TaskB() running with priority seven (lower). By replace I mean that the same tcb will be used for each task, thus saving you RAM. TaskB() will run after TaskA() returns to the scheduler after the call to OSCreateTask().

This works by first destroying (i.e. re-initializing) the current task (no big deal, OS_Destroy() is already in the API -- it's a macro that calls OSDestroy() and then OS_Yield()), then creating a new task in its place. The key is that it is not followed by OS_Yield(), but rather by a simple return-from-subroutine. This returns control to the scheduler without changing the current tasks's "start/resume address" that was set with the call to OSCreateTask(). If you can't / don't want to place this code at the very end of a task, you can put this anywhere in the task for the same effect:

code:
  OSDestroy();
OSCreateTask(TaskB, OScTcbP, 7);
asm(" return");

I can see that this is a pretty useful service -- perhaps we can add it to v2.3.0 as OS_Replace(), to be called like this:

code:
OS_Replace(TaskName, priority);

------------------

[This message has been edited by aek (edited October 01, 2001).]

-------
aek
aek
 
Posts: 1888
Joined: Sat Aug 26, 2000 11:00 pm

Re: How to Replace a Task on-the-fly?

Postby aek » Tue Oct 02, 2001 9:10 am

Done. OS_Replace() will be part of the v2.3.0 release.

------------------

-------
aek
aek
 
Posts: 1888
Joined: Sat Aug 26, 2000 11:00 pm


Return to Coding

Who is online

Users browsing this forum: No registered users and 3 guests

cron