From db684b8653d93f4a374d8d692bb0afb31db67987 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Thu, 28 Mar 2024 22:00:16 -0500 Subject: kernel: add a basic thread scheduler Add a basic thread scheduler. This should allow the kernel to schedule threads according to the round robin algorithm. Signed-off-by: Danny Holman --- kernel/sched.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 kernel/sched.c (limited to 'kernel') diff --git a/kernel/sched.c b/kernel/sched.c new file mode 100644 index 0000000..8aab675 --- /dev/null +++ b/kernel/sched.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include + +static struct list_head *ready_queue; +static struct list_head *wait_queue; + +static struct task_block *cur = NULL; +static long int next_id = 1; + +static void _enqueue(struct list_head *queue, struct task_block *task) { + struct list_head *temp = queue; + while (temp->next != NULL) + temp = temp->next; + list_add(&task->list, temp); +} + +static struct task_block* _dequeue(struct list_head *queue) { + if (queue->next == NULL) + return NULL; + struct list_head *temp = queue->next; + list_del(temp); + struct task_block *ret = container_of(temp, struct task_block, list); + return ret; +} + +void sched_init(void) { + ready_queue = kmalloc(sizeof(struct list_head)); + wait_queue = kmalloc(sizeof(struct list_head)); + struct task_block *boot_task = init_task(0, 0); + boot_task->threads->state = THREAD_RUNNING; + cur = boot_task; + + switch_thread(boot_task->threads, boot_task->threads); +} + +void schedule_next(void) { + struct task_block *task = _dequeue(ready_queue); + if (task == NULL) + return; + if (cur != NULL) + _enqueue(ready_queue, cur); + disable_ints(); + switch_thread(cur->threads, task->threads); + enable_ints(); + task->threads->state = THREAD_RUNNING; + cur->threads->state = THREAD_READY; + cur = task; +} + +struct task_block* init_task(int nice, unsigned int ppid) { + struct task_block *ret = kmalloc(sizeof(struct task_block)); + ret->pid = next_id++; + ret->parent_pid = ppid; + ret->nice = nice; + ret->num_threads = 1; + ret->threads = kmalloc(sizeof(struct thread_block)); + ret->threads->tid = ret->pid; + ret->threads->tgid = ret->pid; + return ret; +} -- cgit v1.2.3