thread.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "thread.h"
  2. #include "sync.h"
  3. #include "../lib/string.h"
  4. #include "../kernel/interrupt.h"
  5. #include "../kernel/debug.h"
  6. #include "../lib/kernel/list.h"
  7. #include "../lib/kernel/print.h"
  8. #include "../userprog/process.h"
  9. struct task_struct *main_thread; // 主线程 PCB
  10. struct list thread_ready_list; // 就绪队列
  11. struct list thread_all_list; // 所有任务队列
  12. static struct list_elem *thread_tag; // 用于保存队列中的线程节点
  13. static pid_t allocate_pid(void); // 分配 pid
  14. struct lock pid_lock;
  15. extern void switch_to(struct task_struct *cur, struct task_struct *next);
  16. // 获取当前线程的 PCB 指针
  17. struct task_struct *running_thread(void)
  18. {
  19. uint32_t esp;
  20. asm("mov %%esp, %0" : "=g"(esp));
  21. // 取 esp 整数部分作为 PCB 的指针
  22. return (struct task_struct *)((uintptr_t)esp & 0xfffff000);
  23. }
  24. // 由 kernel_thread 去执行 function(func_arg)
  25. static void kernel_thread(thread_func *function, void *func_arg)
  26. {
  27. // 执行 function 前开中断,避免后面的时钟中断被屏蔽,而无法调度其它线程
  28. intr_enable();
  29. function(func_arg);
  30. }
  31. // 初始化线程栈 thread_stack
  32. void thread_create(struct task_struct *pthread, thread_func function, void *func_arg)
  33. {
  34. // 先预留中断栈空间
  35. pthread->self_kstack -= sizeof(struct intr_stack);
  36. // 再预留线程栈空间,线程栈位于中断栈顶
  37. pthread->self_kstack -= sizeof(struct thread_stack);
  38. struct thread_stack *kthread_stack = (struct thread_stack *)pthread->self_kstack;
  39. kthread_stack->eip = kernel_thread;
  40. kthread_stack->function = function;
  41. kthread_stack->func_arg = func_arg;
  42. kthread_stack->ebp = kthread_stack->ebx = kthread_stack->edi = kthread_stack->esi = 0;
  43. }
  44. // 分配 pid
  45. static pid_t allocate_pid(void)
  46. {
  47. static pid_t next_pid = 0;
  48. lock_acquire(&pid_lock);
  49. next_pid++;
  50. lock_release(&pid_lock);
  51. return next_pid;
  52. }
  53. // 初始化线程基本信息
  54. void init_thread(struct task_struct *pthread, char *name, int prio)
  55. {
  56. _memset(pthread, 0, sizeof(*pthread)); // 全部置为 0
  57. pthread->pid = allocate_pid(); // 分配 pid
  58. _strcpy(pthread->name, name); // name
  59. // 线程状态, 目前只有两种状态: TASK_RUNNING, TASK_READY
  60. if (pthread == main_thread)
  61. { // 主线程,把 main 也封装成一个线程,并且它一直是运行的,所以是 TASK_RUNNING
  62. pthread->status = TASK_RUNNING;
  63. }
  64. else
  65. {
  66. pthread->status = TASK_READY;
  67. }
  68. pthread->self_kstack = (uint32_t *)((uintptr_t)pthread + PG_SIZE); // self_kstack是线程自己在内核态下使用的栈顶地址
  69. pthread->priority = prio; // 线程优先级
  70. pthread->ticks = prio; // 嘀嗒数
  71. pthread->elapsed_ticks = 0; // 线程已执行的时间嘀嗒数
  72. pthread->pgdir = NULL; // 进程自己页表的虚拟地址, 如果是线程则为 NULL
  73. // pthread->userprog_addr = NULL;
  74. pthread->stack_magic = 0x19940625; // 自定义的魔数
  75. }
  76. // 创建优先级为 prio 的线程, 线程名为 name, 线程所执行的函数是 function(func_arg)
  77. struct task_struct *thread_start(char *name, int prio, thread_func function, void *func_arg)
  78. {
  79. // pcb 线程控制块
  80. struct task_struct *thread = get_kernel_pages(1); // 申请一页内存(内核空间)做为PCB
  81. init_thread(thread, name, prio); // 初始化线程基本信息
  82. thread_create(thread, function, func_arg); // 初始化线程栈 thread_stack
  83. ASSERT(!elem_find(&thread_ready_list, &thread->general_tag)); // 保证加入就绪队列的线程不在队列中
  84. list_append(&thread_ready_list, &thread->general_tag); // 加入就绪队列
  85. ASSERT(!elem_find(&thread_all_list, &thread->all_list_tag)); // 保证加入所有线程队列的线程不在队列中
  86. list_append(&thread_all_list, &thread->all_list_tag); // 加入所有线程队列
  87. return thread;
  88. }
  89. // 将 kernel 中的 main 函数封装成线程
  90. static void make_main_thread(void)
  91. {
  92. // 因为 main 函数也是一个线程,但它被单独执行,所以要单独处理
  93. // 因为 main 函数是操作系统的第一个函数,所以它的 PCB 也是第一个
  94. // ! 在 loader.S 中进入内核时 mov esp, 0xc009f000,
  95. // ! 所以 main 函数的栈顶是 0xc009f000, PCB 是 0xc009e000
  96. main_thread = running_thread();
  97. init_thread(main_thread, "main", 31); // 31 是最高优先级
  98. // main 函数是当前线程,当前线程不在 thread_ready_list 中
  99. ASSERT(!elem_find(&thread_all_list, &main_thread->all_list_tag));
  100. list_append(&thread_all_list, &main_thread->all_list_tag);
  101. }
  102. /// @brief 将当前线程换下处理器,并在就绪队列中找出下个可运行的程序,换上处理器
  103. ///! 此过程由 时钟中断 来调用
  104. void schedule(void)
  105. {
  106. ASSERT(intr_get_status() == INTR_OFF);
  107. struct task_struct *cur = running_thread();
  108. if (cur->status == TASK_RUNNING)
  109. {
  110. // 若此线程只是时间片到了,将其加入到就绪队列尾
  111. ASSERT(!elem_find(&thread_ready_list, &cur->general_tag));
  112. list_append(&thread_ready_list, &cur->general_tag);
  113. cur->ticks = cur->priority; // 重置 ticks
  114. cur->status = TASK_READY;
  115. }
  116. else
  117. {
  118. // 若此线程需要某事件发生后才能继续上 cpu 运行, 不需要加入队列
  119. }
  120. // todo: 暂未实现 idle 线程,暂用 assertion 来保障
  121. ASSERT(!list_empty(&thread_ready_list));
  122. thread_tag = NULL; // thread_tag 清空
  123. thread_tag = list_pop(&thread_ready_list); // 弹出队列中的第一个就绪线程
  124. struct task_struct *next = elem2entry(struct task_struct, general_tag, thread_tag);
  125. // 方法 2: PCB 在自然页的起始地址, 所以 pcb 地址=0xfffff000&(&(PCB.general_tag))
  126. next->status = TASK_RUNNING;
  127. // 激活任务页表等
  128. process_activate(next);
  129. switch_to(cur, next); // 切换线程
  130. }
  131. // 当前线程将自己阻塞,标志其状态为 status(不可运行状态)
  132. void thread_block(enum task_status status)
  133. { // 只有这三种状态才做阻塞
  134. ASSERT((status == TASK_BLOCKED) || (status == TASK_WAITING) || (status == TASK_HANGING));
  135. enum intr_status old_status = intr_disable(); // 关中断
  136. struct task_struct *cur = running_thread();
  137. cur->status = status; // 设置其状态为 status, 设置之前 cur->status == TASK_RUNNING
  138. schedule(); // 将当前线程换下处理器
  139. //! 待当前线程被解除阻塞后继续运行下面的 intr_set_status
  140. intr_set_status(old_status); // 恢复中断
  141. }
  142. // 解除线程的阻塞状态,标志其状态为 TASK_RUNNING
  143. // pthread 指需要被接触阻塞的线程
  144. void thread_unblock(struct task_struct *pthread)
  145. {
  146. enum intr_status old_status = intr_disable(); // 关中断
  147. ASSERT((pthread->status == TASK_BLOCKED) || (pthread->status == TASK_WAITING) || (pthread->status == TASK_HANGING));
  148. if (pthread->status != TASK_READY)
  149. {
  150. ASSERT(!elem_find(&thread_ready_list, &pthread->general_tag));
  151. if (elem_find(&thread_ready_list, &pthread->general_tag))
  152. {
  153. PANIC("thread_unblock: blocked thread in ready_list\n");
  154. }
  155. list_append(&thread_ready_list, &pthread->general_tag); // 加入到队列的最前面,使其尽快得到调度
  156. pthread->status = TASK_READY;
  157. }
  158. intr_set_status(old_status); // 恢复中断
  159. }
  160. // 初始化线程环境
  161. void thread_init(void)
  162. {
  163. put_str("thread_init start\n");
  164. list_init(&thread_ready_list);
  165. list_init(&thread_all_list);
  166. lock_init(&pid_lock);
  167. make_main_thread(); // 将当前 main 函数创建为线程
  168. put_str("thread_init end\n");
  169. }