Context Switching

The context switching is a hardware dependent part of the operating system. It is used in multitasking to allow simultaneous execution of tasks. It is realized by fast switching between execution of one task and another. This part of the port file is mandatory only when operating system is used (an OS_USED constant is set to 1).

To perform a switching from execution of one task to another, the current state of running task must be saved directly after halting it and before starting execution of the second task. State of the task is called a task context. It includes set of used CPU registers, and other necessary data, that is expected to continue task execution from the interrupted position. The standard scheme of the task switching realization is:

  • interrupting execution of the current task
  • saving current task context
  • determining a next task to run (done by operating system scheduler)
  • restoring the new task context
  • continuing execution of the new task

The task context is described by TTaskContext structure. All task context data may be stored in this structure, but it is recommended to hold only task stack information. The rest of task context data should be saved in the task stack, which allow faster task context saving and restoring. What more, it decreases time expected to manipulate the TTaskContextdescriptor.

Before the task will be executed first time, its stack and a startup context should be created with a default data. The Program Counter register - PC (also known as Instruction Pointer - IP), should be set to task startup procedure. When the context switch has been processed and a task is executed first time, the task execution will start from the startup procedure with the default context values.

Disabling and enabling task preemption can be realized simply by disabling or enabling the interrupts. Each task has an individual state of preemption enable. This information is also stored on the task context. When preemption is disabled for a currently executing task, switching to another task may be performed only by manual execution of the context switching (by calling an arYield function). When it is enabled, switching to another task will be realized every time when preemption occurs.

The following stuff should be defined when the Sirius RTOS is used:

  • Creating arLock/arRestore functions for disabling and re-enabling preemption
  • Defining an TTaskContext structure that contains the information that is required to save and restore task context
  • Creating arCreateTaskContext and arReleaseTaskContext functions to create and release task context
  • Creating a procedure that realizes the preemption and the arYield function for manual context switching
  • Creating an arSetPreemptiveHandler function to enable the operating system scheduler
  • Creating an arSavePower function used to saving power by disabling CPU when no one task is executing

All of the above items are detailed described in this content.

SpaceShadow documentation