scheduler: pid allocation, thread queueing function and a few fixes

This commit is contained in:
Matheus
2022-09-19 18:41:25 -03:00
parent ce217ea8d8
commit 2ab048c322
2 changed files with 38 additions and 9 deletions
+3
View File
@@ -40,6 +40,7 @@ typedef struct _thread_t{
} thread_t; } thread_t;
typedef struct _proc_t{ typedef struct _proc_t{
int lock;
struct _proc_t* parent; struct _proc_t* parent;
struct _proc_t* sibling; struct _proc_t* sibling;
struct _proc_t* child; struct _proc_t* child;
@@ -60,7 +61,9 @@ typedef struct{
int lock; int lock;
} sched_queue; } sched_queue;
thread_t* sched_newuthread(void* ip, size_t stacksize, void* stack, proc_t* proc, bool run, int prio);
thread_t* sched_newkthread(void* ip, size_t stacksize, bool run, int prio); thread_t* sched_newkthread(void* ip, size_t stacksize, bool run, int prio);
void sched_queuethread(thread_t* thread);
void sched_init(); void sched_init();
void sched_runinit(); void sched_runinit();
void sched_eventsignal(event_t* event, thread_t* thread); void sched_eventsignal(event_t* event, thread_t* thread);
+34 -8
View File
@@ -20,6 +20,11 @@
sched_queue queues[QUEUE_COUNT]; sched_queue queues[QUEUE_COUNT];
// XXX allocate these in a better way
static int pidlock;
static pid_t nextpid = 1;
static proc_t* allocproc(size_t threadcount){ static proc_t* allocproc(size_t threadcount){
proc_t* proc = alloc(sizeof(proc_t)); proc_t* proc = alloc(sizeof(proc_t));
@@ -39,6 +44,10 @@ static proc_t* allocproc(size_t threadcount){
return NULL; return NULL;
} }
spinlock_acquire(&pidlock);
proc->pid = nextpid++;
spinlock_release(&pidlock);
return proc; return proc;
} }
@@ -46,6 +55,7 @@ static proc_t* allocproc(size_t threadcount){
static thread_t* allocthread(proc_t* proc, state_t state, pid_t tid, size_t kstacksize){ static thread_t* allocthread(proc_t* proc, state_t state, pid_t tid, size_t kstacksize){
thread_t* thread = alloc(sizeof(thread_t)); thread_t* thread = alloc(sizeof(thread_t));
if(!thread) if(!thread)
return NULL; return NULL;
@@ -152,10 +162,22 @@ static thread_t* getnext(){
} }
int aa = 0; void sched_queuethread(thread_t* thread){
sched_queue* queue = &queues[thread->priority];
arch_interrupt_disable();
spinlock_acquire(&queue->lock);
queue_add(queue, thread);
spinlock_release(&queue->lock);
arch_interrupt_enable();
}
void sched_timerhook(arch_regs* regs){ void sched_timerhook(arch_regs* regs){
#ifdef __X86_64__ #ifdef __X86_64__
apic_eoi(); apic_eoi();
@@ -177,12 +199,17 @@ void sched_timerhook(arch_regs* regs){
thread_t* next = getnext(); thread_t* next = getnext();
if(next)
memcpy(regs, next->regs, sizeof(arch_regs));
arch_getcls()->thread = next; arch_getcls()->thread = next;
if(next){
memcpy(regs, next->regs, sizeof(arch_regs));
}
if(next->ctx != current->ctx)
vmm_switchcontext(next->ctx);
arch_setkernelstack(next->kernelstack); arch_setkernelstack(next->kernelstack);
arch_regs_setupextra(&next->extraregs); arch_regs_setupextra(&next->extraregs);
@@ -201,8 +228,7 @@ void switch_thread(thread_t* thread){
arch_getcls()->thread = thread; arch_getcls()->thread = thread;
if(thread->ctx != current->ctx)
if(current->ctx != thread->ctx)
vmm_switchcontext(thread->ctx); vmm_switchcontext(thread->ctx);
arch_setkernelstack(thread->kernelstack); arch_setkernelstack(thread->kernelstack);
@@ -247,6 +273,7 @@ thread_t* sched_newuthread(void* ip, size_t kstacksize, void* stack, proc_t* pro
spinlock_release(&queues[prio].lock); spinlock_release(&queues[prio].lock);
arch_interrupt_enable(); arch_interrupt_enable();
} }
return thread; return thread;
} }
@@ -354,7 +381,6 @@ void sched_runinit(){
proc->root = vfs_root(); proc->root = vfs_root();
proc->cwd = vfs_root(); proc->cwd = vfs_root();
proc->pid = 1;
int tmp; int tmp;