Section 6.37

Cooperative and preemptive scheduling

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.

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.

  1. If the interrupted thread is cooperative, execution returns to it, even if the ISR has made a higher-priority thread ready.

  2. 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.

Worked example

One processor runs two threads. Thread A, priority −2-2 (cooperative), starts at 0 ms a computation that needs 20 ms of processor time and never waits. Thread B, priority −5-5 (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.

Now make A preemptive, with priority 3, and keep B at −5-5.

Both runs end at 21.05 ms, because the processor did the same 20+0.05+1=21.0520 + 0.05 + 1 = 21.05 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 AA, with priority pAp_A. On one processor, Zephyr switches from AA to another thread only at these events:

Separately, an ISR can begin between any two instructions of any thread whenever its interrupt is enabled and not masked.

Consequently, if AA is cooperative and BB is a ready thread that outranks every other ready thread, BB waits at most the longest stretch of AA’s execution between two points where AA waits or yields, plus the time spent in ISRs during that stretch. A response-time §9.32 bound for BB 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].