Kernel - Processes and threads - Scheduler
The operating system scheduler controls threads execution with a predetermined policy. It is a part of Phoenix-RTOS having the most significant influence on the performance and responsiveness of the whole system.
The scheduling algorithm is defined in the
threads_schedule function. It is invoked by timer interrupt or voluntary
hal_cpuReschedule). Each function's invocation changes the context of the currently executed thread to
the next one, available in a thread's ready queue. The scheduling policy determined which thread shall be chosen for
the next execution. In Phoenix-RTOS a priority preempted round-robin algorithm is used. There are eight priority levels,
the smallest value holds the highest priority.
The thread management unit contains an eight-element array, where each of them holds a pointer to the list of threads of the same priority. A scheduling algorithm is defined as follows:
threads_common.spinlockis set before any operations on common data.
- The current thread's context for the interrupted core is saved and added to the end of its priority list.
- The next available thread with the highest priority is selected to be run and is removed from the ready thread list. If a selected thread is a ghost (a thread whose process has ended execution) and has not been executed in a supervisor mode, it is added to the ghosts list and the reaper thread woke up.
- For the selected thread, the following actions are performed:
- A global pointer to the current thread is changed to the selected one,
- A pointer to the kernel stack is updated to the stack of a new thread,
- A memory map is changed to the map associated with the thread's process,
- Signal handlers are performed,
- Performance data is saved in a perf unit,
- In the
hal_cpuRestorepointer to the stack in a context of current thread is updated with a pointer to the stack of selected thread. When the scheduler finishes work, the context of selected thread restore is performed.
- The CPU usage is updated for the current and selected thread.
- At the end of the modification of the