1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
| #include "thread.h" #include "stdint.h" #include "string.h" #include "global.h" #include "memory.h" #include "debug.h" #include "interrupt.h" #include "print.h" #include "../userprog/process.h" #include "../thread/sync.h" #include "../userprog/fork.h"
#define PG_SIZE 4096
struct task_struct* main_thread; struct task_struct* idle_thread; struct list thread_ready_list; struct list thread_all_list; static struct list_elem* thread_tag; struct lock pid_lock;
struct task_struct* running_thread() { uint32_t esp; asm ("mov %%esp, %0" : "=g" (esp)); return (struct task_struct*)(esp & 0xfffff000); }
static void kernel_thread(thread_func* function, void* func_arg) {
intr_enable(); function(func_arg); }
void thread_create(struct task_struct* pthread, thread_func function, void* func_arg) { pthread->self_kstack -= sizeof(struct intr_stack);
pthread->self_kstack -= sizeof(struct thread_stack); struct thread_stack* kthread_stack = (struct thread_stack*)pthread->self_kstack; kthread_stack->eip = kernel_thread; kthread_stack->function = function; kthread_stack->func_arg = func_arg; kthread_stack->ebp = kthread_stack->ebx = kthread_stack->esi = kthread_stack->edi = 0; }
void init_thread(struct task_struct* pthread, char* name, int prio) { memset(pthread, 0, sizeof(*pthread)); pthread->pid = allocate_pid(); strcpy(pthread->name, name);
if (pthread == main_thread) { pthread->status = TASK_RUNNING; } else { pthread->status = TASK_READY; }
pthread->self_kstack = (uint32_t*)((uint32_t)pthread + PG_SIZE); pthread->priority = prio; pthread->ticks = prio; pthread->elapsed_ticks = 0; pthread->pgdir = NULL; pthread->cwd_inode_nr = 0; pthread->stack_magic = 0x19870916; pthread->parent_pid = -1; pthread->fd_table[0] = 0; pthread->fd_table[1] = 1; pthread->fd_table[2] = 2; uint8_t fd_idx = 3; while (fd_idx < MAX_FILES_OPEN_PER_PROC) { pthread->fd_table[fd_idx] = -1; fd_idx++; } }
struct task_struct* thread_start(char* name, int prio, thread_func function, void* func_arg) {
struct task_struct* thread = get_kernel_pages(1); init_thread(thread, name, prio); thread_create(thread, function, func_arg);
ASSERT(!elem_find(&thread_ready_list, &thread->general_tag)); list_append(&thread_ready_list, &thread->general_tag);
ASSERT(!elem_find(&thread_all_list, &thread->all_list_tag)); list_append(&thread_all_list, &thread->all_list_tag);
return thread; }
static void make_main_thread(void) {
main_thread = running_thread(); init_thread(main_thread, "main", 31);
ASSERT(!elem_find(&thread_all_list, &main_thread->all_list_tag)); list_append(&thread_all_list, &main_thread->all_list_tag); }
void schedule() { ASSERT(intr_get_status() == INTR_OFF);
struct task_struct* cur = running_thread(); if (cur->status == TASK_RUNNING) { ASSERT(!elem_find(&thread_ready_list, &cur->general_tag)); list_append(&thread_ready_list, &cur->general_tag); cur->ticks = cur->priority; cur->status = TASK_READY; } else {
}
if (list_empty(&thread_ready_list)) { thread_unblock(idle_thread); }
ASSERT(!list_empty(&thread_ready_list)); thread_tag = NULL; thread_tag = list_pop(&thread_ready_list); struct task_struct* next = elem2entry(struct task_struct, general_tag, thread_tag); next->status = TASK_RUNNING;
process_activate(next); switch_to(cur, next); }
void thread_init(void) { put_str("thread_init start\n");
list_init(&thread_ready_list); list_init(&thread_all_list); lock_init(&pid_lock); process_execute(init,"init"); make_main_thread(); idle_thread = thread_start("idle", 10, idle, NULL); put_str("thread_init done\n"); }
void thread_block(enum task_status stat) {
ASSERT(((stat == TASK_BLOCKED) || (stat == TASK_WAITING) || (stat == TASK_HANGING))); enum intr_status old_status = intr_disable(); struct task_struct* cur_thread = running_thread(); cur_thread->status = stat; schedule();
intr_set_status(old_status); }
void thread_unblock(struct task_struct* pthread) { enum intr_status old_status = intr_disable(); ASSERT(((pthread->status == TASK_BLOCKED) || (pthread->status == TASK_WAITING) || (pthread->status == TASK_HANGING))); if (pthread->status != TASK_READY) { ASSERT(!elem_find(&thread_ready_list, &pthread->general_tag)); if (elem_find(&thread_ready_list, &pthread->general_tag)) { PANIC("thread_unblock: blocked thread in ready_list\n"); } list_push(&thread_ready_list, &pthread->general_tag); pthread->status = TASK_READY; } intr_set_status(old_status); }
static pid_t allocate_pid(void) { static pid_t next_pid = 0; lock_acquire(&pid_lock); next_pid++; lock_release(&pid_lock); return next_pid; }
static void idle(void) { while(1) { thread_block(TASK_BLOCKED); asm volatile ("sti; hlt" : : : "memory"); } }
void thread_yield(void) { struct task_struct* cur = running_thread(); enum intr_status old_status = intr_disable(); ASSERT(!elem_find(&thread_ready_list, &cur->general_tag)); list_append(&thread_ready_list, &cur->general_tag); cur->status = TASK_READY; schedule(); intr_set_status(old_status); }
pid_t fork_pid(void) { return allocate_pid(); }
|