more stuff

This commit is contained in:
2026-09-19 21:57:06 +02:00
parent 1e9a1ebf4d
commit 831308b2ad
61 changed files with 777 additions and 837 deletions
+15 -56
View File
@@ -1,5 +1,6 @@
#include <zinnia/alloc.h>
#include <zinnia/assert.h>
#include <zinnia/atomic.h>
#include <zinnia/errno.h>
#include <zinnia/irq.h>
#include <zinnia/list.h>
@@ -10,7 +11,7 @@
#include <zinnia/spin.h>
#include <zinnia/tailq.h>
#include <zinnia/vma.h>
#include <stdatomic.h>
#include <arch/sched.h>
#include <stdint.h>
#include <string.h>
@@ -34,7 +35,7 @@ void sched_to_user_context(uintptr_t ctx) {
arch_sched_jump_to_context((struct arch_context*)ctx);
}
void sched_init(struct sched_percpu* sched) {
void sched_init(struct scheduler* sched) {
struct task* idle_task;
assert(task_create("idle", &kernel_vma, idle_func, 0, &idle_task) == 0, "Unable to create idle task!\n");
sched->idle_task = idle_task;
@@ -42,15 +43,15 @@ void sched_init(struct sched_percpu* sched) {
tailq_init(&sched->run_queue);
log("sched", "Scheduler initialized\n");
log("sched: Scheduler initialized\n");
}
void sched_add_task(struct sched_percpu* sched, struct task* task) {
void sched_add_task(struct scheduler* sched, struct task* task) {
assert(task != nullptr, "Tried to add NULL to the run queue\n");
tailq_insert_tail(&sched->run_queue, task, next);
}
static struct task* next_task(struct sched_percpu* sched) {
static struct task* next_task(struct scheduler* sched) {
struct task* t = tailq_first(&sched->run_queue);
if (t == nullptr)
@@ -61,17 +62,17 @@ static struct task* next_task(struct sched_percpu* sched) {
return t;
}
static void do_reschedule(struct sched_percpu* sched) {
struct task* from = atomic_load_explicit(&sched->current, memory_order_acquire);
static void do_reschedule(struct scheduler* sched) {
struct task* from = sched->current;
struct task* to = next_task(sched);
// We'd end up on the same task, skip switching altogether.
if (from == to) {
irq_unlock();
return;
}
atomic_store_explicit(&sched->current, to, memory_order_relaxed);
sched->current = to;
pmap_set(&to->space->pmap);
struct percpu* cpu = percpu_get();
@@ -80,17 +81,17 @@ static void do_reschedule(struct sched_percpu* sched) {
cpu->kernel_stack = to->kernel_stack;
cpu->user_stack = to->user_stack;
arch_sched_switch(from, to);
struct task* old = arch_sched_switch(from, to);
}
void sched_reschedule(struct sched_percpu* sched) {
void sched_reschedule(struct scheduler* sched) {
irq_lock();
if (sched->current->state != TASK_STATE_DEAD)
sched_add_task(sched, sched->current);
do_reschedule(sched);
}
void sched_yield(struct sched_percpu* sched) {
void sched_yield(struct scheduler* sched) {
irq_lock();
do_reschedule(sched);
}
@@ -104,7 +105,7 @@ errno_t task_create(const char* name, struct vma* space, task_fn_t entry, uintpt
return ENOMEM;
static size_t last_tid = 0;
new_task->id = atomic_fetch_add(&last_tid, 1);
new_task->id = atomic_fetch_add(&last_tid, 1, ATOMIC_ACQUIRE);
new_task->space = space;
strncpy(new_task->name, name, sizeof(new_task->name));
@@ -126,50 +127,8 @@ errno_t task_create(const char* name, struct vma* space, task_fn_t entry, uintpt
void task_entry(task_fn_t entry, uintptr_t arg0) {
entry(arg0);
struct sched_percpu* sched = &percpu_get()->sched;
struct scheduler* sched = &percpu_get()->sched;
sched->current->state = TASK_STATE_DEAD;
sched_yield(sched);
panic("unreachable");
}
void wait_queue_init(struct wait_queue* wq) {
tailq_init(&wq->waiters);
wq->lock = (spinlock_t){0};
}
void sched_block(struct sched_percpu* sched, struct wait_queue* wq) {
irq_lock();
spin_lock(&wq->lock);
sched->current->state = TASK_STATE_BLOCKED;
tailq_insert_tail(&wq->waiters, sched->current, next);
spin_unlock(&wq->lock);
do_reschedule(sched);
}
void sched_wake(struct sched_percpu* sched, struct wait_queue* wq) {
spin_lock(&wq->lock);
struct task* t = tailq_first(&wq->waiters);
if (t != nullptr) {
tailq_remove(&wq->waiters, t, next);
t->state = TASK_STATE_READY;
sched_add_task(sched, t);
}
spin_unlock(&wq->lock);
}
void sched_wake_all(struct sched_percpu* sched, struct wait_queue* wq) {
spin_lock(&wq->lock);
struct task* t;
while ((t = tailq_first(&wq->waiters)) != nullptr) {
tailq_remove(&wq->waiters, t, next);
t->state = TASK_STATE_READY;
sched_add_task(sched, t);
}
spin_unlock(&wq->lock);
}