Definition
Cooperative scheduling and preemptive scheduling are two rules for when an operating system’s scheduler may switch the processor from the running thread to another thread. Under cooperative scheduling, the running thread keeps the processor until it gives it up by waiting or yielding. Under preemptive scheduling, the scheduler also switches as soon as a thread of higher priority becomes ready. Neither rule stops interrupts §6.11: unless interrupts are masked, an interrupt handler §6.12 can run in the middle of either kind of thread. Zephyr §6.33 gives each thread one rule through its priority.
The idea
Two threads update the same table. If the scheduler can switch threads anywhere, one thread may stop halfway through an update while the other reads the half-updated table, so every shared update needs a lock. If a thread runs until it chooses to stop, the programmer knows the only places where another thread can run, and code between those places sees no other thread. That is the attraction of cooperative scheduling: fewer locks and fewer surprises. The cost is responsiveness. A cooperative thread that computes for 20 ms makes every other thread wait 20 ms, however urgent.
Preemptive scheduling reverses the trade: an urgent thread runs at once, and shared data needs a lock wherever a switch can happen. Interrupts sit beside both rules, not inside them. The processor starts an interrupt handler whenever an enabled interrupt arrives, whatever kind of thread is running. Cooperative scheduling therefore protects shared data from other threads only, never from interrupt handlers.
How it works
Thread rules in Zephyr.
A negative priority makes a thread cooperative; zero or a positive priority makes it preemptive. A lower number means a higher priority, so every cooperative thread outranks every preemptive thread.
At each scheduling decision, the scheduler runs the highest-priority ready thread, and among ready threads of equal priority, the one that has waited longest.
A cooperative thread stays the current thread until it waits, for example on a semaphore §9.6 that is not available or in
k_sleep(), until it yields withk_yield(), or until it ends.A preemptive thread is also replaced as soon as a thread of higher priority becomes ready. With time slicing enabled (
CONFIG_TIMESLICING), a preemptive thread is also replaced by a ready thread of equal priority when its time slice ends.
Interrupts. When an enabled interrupt arrives, the processor suspends whatever thread is running, cooperative or preemptive, and runs the interrupt service routine (ISR), the function registered for that interrupt. When the ISR finishes, Zephyr decides where to continue.
If the interrupted thread is cooperative, execution returns to it, even if the ISR has made a higher-priority thread ready.
If the interrupted thread is preemptive and the ISR has made a higher-priority thread ready, Zephyr switches to that thread, and the interrupted thread resumes later.
Interrupt priorities, set in the interrupt controller (on Arm Cortex-M processors, the Nested Vectored Interrupt Controller §6.13, NVIC), form a separate scale from thread priorities. Any enabled interrupt runs ahead of every thread.
Tools that change the picture.
k_yield()puts the current thread behind the ready threads of its own priority and lets them, and any ready thread of higher priority, run first. A ready thread of lower priority still does not run.k_sleep()makes the current thread wait for a time, so a ready thread of any priority can run.k_sched_lock()makes the current preemptive thread behave as a cooperative one untilk_sched_unlock(). Interrupts still run.irq_lock()masks interrupts. This holds off interrupt handlers, and with them every thread switch that an interrupt would cause. It is the tool for data shared with an ISR, and it must be held only briefly.Zephyr can reserve its highest cooperative priorities for meta-IRQ threads, where IRQ stands for interrupt request (build option
CONFIG_NUM_METAIRQ_PRIORITIES). A meta-IRQ thread preempts even a cooperative thread, which suits work that an interrupt hands over and that must run at once.
Worked example
One processor runs two threads. Thread A, priority (cooperative), starts at 0 ms a computation that needs 20 ms of processor time and never waits. Thread B, priority (higher than A), waits on a semaphore. At 5 ms an interrupt arrives; its ISR runs for 0.05 ms and gives the semaphore, which makes B ready. B needs 1 ms.
From 5.00 to 5.05 ms the ISR runs, although A is cooperative.
At 5.05 ms the ISR returns. A is cooperative, so execution returns to A, although B has the higher priority. A has used 5 ms of its 20 ms and needs ms more.
A finishes at ms. B starts then and finishes at ms. B waited ms.
Now make A preemptive, with priority 3, and keep B at .
The ISR again runs from 5.00 to 5.05 ms.
At 5.05 ms the ISR returns. A is preemptive and B has the higher priority, so Zephyr switches to B. B runs from 5.05 to 6.05 ms and waited 0 ms.
A resumes at 6.05 ms with 15 ms of work left and finishes at ms.
Both runs end at 21.05 ms, because the processor did the same ms of work; only the order changed, and with it B’s wait, from 15 ms to 0 ms. In both runs the ISR ran in the middle of A, so if the ISR and A update the same variable, A needs protection against the ISR in both runs.
Stated precisely
Let the current thread be , with priority . On one processor, Zephyr switches from to another thread only at these events:
waits, yields, sleeps or ends (for any );
a thread of priority higher than ’s becomes ready, including at the return from an ISR, if , that is, if is preemptive;
’s time slice ends while a ready thread of equal priority exists, if is preemptive and time slicing is enabled;
a meta-IRQ thread becomes ready (for any ).
Separately, an ISR can begin between any two instructions of any thread whenever its interrupt is enabled and not masked.
Consequently, if is cooperative and is a ready thread that outranks every other ready thread, waits at most the longest stretch of ’s execution between two points where waits or yields, plus the time spent in ISRs during that stretch. A response-time §9.32 bound for needs a bound on that stretch.
Limits and common mistakes
Cooperative does not mean that interrupts are disabled. Data shared with an ISR needs interrupt masking or atomic operations §9.8, whichever class the thread has.
Cooperative does not mean alone. On a processor with several cores, a cooperative thread only keeps other threads off its own core; threads on other cores run at the same time and need locks for shared data.
A cooperative loop that never waits stops every other thread. If the loop never ends, only an interrupt handler or a watchdog timer §6.16, which resets the device when software stops reporting in, can recover the device. This is starvation §9.18 of the other threads.
k_yield() does not let lower-priority threads run. To let them run, the thread must wait, for example with k_sleep().
Preemption does not prevent priority inversion §9.16, in which a high-priority thread waits for a lock that a low-priority thread holds while medium-priority threads run. Zephyr’s mutexes §9.5 use priority inheritance §9.17 against it.
Neither class guarantees a deadline. Response times come from analysis of execution times, priorities and the points where threads wait or yield.
Thread priority and interrupt priority are different scales. No thread priority, however high, outranks an enabled interrupt, and an interrupt priority number says nothing about threads.
On Arm Cortex-M processors, Zephyr can mark some interrupts as zero-latency, and irq_lock() does not mask those. Code that holds irq_lock() is then still not protected from their handlers. The build option is CONFIG_ZERO_LATENCY_IRQS.
Questions answered
Can an ISR interrupt a cooperative thread?
Yes: an interrupt service routine (ISR), the function that the processor starts when an interrupt arrives, can interrupt a cooperative thread at any instruction, whenever that interrupt is enabled and not masked. Cooperative scheduling governs only switches between threads: a cooperative thread is not replaced by another thread until it waits or yields (apart from Zephyr’s special meta-IRQ threads, which are reserved for urgent work handed over by interrupts). Interrupts are handled by the processor beneath the thread scheduler, and in Zephyr every enabled interrupt ranks above every thread.
After the ISR, Zephyr returns to the interrupted cooperative thread, even if the ISR has made a higher-priority thread ready; that thread runs only when the cooperative thread waits or yields. Consequently, data that a cooperative thread shares with an ISR still needs protection, such as briefly masking interrupts with irq_lock() or using atomic operations.
Related entries: Interrupt (§6.11), Starvation (§9.18), Schedulability (§9.37), Interrupt service routine (ISR) (§6.12), NVIC (§6.13), Critical section (§9.4), Priority inversion (§9.16), Zephyr thread (§6.35), Watchdog timer (§6.16). Further reading: [14, 176].