---
title: "preemption"
source: https://www.jemoka.com/posts/kbhpreemption/
---

We use interrupts to implement preemption, &ldquo;preempting&rdquo; threads in order to swap on another thread to CPU. This enables scheduling to happen.
preempting into a brand new thread IMPORTANT: because interrupts are disabled at the beginning of the interrupt handler, and re-enabled by the end, new threads (which starts not at the interrupt handle) will not re-enable interrupts.
void interrupt_handler() { /* disables interupts, automatically by timer handler */ // future spawns start here context_switch(...); /* enables interupts, automatically by timer handler */ } void threadfunc_wrapper() { // manually enable interrupts before first run intr_enable(true); // start thread&#39;s actual business threadfunc(); }
