Cooperative scheduler

Different policies can be defined to alternate the execution of the tasks in the system. In the simplest case, the main functions of each task voluntarily suspend its execution, by invoking the schedule macro.

In this example implementation, two threads are defined. Both will turn on an LED and hold the CPU in a busy-loop for one second, then turn off the LED, and explicitly call the schedule() function to trigger a context switch:

void task_test0(void *arg){    uint32_t now = jiffies;    blue_led_on();    while(1) {        if ((jiffies - now) > 1000) {            blue_led_off();            schedule();            now = jiffies;            blue_led_on();        }    }}void task_test1(void *arg){    uint32_t now = jiffies;    red_led_on();    while(1) {        if ((jiffies - now) > 1000) { red_led_off(); ...

Get Embedded Systems Architecture now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.