thread.h 4.2 KB

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