am-kernels/kernels/yield-os/yield-os.c
xinyangli 8630fe7667
All checks were successful
Build nix packages / build-matrix (am-kernels) (push) Successful in 2m26s
Build nix packages / build-matrix (rv32Cross.am-kernels-nemu) (push) Successful in 2m23s
Build nix packages / build-matrix (rv32Cross.am-kernels-npc) (push) Successful in 2m21s
refactor: use cmake macros to do objcopy, install and tests
2024-08-15 17:52:04 +08:00

35 lines
798 B
C

#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) {
for (int i = 0; i < 100; i++) {
putch("?AB"[(uintptr_t)arg > 2 ? 0 : (uintptr_t)arg]);
for (int volatile i = 0; i < 100000; i++)
;
yield();
}
halt(0);
}
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!");
}