Files
mango/sched/thread.c

73 lines
1.4 KiB
C

#include <socks/sched.h>
#include <socks/object.h>
#include <socks/cpu.h>
#include <socks/machine/thread.h>
static struct object_type thread_type = {
.ob_name = "thread",
.ob_size = sizeof(struct thread),
};
kern_status_t thread_object_type_init(void)
{
return object_type_register(&thread_type);
}
struct thread *thread_alloc(void)
{
struct object *thread_obj = object_create(&thread_type);
if (!thread_obj) {
return NULL;
}
struct thread *t = object_data(thread_obj);
memset(t, 0x00, sizeof *t);
return t;
}
kern_status_t thread_init(struct thread *thr, uintptr_t ip)
{
thr->tr_prio = PRIO_NORMAL;
thr->tr_state = THREAD_READY;
thr->tr_quantum_target = default_quantum();
thr->tr_kstack = vm_page_alloc(THREAD_KSTACK_ORDER, VM_NORMAL);
if (!thr->tr_kstack) {
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_bp = thr->tr_sp;
prepare_stack(ip, &thr->tr_sp);
return KERN_OK;
}
void thread_free(struct thread *thr)
{
}
struct thread *current_thread(void)
{
struct cpu_data *cpu = get_this_cpu();
if (!cpu) {
return NULL;
}
struct thread *out = cpu->c_rq.rq_cur;
put_cpu(cpu);
return out;
}
bool need_resched(void)
{
return (current_thread()->tr_flags & THREAD_F_NEED_RESCHED) != 0;
}
int thread_priority(struct thread *thr)
{
return thr->tr_prio;
}