Minor changes to build system
This commit is contained in:
parent
f478f8d38b
commit
a7fd9ac224
16 changed files with 391 additions and 224 deletions
|
|
@ -39,46 +39,31 @@ void thread_exit(){
|
|||
cpu_state *cpu = get_cpu_struct();
|
||||
struct thread *p = cpu->current_process;
|
||||
|
||||
kprintf("hi");
|
||||
|
||||
// Remove process from circular linked list
|
||||
p->prev->next = p->next;
|
||||
|
||||
if(p == p->next){
|
||||
cpu->current_process = idle;
|
||||
}
|
||||
// If this is the only thread in the queue then set cpu->head to NULL, so scheduler knows to idle
|
||||
cpu->head = NULL;
|
||||
}else{
|
||||
// Remove process from circular linked list
|
||||
p->next->prev = p->prev;
|
||||
p->prev->next = p->next;
|
||||
|
||||
if(p == cpu->head){
|
||||
if(p == cpu->head){
|
||||
//cpu->head = p->next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ma_cache_dealloc(p); // KILL the thread
|
||||
p->state = ZOMBIE;
|
||||
|
||||
sched();
|
||||
// Switch to scheduler
|
||||
yield();
|
||||
}
|
||||
|
||||
/* Setup a process structure */
|
||||
struct thread *alloc_thread(void){
|
||||
struct cpu_state *cpu = get_cpu_struct();
|
||||
struct thread *head = get_cpu_struct()->head;
|
||||
struct thread *base = get_cpu_struct()->base;
|
||||
|
||||
struct thread *t = ma_cache_alloc(thread_cache, 0);
|
||||
memset(t, 0, sizeof(struct thread));
|
||||
|
||||
if(base == NULL){
|
||||
t->next = t;
|
||||
t->prev = t;
|
||||
cpu->base = t;
|
||||
cpu->head = t;
|
||||
} else {
|
||||
t->prev = head;
|
||||
t->next = base;
|
||||
head->next = t;
|
||||
base->prev = t;
|
||||
cpu->head = t;
|
||||
}
|
||||
|
||||
t->state = READY;
|
||||
|
||||
t->kstack = kzalloc(8 * 4096);
|
||||
|
|
@ -100,36 +85,61 @@ struct thread *alloc_thread(void){
|
|||
|
||||
}
|
||||
|
||||
struct thread *add_process(uint64_t *entry){
|
||||
struct thread *proc = alloc_thread();
|
||||
struct thread *add_thread(uint64_t *entry){
|
||||
struct thread *t = alloc_thread();
|
||||
|
||||
if (proc == NULL) {
|
||||
klog(__func__, "proc == null!");
|
||||
kkill();
|
||||
assert(t != NULL && "Thread allocation failed!");
|
||||
|
||||
struct cpu_state *cpu = get_cpu_struct();
|
||||
struct thread *head = get_cpu_struct()->head;
|
||||
struct thread *base = get_cpu_struct()->base;
|
||||
|
||||
// Manage circley linked list
|
||||
if(base == NULL){
|
||||
t->next = t;
|
||||
t->prev = t;
|
||||
cpu->base = t;
|
||||
cpu->head = t;
|
||||
} else {
|
||||
t->prev = head;
|
||||
t->next = base;
|
||||
head->next = t;
|
||||
base->prev = t;
|
||||
cpu->head = t;
|
||||
}
|
||||
|
||||
proc->context->rip = (uint64_t)entry;
|
||||
t->context->rip = (uint64_t)entry;
|
||||
|
||||
//kprintf("entry: 0x{xn}", entry);
|
||||
|
||||
return proc;
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
[[noreturn]] void sched(){
|
||||
cpu_state *cpu = get_cpu_struct();
|
||||
for(;;){
|
||||
asm("cli"); // we sti at the end of switch_context
|
||||
cpu_state *cpu = get_cpu_struct();
|
||||
struct thread *prev = cpu->current_process;
|
||||
|
||||
asm("cli");
|
||||
if(prev->state == ZOMBIE){
|
||||
kprintf("we are freeing allocated thread 0x{x}\n", prev);
|
||||
ma_cache_dealloc(prev);
|
||||
}else{
|
||||
prev->state = READY;
|
||||
}
|
||||
|
||||
struct thread *prev = cpu->current_process;
|
||||
prev->state = READY;
|
||||
|
||||
cpu->current_process = prev->next;
|
||||
|
||||
cpu->current_process->state = RUNNING;
|
||||
asm("sti");
|
||||
switch_context(&(get_cpu_struct()->scheduler_context), get_cpu_struct()->current_process->context);
|
||||
|
||||
if(cpu->head == NULL){
|
||||
cpu->current_process = idle;
|
||||
}else{
|
||||
if(prev->next){
|
||||
cpu->current_process = prev->next;
|
||||
}else{
|
||||
cpu->current_process = cpu->head;
|
||||
}
|
||||
}
|
||||
|
||||
cpu->current_process->state = RUNNING;
|
||||
switch_context(&(get_cpu_struct()->scheduler_context), get_cpu_struct()->current_process->context);
|
||||
}
|
||||
}
|
||||
|
||||
void scheduler_init(){
|
||||
|
|
@ -142,11 +152,11 @@ void scheduler_init(){
|
|||
return;
|
||||
}
|
||||
|
||||
idle = add_process((uint64_t*)idle_task);
|
||||
idle = alloc_thread();
|
||||
idle->context->rip = (uint64_t)idle;
|
||||
|
||||
assert(idle != NULL && "Failed to allocate idle task!");
|
||||
|
||||
|
||||
cpu->current_process = idle;
|
||||
|
||||
cpu->scheduler_stack = kzalloc(4096);
|
||||
|
|
@ -154,9 +164,9 @@ void scheduler_init(){
|
|||
cpu->scheduler_context->rbp = (uint64_t)cpu->scheduler_context;
|
||||
cpu->scheduler_context->rip = (uint64_t)sched;
|
||||
|
||||
add_process((uint64_t*)test_task);
|
||||
add_thread((uint64_t*)test_task);
|
||||
|
||||
add_process((uint64_t *)best_task);
|
||||
add_thread((uint64_t *)best_task);
|
||||
|
||||
// Initialize scheduler -> we will now get timer interrupts to switch us into sched()
|
||||
cpu->scheduler_initialized = true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue