Search code examples
cfreertosrtosesp-idf

Freertos tasks context switching


Context switching as I understood it, is when a highier priority task becomes ready and the schedular immediately selects it to run saving the context of the previous task so it continues once this new high priority task is blocking. So what happens when a task calls a blocking function ex: vTaskDelay() mid task, does context switching happens the same way as described or will the task start from the start.

Take this code for example

int i = 0;
while( c != EOF ){
     printf("%c",c);
     if( i == 10 ){vTaskDelay(100/portTICK_PERIOD_MS); }
     i++;
}

Does the loop continue or start from i=0


Solution

  • so what happens when a task calls a blocking function ex: vTaskDelay()

    vTaskDelay() is not blocking. The task is put into the BLOCKED state and control is passed to another task and your processor will execute other tasks. The control is passed back to the task at some point when delay passes.

    Context switching as I understood it, is when a highier priority task becomes ready

    It you do not enable round-robin in the freeRTOS configuration the control will only passed to another task when you allow it (by [for example] calling vTaskDelay() function, semaphore, queue etc functions). freeRTOS is not like big OSes.

    mid task, does context switching happens the same way as described or will the task start from the start.

    The execution will continue from from the next statement after the vTaskDelay().