Skip to content

4.2 Scheduler and Task Lifecycle

Glen edited this page Nov 14, 2026 · 2 revisions

Scheduler and Task Lifecycle

Relevant source files

Purpose and Scope

This page details the implementation of CadenzaOS's cooperative and preemptive task scheduling subsystem src/kernel/scheduler.zig1-11 It covers task state definitions, register context structures (TaskContext, InterruptContext) src/kernel/scheduler.zig36-88 dynamic task registration, interrupt-driven preemption via irq0_stubsrc/kernel/interrupts/irq_stubs.asm105-160 and low-level cooperative context switching via switch_taskssrc/kernel/arch_utils.s23-85

Sources: src/kernel/scheduler.zig1-11, src/kernel/scheduler.zig36-88, src/kernel/interrupts/irq_stubs.asm105-160, src/kernel/arch_utils.s23-85


4.2.1 Task States, Contexts, and Data Structures

CadenzaOS defines explicit representations for task execution states and processor register frames within src/kernel/scheduler.zig.

  • TaskState: An enumeration of possible task execution conditions (Ready, Running, Suspended, and Dead) src/kernel/scheduler.zig36-50
  • TaskContext: A packed struct representing the register layout preserved during cooperative context switches, encompassing general-purpose registers (r15 down to rax) and the instruction pointer (rip) src/kernel/scheduler.zig52-62
  • InterruptContext: A packed struct capturing complete processor state during interrupt-driven switches, combining GPRs saved by assembly stubs with the hardware trap frame (rip, cs, rflags, rsp, ss) pushed automatically by the CPU src/kernel/scheduler.zig64-88
  • Task: The core scheduler-owned task record containing its unique identifier (id), saved stack pointer (stack_ptr), current TaskState, wake_tick, and backing allocations for stack and code memory src/kernel/scheduler.zig90-113
  • resolveUserRange: A utility function within the scheduler to translate a task-supplied address range into a kernel-virtual slice. It handles various address forms including physical, higher-half virtual, and bare offsets within the task's code or stack memory src/kernel/scheduler.zig132-175

Task State Transition Model

diagram

Figure 4.2.1: Mapping between natural language lifecycle states and TaskState enum variants in src/kernel/scheduler.zig.

Sources: src/kernel/scheduler.zig36-113, src/kernel/scheduler.zig132-175


4.2.2 Scheduler Initialization and Task Registration

The Scheduler struct maintains the master task table (tasks), the active task index (current_task_idx), preemption flags (yield_enabled), and a global tick counter (ticks) src/kernel/scheduler.zig178-194

  • Scheduler.init: Allocates and returns an empty scheduler instance with all task slots initialized to nullsrc/kernel/scheduler.zig201-208
  • registerCurrentThreadAsTask: Converts the initial kernel execution context into a scheduler-managed task (slot 0) by allocating contiguous stack pages via frame allocators without forcing an immediate context switch src/kernel/scheduler.zig210-230
  • registerDynamicTask: Validates argument size limits (MAX_ARGS, MAX_ARG_BYTES), finds an unused task slot, assigns a unique identifier, allocates dedicated stack pages, and builds an initial task record src/kernel/scheduler.zig29-35src/kernel/scheduler.zig231-300

Sources: src/kernel/scheduler.zig29-35, src/kernel/scheduler.zig178-300


4.2.3 Preemption and Interrupt-Driven Switching

Timer-driven preemption relies on hardware interrupt 0 (IRQ0). The low-level assembly stub irq0_stub preserves general-purpose registers to match TaskContext, invokes the timer handler and scheduler preemption handler preempt_handler, and restores the next task's execution environment src/kernel/interrupts/irq_stubs.asm105-160

Preemption Data Flow

diagram

Figure 4.2.2: Execution flow mapping hardware timer preemption from irq0_stub through preempt_handler.

Sources: src/kernel/interrupts/irq_stubs.asm105-160, src/kernel/scheduler.zig1-11


4.2.4 Cooperative Context Switching (switch_tasks)

Low-level context switching between threads is performed by the assembly routine switch_tasks defined in arch_utils.ssrc/kernel/arch_utils.s23-85

  1. Saves all general-purpose registers (rax through r15) onto the current task stack src/kernel/arch_utils.s33-53
  2. Stores the active stack pointer into *old_rsp ((%rdi)) and loads new_rsp (%rsi) into %rspsrc/kernel/arch_utils.s55-58
  3. Pops general-purpose registers in reverse order from the new task stack src/kernel/arch_utils.s60-78
  4. Executes a standard ret instruction to transfer control to the restored task's instruction pointer src/kernel/arch_utils.s80-85

Sources: src/kernel/arch_utils.s23-85

Clone this wiki locally