Add a function to schedule a thread

This commit is contained in:
pjht 2020-07-29 11:31:31 -05:00
parent d4fa7974b7
commit 6d418b242f

View File

@ -59,6 +59,30 @@ static void unmark_proc_scheduled(pid_t index) {
proc_schedule_bmap[byte]=proc_schedule_bmap[byte]&(~(1<<bit)); proc_schedule_bmap[byte]=proc_schedule_bmap[byte]&(~(1<<bit));
} }
/**
* Schedules a thread if the thread's prcess does not have a scheduled thread
* \param thread The thread to schedule
*/
void schedule_thread(Thread* thread) {
if(!is_proc_scheduled(thread->process->pid)) {
if (ready_to_run_tail) {
thread->state=THREAD_READY;
ready_to_run_tail->next_ready_to_run=thread;
thread->prev_ready_to_run=ready_to_run_tail;
ready_to_run_tail=thread;
mark_proc_scheduled(thread->process->pid);
} else if (current_thread) {
thread->state=THREAD_READY;
ready_to_run_head=thread;
ready_to_run_tail=thread;
mark_proc_scheduled(thread->process->pid);
} else {
thread->state=THREAD_RUNNING;
current_thread=thread;
}
}
}
void tasking_create_task(void* eip,void* address_space,char kmode,void* param1,void* param2,char isThread) { void tasking_create_task(void* eip,void* address_space,char kmode,void* param1,void* param2,char isThread) {
if (next_pid>MAX_PROCS && !isThread) { if (next_pid>MAX_PROCS && !isThread) {
serial_printf("Failed to create a process, as 32k processes have been created already.\n"); serial_printf("Failed to create a process, as 32k processes have been created already.\n");
@ -100,24 +124,8 @@ void tasking_create_task(void* eip,void* address_space,char kmode,void* param1,v
} else { } else {
thread->next_thread_in_process=NULL; thread->next_thread_in_process=NULL;
thread->prev_thread_in_process=NULL; thread->prev_thread_in_process=NULL;
if (!is_proc_scheduled(proc->pid)) { schedule_thread(thread);
if (ready_to_run_tail) {
thread->state=THREAD_READY;
ready_to_run_tail->next_ready_to_run=thread;
thread->prev_ready_to_run=ready_to_run_tail;
ready_to_run_tail=thread;
mark_proc_scheduled(proc->pid);
} else if (current_thread) {
thread->state=THREAD_READY;
ready_to_run_head=thread;
ready_to_run_tail=thread;
mark_proc_scheduled(proc->pid);
} else {
thread->state=THREAD_RUNNING;
current_thread=thread;
} }
}
}
if (!isThread) { if (!isThread) {
num_procs++; num_procs++;
} }
@ -195,18 +203,9 @@ void switch_to_thread(Thread* thread) {
} }
} }
} }
if (current_thread_next_ready && !is_proc_scheduled(current_thread->process->pid)) { if (current_thread_next_ready) {
// Link the thread onto the list of ready to run threads schedule_thread(current_thread_next_ready);
if (ready_to_run_tail) {
current_thread_next_ready->prev_ready_to_run=ready_to_run_tail;
ready_to_run_tail->next_ready_to_run=current_thread_next_ready;
ready_to_run_tail=current_thread_next_ready;
} else {
ready_to_run_head=current_thread_next_ready;
ready_to_run_tail=current_thread_next_ready;
} }
mark_proc_scheduled(current_thread->process->pid);
}
serial_printf("Switching to PID %d TID %d.\n",thread->process->pid,thread->tid); serial_printf("Switching to PID %d TID %d.\n",thread->process->pid,thread->tid);
switch_to_thread_asm(thread); switch_to_thread_asm(thread);
} }
@ -291,18 +290,7 @@ void tasking_unblock(pid_t pid,pid_t tid) {
return; return;
} }
thread->state=THREAD_READY; thread->state=THREAD_READY;
if (!is_proc_scheduled(thread->process->pid)) { schedule_thread(thread);
// Link the thread onto the list of ready to run threads
if (ready_to_run_tail) {
thread->prev_ready_to_run=ready_to_run_tail;
ready_to_run_tail->next_thread_in_process=thread;
ready_to_run_tail=thread;
} else {
ready_to_run_head=thread;
ready_to_run_tail=thread;
}
mark_proc_scheduled(thread->process->pid);
}
} }
void tasking_exit(int code) { void tasking_exit(int code) {