CooCox CoOS supports two kinds of scheduling mode, preemptive priority and round-robin. The former is used among tasks of different priority, while the latter among tasks of the same priority. CooCox CoOS will start a task scheduling in the following three situations:
- 1) A task whose priority is higher than the current running one is converting to the ready state
- 2) The current running task is changing from the running state to the waiting or dormant state
- 3) A task sharing the same priority with the current running task is in the ready state, and meanwhile the time slice of the current task runs out
When a system tick interrupt exits or some tasks’ states have changed, CooCox CoOS will call the task scheduling function to determine whether it is essential to start a task scheduling or not.
For the scheduling of tasks sharing the same priority, the system starts the rotation scheduling according to the time slice of each task. When the system has run out the time slice of the current task, it will give the right of control to the next task with the same priority. Figure 2.5.1 shows the system running state of the three tasks (A, B, C with their respective time slices 1, 2, 3) with the same priority when they are entering the ready state in turn.
In CooCox CoOS source codes, the implementing of task scheduling is shown as follows:
Code 4 Task with a higher priority is ready
Code 5 The state of the current task changes
Code 6 The task scheduling among the same priority tasks