arYield Function
Declaration
void arYield(void);
Description

This function triggers a manual context switch, allowing the current task to voluntarily relinquish the CPU to another task.

The routine executes a sequence similar to the standard preemption handler. The recommended implementation workflow is as follows:

  • Save the execution context (CPU registers) of the current task onto its stack.
  • Disable interrupts (preemption) to protect critical scheduler operations.
  • Save the current task's stack pointer into the global task context descriptor (a variable of type TTaskContext).
  • Switch to the operating system (scheduler) context/stack (see details below).
  • Invoke the operating system scheduler, passing the address of the global task context descriptor as a parameter.
  • Upon return from the scheduler, restore the task context (stack pointer) from the global descriptor (which may now point to a new task).
  • Restore CPU registers from the new stack and resume execution.

Disabling interrupts is mandatory to ensure the atomicity of the context switch and prevent scheduler re-entry. The interrupt enable state is intrinsic to each task and is preserved within its saved context. Explicit re-enabling of interrupts is generally not required within this function, as the state will be automatically restored when the next task resumes execution (assuming interrupts were enabled when that task was suspended).

The operating system context refers to a dedicated execution environment (specifically, a separate stack) used by the scheduler. This isolation ensures that the scheduler does not consume the stack space of user tasks. This context is initialized when the preemption handler is registered via the arSetPreemptiveHandler function. Please refer to its description for further details.

The scheduler function accepts a single parameter: a pointer to the global task context descriptor. It uses this to save the state of the preempted task. When the scheduler decides which task to run next, it updates this global descriptor. The arYield routine then uses this updated information to restore the context of the next task to be executed.

SpaceShadow documentation