Function task::task_switch

source ·
pub fn task_switch(
    next: TaskRef,
    cpu_id: CpuId,
    preemption_guard: PreemptionGuard
) -> (bool, PreemptionGuard)
Expand description

Switches from the current task to the given next task.

Arguments

  • next: the task to switch to.
  • cpu_id: the ID of the current CPU.
  • preemption_guard: a guard that is used to ensure preemption is disabled for the duration of this task switch operation.

Important Note about Control Flow

If this is the first time that next task has been switched to, the control flow will NOT return from this function, and will instead jump to a wrapper function (that will directly invoke the next task’s entry point function.

Control flow may eventually return to this point, but not until another task switch occurs away from the given next task to a different task. Note that regardless of control flow, the return values will always be valid and correct.

Return

Returns a tuple of:

  1. a bool indicating whether an actual task switch occurred:
    • If true, the task switch did occur, and next is now the current task.
    • If false, the task switch did not occur, and the current task is unchanged.
  2. a [PreemptionGuard] that allows the caller to control for how long preemption remains disabled, i.e., until the guard is dropped.

Locking / Deadlock

Obtains brief locks on both the current Task’s inner state and the given next Task’s inner state in order to mutate them.