prepare_task_switch(9)
π prepare_task_switch(9) β like, the official kernel API manual, bestie β¨
Okay so real talk: this is a page from the Kernel API Manual and it's all about prepare_task_switch β the little pre-game ritual the scheduler does before it hands the CPU to somebody else. It's short, it's iconic, and it's load-bearing, so pay attention π«Ά
β¨ SYNOPSIS β¨
void prepare_task_switch (struct rq *rq , struct task_struct *prev ,
struct task_struct *next );
That's the whole thing. Returns void. No notes. No drama. Just vibes and a context switch incoming π¦
π ARGUMENTS π
rqβ the runqueue that's getting ready to do the switch. It's the one throwing the party πprevβ the current task, aka the one being switched out. It's leaving the group chat πnextβ the task we're going to switch to. Main character energy, arriving now π
π₯ DESCRIPTION π₯
So here's the tea: this gets called with the rq lock held and interrupts off. That's not optional, that's the whole deal, no cap.
It must be paired with a subsequent finish_task_switch after the context switch. Like, they're a matching set. Separating them is giving "unfinished business" and we don't do that here π
What it actually does: prepare_task_switch sets up locking and calls architecture specific hooks. Architecture specific. Like, the arch code gets to do its own little thing. Slay β¨
β οΈ Genuine caution, bestie: rq lock held, interrupts off. Do not show up to this function unprepared. It is not a "figure it out as you go" situation π
π SEE ALSO π
Kernel file ./kernel/sched/core.c β and like, a whole friend group of related functions:
wake_q_add(9)wake_q_add_safe(9)task_curr(9)try_to_wake_up(9)task_call_func(9)cpu_curr_snapshot(9)wake_up_process(9)preempt_notifier_register(9)preempt_notifier_unregister(9)finish_task_switch(9)β the bestie, obviously πschedule_tail(9)schedule_preempt_disabled(9)preempt_schedule_notrace(9)init_idle(9)curr_task(9)
And that's the page, periodt π Huge thanks to ./kernel/sched for keeping the receipts β this one's from the October 2026 release of the Kernel API Manual. Go forth and switch tasks responsibly, babe π§π
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms