Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Coroutine Scheduling

Posted by dennis on 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!


RE: Coroutine Scheduling

Posted by Richard on 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.


RE: Coroutine Scheduling

Posted by Jeff Smith on 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.

RE: Coroutine Scheduling

Posted by dennis on 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?



RE: Coroutine Scheduling

Posted by Richard on 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.


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Version 10.1.0 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner