From 6f4a47afb9d6ca1872b52ec5ebaa0351d3549c71 Mon Sep 17 00:00:00 2001 From: Zihao Yu Date: Tue, 3 Oct 2023 15:33:38 +0800 Subject: [PATCH] add yield-os --- kernels/yield-os/Makefile | 3 +++ kernels/yield-os/yield-os.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 kernels/yield-os/Makefile create mode 100644 kernels/yield-os/yield-os.c diff --git a/kernels/yield-os/Makefile b/kernels/yield-os/Makefile new file mode 100644 index 0000000..ba8c264 --- /dev/null +++ b/kernels/yield-os/Makefile @@ -0,0 +1,3 @@ +NAME = yield-os +SRCS = yield-os.c +include $(AM_HOME)/Makefile diff --git a/kernels/yield-os/yield-os.c b/kernels/yield-os/yield-os.c new file mode 100644 index 0000000..90eb3ff --- /dev/null +++ b/kernels/yield-os/yield-os.c @@ -0,0 +1,31 @@ +#include +#include + +#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!"); +}