vbos/kernel/proc/sched.c
Michael Shaw b926d50365 fork: properly set up child process
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-22 00:06:57 -04:00

83 lines
1.5 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <arch/proc.h>
#include <arch/page.h>
#include <kernel/proc.h>
#include <kernel/sched.h>
/* Run queue */
static struct {
struct thread *head;
struct thread *tail;
spinlock_t lock;
} sched_queue;
void sched_add_thread(struct thread *t)
{
spinlock_acquire(&sched_queue.lock);
t->next = NULL;
if (sched_queue.tail)
sched_queue.tail->next = t;
sched_queue.tail = t;
if (!sched_queue.head)
sched_queue.head = t;
spinlock_release(&sched_queue.lock);
}
void sched_purge_proc(struct proc *proc)
{
spinlock_acquire(&sched_queue.lock);
struct thread *t = sched_queue.head;
struct thread *p = NULL;
while (t) {
if (t->proc == proc) {
if (p)
p->next = t->next;
else
sched_queue.head = t->next;
if (!t->next)
sched_queue.tail = p;
t->next = NULL;
} else {
p = t;
}
t = t->next;
}
spinlock_release(&sched_queue.lock);
}
static struct thread *dequeue_thread()
{
spinlock_acquire(&sched_queue.lock);
struct thread *t = sched_queue.head;
if (t) {
sched_queue.head = t->next;
if (!sched_queue.head)
sched_queue.tail = NULL;
t->next = NULL;
}
spinlock_release(&sched_queue.lock);
return t;
}
void schedule()
{
struct thread *cur = get_cur_thread();
struct thread *next = dequeue_thread();
if (!next)
return;
struct thread *prev = cur;
if (cur)
sched_add_thread(cur);
struct cpu_local *tls = tls_get();
tls->cur_thread = next;
tls->kernel_rsp = next->kstack_top;
tls->tss.rsp0 = next->kstack_top;
pgd_load(next->proc->pgd);
switch_to(prev, next);
}