am-kernels/kernels/yield-os/yield-os.c

36 lines
798 B
C
Raw Permalink Normal View History

2023-10-03 07:33:38 +00:00
#include <am.h>
#include <klib-macros.h>
#define STACK_SIZE (4096 * 8)
typedef union {
uint8_t stack[STACK_SIZE];
struct {
Context *cp;
};
2023-10-03 07:33:38 +00:00
} PCB;
static PCB pcb[2], pcb_boot, *current = &pcb_boot;
static void f(void *arg) {
for (int i = 0; i < 100; i++) {
2023-10-03 07:33:38 +00:00
putch("?AB"[(uintptr_t)arg > 2 ? 0 : (uintptr_t)arg]);
for (int volatile i = 0; i < 100000; i++)
;
2023-10-03 07:33:38 +00:00
yield();
}
halt(0);
2023-10-03 07:33:38 +00:00
}
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);
2023-10-03 07:33:38 +00:00
yield();
panic("Should not reach here!");
}