Pumpkin, Inc.

Pumpkin User Forums

same TCB task swtiching

If you have a general question or comment regarding Salvo, post it here.

same TCB task swtiching

Postby tyski » Sat Nov 12, 2005 1:08 am

This is probably more of a feature request, however, since I'm going to implement it myself, I'll post it in the "help now" section. Also, I'm working with PIC18F using the c18 compiler. And while the solution will be highly target dependent, the problem is not.

Basically, I have the following situation. I have multiple tasks that all need to run a specific sequence of code - code that requires delays, event handling, etc., etc. - code that is best suited to be in a task and not a 'normal' function. Now, I certainly don't want to write this same code multiple times for each of the "host" tasks. And I know the solution is obviously to place this code in a task and perform a context switch, then do the callback via an event of some sorts. However, this inherently requires two TCB's, one for the host task and one for the client task. The question then becomes, if all the host task is going to do is wait for the client task to finish, why waste a TCB on the client task?

If Salvo was a preemptive OS, this would be no problem at all - just call a function that performs the needed sequence of code. However, this is not the case.

My solution at this point is to roughly emulate the OS_Replace macro with my own special macro that stores the PC for return from the client task. I know this works as I've tested it in MPLAB SIM but I can see that it is fraught with danger, that is why I'm posting it here, for anybody's advice.

Also, if somebody can think of better ways of doing this, either at a high level, or a better macro, please let me know.

I have the following typedef and macro. The assumption can also be made that no other task will try to use the client task at the same time.

typedef unsigned short long OStypeCB;

#define OS_SwitchContext(tfp,prio,pCallBack)
{
OSCreateTask(tfp,OScTcbP,prio);
*((OStypeCB*)pCallBack)=(OStypeCB)PC;
*((OStypeCB*)pCallBack)+=30;
return;
}

and then the following code with two tasks

OStypeCB callback;
void HostTask(void) {
for (; {
// do some specific work
// do some more general work client task
OS_SwitchContext(ClientTask, 10, &callback);
// check result of client task
// do some more specific work
}
}
void ClientTask(void) {
// do some general work
// return back to host task
OS_Replace((OStypeTFP)callback, 10);
}

------------------
Tyrel Newton
Electrical Engineer

Tethers Unlimited, Inc.
11807 North Creek Parkway South, Suite B-102
Bothell, WA 98011-8804, USA
425-486-0100 x836 425-482-9670 FAX
newton@tethers.com
http://www.tethers.com/

Tyrel Newton
Electrical Engineer

Tethers Unlimited, Inc.
11807 North Creek Parkway South, Suite B-102
Bothell, WA 98011-8804, USA
425-486-0100 x836 425-482-9670 FAX
newton@tethers.com
http://www.tethers.com/

tyski
 
Posts: 17
Joined: Thu Aug 18, 2005 11:00 pm
Location: Bothell, WA, USA

Re: same TCB task swtiching

Postby tyski » Sat Nov 12, 2005 1:10 am

doh, should have turned the smilies off

that should be

for (;;) {

Tyrel Newton
Electrical Engineer

Tethers Unlimited, Inc.
11807 North Creek Parkway South, Suite B-102
Bothell, WA 98011-8804, USA
425-486-0100 x836 425-482-9670 FAX
newton@tethers.com
http://www.tethers.com/

tyski
 
Posts: 17
Joined: Thu Aug 18, 2005 11:00 pm
Location: Bothell, WA, USA

Re: same TCB task swtiching

Postby aek » Sat Nov 12, 2005 5:25 am

Hi Tyrel.

Well, there is another option -- how about Adam Dunkels' protothreads?

These provide a real "callback" functionality, though it is done to some degree with a C sleight of hand ...

Check out http://www.sics.se/~adam/pt/.

I've been wanting to do a combined Salvo + pt implementation (gets you al the functionality of Salvo, plus the callback functions that are similar to being able to context switch at a level deeper than the task level only ...

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

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

Re: same TCB task swtiching

Postby aek » Sat Nov 12, 2005 7:40 am

Hi Tyrel.

You can already do this easily (but you need Salvo Pro).

You just use tcb extensions -- all you'd need is one extension, which you initialize a priori for each task to be, say, a pointer to a task-specific structure. Then, using just a single task function that references the tcb extension within it (at runtime), you initialize all the tasks using that same, single function. When each task runs, it references its tcb extension to get whatever task-specific stuff it needs.

So the net cost in RAM is one tcb per task (as usual), one extra pointer per tcb (to hold the tcb extensions), and just a single function (the task itself).

You need Pro to enable the tcb extensions.

Other RTOSes that allow for you to pass arguments to the task would use the argument to do the task-specufic stuff. Salvo doesn't allow argument passing to tasks, so instead you use tcb extensions.

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

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

Re: same TCB task swtiching

Postby tyski » Sat Nov 12, 2005 10:43 am

I will consider the Salvo Pro part. I can see how it will work very well for solving the callback pointer issue.

But, this solution does not seem to solve the problem of returning to the host task exactly where it left off when it called the client task. The issue I have with my solution is the PC += 30 hack. There are a lot of issues with this.

------------------
Tyrel Newton
Electrical Engineer

Tethers Unlimited, Inc.
11807 North Creek Parkway South, Suite B-102
Bothell, WA 98011-8804, USA
425-486-0100 x836 425-482-9670 FAX
newton@tethers.com
http://www.tethers.com/

Tyrel Newton
Electrical Engineer

Tethers Unlimited, Inc.
11807 North Creek Parkway South, Suite B-102
Bothell, WA 98011-8804, USA
425-486-0100 x836 425-482-9670 FAX
newton@tethers.com
http://www.tethers.com/

tyski
 
Posts: 17
Joined: Thu Aug 18, 2005 11:00 pm
Location: Bothell, WA, USA

Re: same TCB task swtiching

Postby tyski » Sat Nov 12, 2005 11:40 am

Two things.

First, my current project cannot support the cost of Salvo Pro at the moment. That is why I'm going through these iterations.

Second, I found a better solution than the macro I had. And I'm betting its essentially the same way the context switcher is implemented for the c18/PIC18F setup.

So for now, this will have to do. Although the TCB extensions would be very useful here...

------------------
Tyrel Newton
Electrical Engineer

Tethers Unlimited, Inc.
11807 North Creek Parkway South, Suite B-102
Bothell, WA 98011-8804, USA
425-486-0100 x836 425-482-9670 FAX
newton@tethers.com
http://www.tethers.com/

Tyrel Newton
Electrical Engineer

Tethers Unlimited, Inc.
11807 North Creek Parkway South, Suite B-102
Bothell, WA 98011-8804, USA
425-486-0100 x836 425-482-9670 FAX
newton@tethers.com
http://www.tethers.com/

tyski
 
Posts: 17
Joined: Thu Aug 18, 2005 11:00 pm
Location: Bothell, WA, USA


Return to General

Who is online

Users browsing this forum: No registered users and 0 guests

cron