heading to laptop

This commit is contained in:
2026-07-07 18:52:22 -05:00
parent 177a0b8fe9
commit e42ee8eb85
13 changed files with 68 additions and 96 deletions

View File

@@ -17,34 +17,20 @@ static process_t* idle_task = NULL;
static int total_processes = 0;
static void lock_scheduler() {
void lock_scheduler() {
disable_interrupts();
}
static void unlock_scheduler() {
void unlock_scheduler() {
enable_interrupts();
}
uint32_t switch_context(uint32_t current_esp) {
current_process->esp = current_esp;
for (int i = 0; i < total_processes; i++) {
if (process_table[i].state == STATE_SLEEPING) {
if (process_table[i].sleep > 0) {
process_table[i].sleep--;
}
if (process_table[i].sleep == 0) {
process_table[i].state = STATE_READY;
}
}
}
process_t* next_process = current_process;
process_t* next_process = current_process->sched_next;
int found_ready = 0;
while (1) {
next_process = (process_t*)next_process->sched_next;
while (next_process != NULL) {
if (next_process != idle_task && next_process->state == STATE_READY) {
found_ready = 1;
break;
@@ -53,10 +39,12 @@ uint32_t switch_context(uint32_t current_esp) {
if (next_process == current_process) {
break;
}
next_process = (process_t*)next_process->sched_next;
}
if (!found_ready) {
if (current_process->state == STATE_READY) {
if (current_process && current_process->state == STATE_READY) {
next_process = current_process;
} else {
next_process = idle_task;
@@ -68,6 +56,7 @@ uint32_t switch_context(uint32_t current_esp) {
uint32_t current_cr3 = fetch_cr3();
if (current_cr3 != current_process->cr3) {
load_pd((void*)current_process->cr3);
flush_tlb();
}
if (current_process->flags & PROCESS_FLAG_USER) {
@@ -145,25 +134,19 @@ void init_task (
}
int get_next_pid() {
lock_scheduler();
int pid = total_processes;
total_processes++;
unlock_scheduler();
return pid;
}
void add_task(process_t* task) {
lock_scheduler();
if (!task) {
unlock_scheduler();
return;
}
if (process_table->sched_next == NULL) {
process_table->sched_next = task;
task->sched_next = process_table;
unlock_scheduler();
return;
}
@@ -174,37 +157,27 @@ void add_task(process_t* task) {
curr->sched_next = task;
task->sched_next = process_table;
unlock_scheduler();
}
process_t* current_task() {
lock_scheduler();
process_t* rtn = current_process;
unlock_scheduler();
return rtn;
}
void sleep(uint32_t ms) {
if (ms == 0) return;
lock_scheduler();
current_process->sleep = ms;
current_process->state = STATE_SLEEPING;
yield();
unlock_scheduler();
}
void exit() {
lock_scheduler();
current_process->state = STATE_DEAD;
yield(); // yield forever
unlock_scheduler();
}
void wake(process_t* task) {
lock_scheduler();
if (task->state == STATE_SLEEPING || task->state == STATE_BLOCKED) {
task->state = STATE_READY;
}
unlock_scheduler();
}