thread.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef __THREAD_THREAD_H
  2. #define __THREAD_THREAD_H
  3. #include "../lib/stdint.h"
  4. #include "../lib/kernel/list.h"
  5. #define PG_SIZE 4096
  6. // 通用函数类型,它将在很多线程函数中作为参数类型
  7. typedef void thread_func(void *);
  8. // 线程的状态
  9. enum task_status
  10. {
  11. TASK_RUNNING,
  12. TASK_READY,
  13. TASK_BLOCKED,
  14. TASK_WAITING,
  15. TASK_HANGING,
  16. TASK_DIED
  17. };
  18. /******************** 中断栈 intr_stack **********************************
  19. * 些结构用于中断发生时保护程序(线程或进程)的上下文环境:
  20. * 进程或线程被外部中断或软中断打断时,需要保护程序(线程或进程)的上下文寄存器,
  21. * kernel.S 中的 intr_exit 出栈操作是些结构的逆操作。
  22. * 此栈在线程自己的内核栈中位置固定,所在页的最顶端。
  23. ***********************************************************************/
  24. struct intr_stack
  25. {
  26. uint32_t vec_no; // kernel.S 宏VECTOR中push %1压入的中断号
  27. uint32_t edi;
  28. uint32_t esi;
  29. uint32_t ebp;
  30. uint32_t esp_dummy; // 虽然pushad会把esp压入,但esp是不断变化的,所以会被popad忽略
  31. uint32_t ebx;
  32. uint32_t edx;
  33. uint32_t ecx;
  34. uint32_t eax;
  35. uint32_t gs;
  36. uint32_t fs;
  37. uint32_t es;
  38. uint32_t ds;
  39. // 以下由cpu从低特权级进入高特权级时压入
  40. uint32_t err_code; // err_code会被压入在eip之后
  41. void (*eip)(void);
  42. uint32_t cs;
  43. uint32_t eflags;
  44. void *esp;
  45. uint32_t ss;
  46. };
  47. /********************* 线程栈thread_stack ********************************
  48. * 线程自己的栈,用于存储线程中待执行的函数
  49. * 此结构在线程自己的内核栈中位置不固定
  50. * 仅用在 switch_to 时保存线程环境
  51. * 实际位置取决于实际运行情况
  52. ***********************************************************************/
  53. struct thread_stack
  54. {
  55. // All registers on the Intel386 缸e global and thus visible to both a calling and a called function.
  56. // Registers %ebp, %ebx, %edi, %esi, and %esp “ belong” to the calling function.
  57. // In other words, a called function must preserve these registers' values for its caller.
  58. // Remaining registers “ belong” to the called function.
  59. // If a calling function wants to preserve such a register value across a function call,
  60. // it must save the value in its local stack frame.
  61. uint32_t ebp;
  62. uint32_t ebx;
  63. uint32_t edi;
  64. uint32_t esi;
  65. // 线程第一次执行时,eip指向待调用的函数kernel_thread
  66. // 其他时候,指向switch_to的返回地址
  67. void (*eip)(thread_func *func, void *func_arg);
  68. // 以下仅供第一次被调度上cpu时使用
  69. // 参数unused_ret只为占位置充数为返回地址
  70. void(*unused_retaddr);
  71. thread_func *function; // 由kernel_thread所调用的函数名
  72. void *func_arg; // 由kernel_thread所调用的函数所需的参数
  73. };
  74. // 进程或线程的 pcb,程序控制块(process control block)
  75. struct task_struct
  76. {
  77. uint32_t *self_kstack; // 各内核线程都用自己的内核栈
  78. enum task_status status; // 线程状态
  79. char name[16]; // 进程或线程的名字
  80. uint8_t priority; // 线程优先级
  81. uint8_t ticks; // 每次在处理器上执行的时间嘀嗒数
  82. uint32_t elapsed_ticks; // 累计计执行的 ticks 数
  83. struct list_elem general_tag; // 用于线程在一般队列中的结点
  84. struct list_elem all_list_tag; // 用于线程队列 thread_all_list 中的结点
  85. uint32_t *pgdir; // 进程自己页表的虚拟地址, 如果是线程则为 NULL
  86. uint32_t stack_magic; // 用这串数字做栈的边界标记,用于检测栈的溢出
  87. };
  88. // 获取当前线程的 PCB 指针
  89. struct task_struct *running_thread(void);
  90. // 初始化线程栈 thread_stack
  91. void thread_stack_create(struct task_struct *pthread, thread_func function, void *func_arg);
  92. // 初始化线程基本信息
  93. void init_thread(struct task_struct *pthread, char *name, int prio);
  94. // 创建优先级为 prio 的线程, 线程名为 name, 线程所执行的函数是 function(func_arg)
  95. struct task_struct *thread_start(char *name, int prio, thread_func function, void *func_arg);
  96. // 任务调度
  97. void schedule(void);
  98. // 当前线程将自己阻塞,标志其状态为 status
  99. void thread_block(enum task_status status);
  100. // 解除线程的阻塞状态,标志其状态为 TASK_RUNNING
  101. void thread_unblock(struct task_struct *pthread);
  102. // 初始化线程环境
  103. void thread_init(void);
  104. #endif // __THREAD_THREAD_H