add yield-os

This commit is contained in:
Zihao Yu 2023-10-03 15:33:38 +08:00
parent 86c5532674
commit 6f4a47afb9
2 changed files with 34 additions and 0 deletions

View file

@ -0,0 +1,3 @@
NAME = yield-os
SRCS = yield-os.c
include $(AM_HOME)/Makefile

View file

@ -0,0 +1,31 @@
#include <am.h>
#include <klib-macros.h>
#define STACK_SIZE (4096 * 8)
typedef union {
uint8_t stack[STACK_SIZE];
struct { Context *cp; };
} PCB;
static PCB pcb[2], pcb_boot, *current = &pcb_boot;
static void f(void *arg) {
while (1) {
putch("?AB"[(uintptr_t)arg > 2 ? 0 : (uintptr_t)arg]);
for (int volatile i = 0; i < 100000; i++) ;
yield();
}
}
static Context *schedule(Event ev, Context *prev) {
current->cp = prev;
current = (current == &pcb[0] ? &pcb[1] : &pcb[0]);
return current->cp;
}
int main() {
cte_init(schedule);
pcb[0].cp = kcontext((Area) { pcb[0].stack, &pcb[0] + 1 }, f, (void *)1L);
pcb[1].cp = kcontext((Area) { pcb[1].stack, &pcb[1] + 1 }, f, (void *)2L);
yield();
panic("Should not reach here!");
}