Scoping cooperative scheduler implementation
The plan is to implement a cooperative scheduler in C maintaining linked tasks with separate stacks, using inline assembly to save and restore callee-saved registers plus stack pointers, switching between task contexts manually. The approach targets architectures like ARM Cortex-M or x86_64 by preparing initial stacks with trampoline frames calling task functions and a yield function saving current context and restoring the next task context from their saved stack.Providing minimal ARM Cortex-M cooperative scheduler example
A practical minimal example for ARM Cortex-M shows tasks each with their own allocated stack, a task control block holding stack pointer and function pointer, and a scheduler that switches via a yield function implemented in assembly. The yield saves callee-saved registers and stack pointer of the current task, switches current to the next task in the list, restores that task’s stack pointer and callee-saved registers, and returns to resume that task’s execution. Initial stacks are prepared so returning from yield starts the task’s function via a small trampoline. This code illustrates the essential mechanism of CPU context saving/restoring with custom stacks for cooperative multitasking.
What you need for a cooperative “green‑thread” scheduler are three pieces of state for each thread:
- where to resume execution (
PC/LR),
- a place to save the callee–saved registers,
- a private stack pointer.
A simple task control block that captures that looks like this:
#include <stdint.h>
typedef void (*task_fn_t)(void);
typedef struct task {
uint32_t *sp; /* saved stack pointer */
struct task *next; /* next task in round‑robin list */
task_fn_t entry; /* entry point for this task */
uint32_t stack[256]; /* 256 words of stack for this task */
} task_t;
static task_t *current;
static task_t *task_list;
When you create a task you allocate a stack and prime it with an initial frame such that “restoring” the context will start executing your task function. On ARM Cortex‑M, for example, the pop {r4–r11,lr} idiom restores the callee‑saved registers and then branches to lr, so you can seed lr with a small trampoline that calls the user function:
/* A small trampoline that runs on the new task's stack. */
static void task_trampoline(void) {
current->entry();
/* If the task function ever returns, just yield forever. */
for (;;)
;
}
/* Create a new task and add it to the round‑robin list. */
void task_create(task_t *t, task_fn_t fn)
{
/* Stack grows down on ARM. Start at top of allocated area. */
uint32_t *sp = t->stack + sizeof(t->stack) / sizeof(t->stack[0]);
/* Reserve space for r4–r11 and lr, fill lr with trampoline. */
sp -= 9;
sp[8] = (uint32_t)task_trampoline; /* will be popped into lr */
t->sp = sp;
t->entry = fn;
/* Insert at head of singly linked list. */
t->next = task_list;
task_list = t;
}
The yield function saves the current task’s callee‑saved registers and stack pointer into its TCB, advances current to the next task, loads that task’s saved stack pointer and restores its callee‑saved registers. The body has to be written in assembly so the compiler doesn’t spill registers you aren’t saving; GCC’s naked attribute lets you provide the whole prologue yourself:
__attribute__((naked)) void yield(void)
{
__asm volatile(
/* Save callee‑saved registers and lr onto the current stack. */
"push {r4-r11, lr} \n"
/* Save the current sp into current->sp. */
"ldr r0, =current \n"
"ldr r1, [r0] \n"
"str sp, [r1] \n"
/* Advance current = current->next or wrap to head. */
"ldr r2, [r1, #4] \n" /* next is at offset 4 */
"cmp r2, #0 \n"
"bne 1f \n"
"ldr r2, =task_list \n"
"ldr r2, [r2] \n"
"1: \n"
"str r2, [r0] \n"
/* Load next task's sp into sp. */
"ldr r3, [r2] \n"
"mov sp, r3 \n"
/* Restore that task's callee‑saved registers and lr, branch. */
"pop {r4-r11, lr} \n"
"bx lr \n"
);
}
Finally, start the scheduler by “switching” into the first task:
void scheduler_start(void)
{
current = task_list;
/* Load the first task's stack pointer and pop into registers. */
__asm volatile(
"ldr r0, =current \n"
"ldr r0, [r0] \n"
"ldr sp, [r0] \n"
"pop {r4-r11, lr} \n"
"bx lr \n"
);
}
With that in place you can define tasks as plain functions that call yield() to give up the CPU:
void task1(void) {
for (;;) {
/* do something */
yield();
}
}
void task2(void) {
for (;;) {
/* do something else */
yield();
}
}
int main(void)
{
static task_t t1, t2;
task_create(&t1, task1);
task_create(&t2, task2);
scheduler_start();
for (;;) ; /* never reached */
}
This is a minimal cooperative scheduler: tasks run until they voluntarily call yield(). It maintains its own linked list of tasks, each with a private stack. The context switch code shown is for a Cortex‑M target; on x86_64 or another architecture you would adjust the register save/restore sequence accordingly.