Coroutine Scheduling

tcwden wrote on Friday, January 26, 2007:

Hi all,

I’m new to freeRTOS and coroutine.

I have a question about the function of coroutine scheduler in the documentation, which says:

"vCoRoutineSchedule() executes the highest priority co-routine that is able to run. The co-routine will execute until it either blocks, yields or is preempted by a task. Co-routines execute cooperatively so one co-routine cannot be preempted by another, but can be preempted by a task."

The above paragraph seems a little bit contradicting to me.

If the scheduler always schedules the highest priority co-routine first (presumably, at every system clock tick), then I would think that a higher priority coroutine can preempt a lower priority coroutine.

However, what it continues to say is that a coroutine will keep running until it blocks itself (by crDELAY, crQUEUE_SEND, or crQUEUE_RECEIVE). In that case, what is the use of priority for coroutine?

Also, assuming no tasks are running, when coroutine A (higher priority) blocks itself for t ms, and coroutine B (lower priority) starts running, will coroutine B stops and let A to run after t ms?

Thanks in advance!

Dennis

rtel wrote on Friday, January 26, 2007:

Co-routines will not preempt each other.  Therefore when a co-routine is selected to run it will continue to be the selected co-routine until such a time that it blocks or calls yield.  When a co-routine blocks the scheduler then re-evaluates which co-routine should be selected to run and will at that time choose the highest priority co-routine that is able to run (i.e. is not blocked).

Co-routines use a cooperative system, whereas tasks can be configured to be wither cooperative or preemptive.  The priority is used to select the next co-routine to run at each decision point.

Regards.

imajeff wrote on Saturday, January 27, 2007:

Hi Dennis,

You’ve got to be sure what “preempt” means. richard already said it but maybe my simple explanation will help: Coroutines are only cooperative (in your example cr A blocks itself by crDELAY, etc. therefore cr B can run while A is blocked)

Of course priorities are important each time vCoRoutineSchedule() determines which task should run next.

tcwden wrote on Monday, January 29, 2007:

Thank you Richard and Jeff!

From your description, can I say that when my coroutine executes
  crDELAY( xHandle, xDelayTime );
there is no gurantee that the coroutine will run after xDelayTime (or even close to it) because some other coroutine might be running and not blocking itself?

Cheers.

Dennis

rtel wrote on Monday, January 29, 2007:

That is correct.  It is the nature of cooperative scheduling.  If you need preemption then tasks are the only choice.  Using cooperative scheduling you have to ensure your low priority co-routines call yield often enough to ensure your high priority co-routines meet their real time deadlines.

Regards.