4.4 Scheduling

Scheduling is the software deciding which task to run at what time. FreeRTOS has two modes of operation when it comes to handling task priorities: With and without preemption. Which mode to use can be set in the configuration file. When using the mode without preemption, also called cooperative mode, it is up to the developer to make tasks that yield the CPU through the use of blocking functions and the taskYIELD() function.

When using a preemptive scheduler, a task will automatically yield the CPU when a task of higher priority becomes unblocked. However, there is one exception: When a higher priority task blocks from an ISR, the taskYIELD_FROM_ISR() function has to be called at the end of the ISR for a task switch to occur.

If configUSE_TIME_SLICING is set to 1, the scheduler will also preempt tasks of equal priority at each time the tick occurs. Time slicing is not available in cooperative mode.

In both modes, the scheduler will always switch to the highest priority unblocked task. If there are multiple tasks unblocked with the same priority, the scheduler will choose to execute each task in turn. This is commonly referred to as round robin scheduling.

In the preemptive mode, higher priority tasks are immediately switched to when they get unblocked. In the cooperative mode, the release of a semaphore might unblock a higher priority task, but the actual task switch will only happen when the currently executing task calls the taskYIELD() function or enters a blocking state.

For more information on scheduling, see the “Scheduling Algorithms” section in the Mastering the FreeRTOS Real Time Kernel - a Hands On Tutorial Guide.