sched: allocate and assign ids to each thread

This commit is contained in:
2024-09-17 17:49:05 +01:00
parent d29b955ee8
commit ef05233dcf
2 changed files with 61 additions and 40 deletions

View File

@@ -1,10 +1,13 @@
#include <socks/sched.h>
#include <socks/object.h>
#include <socks/bitmap.h>
#include <socks/cpu.h>
#include <socks/machine/thread.h>
#include <socks/object.h>
#include <socks/sched.h>
#define THREAD_CAST(p) OBJECT_C_CAST(struct thread, thr_base, &thread_type, p)
static DECLARE_BITMAP(thread_ids, THREAD_MAX) = {0};
static struct object_type thread_type = {
.ob_name = "thread",
.ob_size = sizeof(struct thread),
@@ -30,6 +33,9 @@ struct thread *thread_alloc(void)
kern_status_t thread_init(struct thread *thr, uintptr_t ip)
{
thr->tr_id = bitmap_lowest_clear(thread_ids, THREAD_MAX);
bitmap_set(thread_ids, thr->tr_id);
thr->tr_prio = PRIO_NORMAL;
thr->tr_state = THREAD_READY;
thr->tr_quantum_target = default_quantum();
@@ -39,7 +45,8 @@ kern_status_t thread_init(struct thread *thr, uintptr_t ip)
return KERN_NO_MEMORY;
}
thr->tr_sp = (uintptr_t)vm_page_get_vaddr(thr->tr_kstack) + vm_page_order_to_bytes(THREAD_KSTACK_ORDER);
thr->tr_sp = (uintptr_t)vm_page_get_vaddr(thr->tr_kstack)
+ vm_page_order_to_bytes(THREAD_KSTACK_ORDER);
thr->tr_bp = thr->tr_sp;
prepare_stack(ip, &thr->tr_sp);
@@ -49,7 +56,6 @@ kern_status_t thread_init(struct thread *thr, uintptr_t ip)
void thread_free(struct thread *thr)
{
}
struct thread *current_thread(void)
@@ -74,11 +80,14 @@ int thread_priority(struct thread *thr)
return thr->tr_prio;
}
struct thread *create_kernel_thread(void(*fn)(void))
struct thread *create_kernel_thread(void (*fn)(void))
{
struct task *kernel = kernel_task();
struct thread *thr = thread_alloc();
thr->tr_id = 1;
bitmap_set(thread_ids, 1);
thr->tr_parent = kernel;
thr->tr_prio = PRIO_NORMAL;
thr->tr_state = THREAD_READY;
@@ -101,6 +110,9 @@ struct thread *create_idle_thread(void)
struct task *idle = idle_task();
struct thread *thr = thread_alloc();
thr->tr_id = 0;
bitmap_set(thread_ids, 0);
thr->tr_parent = idle;
thr->tr_prio = PRIO_NORMAL;
thr->tr_state = THREAD_READY;