kthread.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #include <common/numa.h>
  3. #include <process/proc-types.h>
  4. #include <common/err.h>
  5. #include <process/process.h>
  6. /**
  7. * @brief kthread信息
  8. * 该结构体将会绑定到pcb的worker_private中
  9. */
  10. struct kthread_info_t
  11. {
  12. uint64_t flags;
  13. uint32_t cpu;
  14. int result;
  15. int (*thread_fn)(void *);
  16. void *data;
  17. // todo: 将这里改为completion机制
  18. bool exited; // 是否已退出
  19. char *full_name; // 内核线程的名称
  20. };
  21. struct process_control_block *kthread_create_on_node(int (*thread_fn)(void *data),
  22. void *data,
  23. int node,
  24. const char name_fmt[], ...);
  25. /**
  26. * @brief 在当前结点上创建一个内核线程
  27. *
  28. * @param thread_fn 该内核线程要执行的函数
  29. * @param data 传递给 thread_fn 的参数数据
  30. * @param name_fmt printf-style format string for the thread name
  31. * @param arg name_fmt的参数
  32. *
  33. * 请注意,该宏会创建一个内核线程,并将其设置为停止状态
  34. */
  35. #define kthread_create(thread_fn, data, name_fmt, arg...) \
  36. kthread_create_on_node(thread_fn, data, NUMA_NO_NODE, name_fmt, ##arg)
  37. /**
  38. * @brief 创建内核线程,并将其唤醒
  39. *
  40. * @param thread_fn 该内核线程要执行的函数
  41. * @param data 传递给 thread_fn 的参数数据
  42. * @param name_fmt printf-style format string for the thread name
  43. * @param arg name_fmt的参数
  44. */
  45. #define kthread_run(thread_fn, data, name_fmt, ...) \
  46. ({ \
  47. struct process_control_block *__kt = kthread_create(thread_fn, data, name_fmt, ##__VA_ARGS__); \
  48. if (!IS_ERR(__kt)) \
  49. process_wakeup(__kt); \
  50. __kt; \
  51. })
  52. /**
  53. * @brief 向kthread发送停止信号,请求其结束
  54. *
  55. * @param pcb 内核线程的pcb
  56. * @return int 错误码
  57. */
  58. int kthread_stop(struct process_control_block * pcb);
  59. /**
  60. * @brief 内核线程调用该函数,检查自身的标志位,判断自己是否应该执行完任务后退出
  61. *
  62. * @return true 内核线程应该退出
  63. * @return false 无需退出
  64. */
  65. bool kthread_should_stop(void);
  66. /**
  67. * @brief 让当前内核线程退出,并返回result参数给kthread_stop()函数
  68. *
  69. * @param result 返回值
  70. */
  71. void kthread_exit(long result);
  72. /**
  73. * @brief 初始化kthread机制(只应被process_init调用)
  74. *
  75. * @return int 错误码
  76. */
  77. int kthread_mechanism_init();
  78. /**
  79. * @brief 设置pcb中的worker_private字段(只应被设置一次)
  80. *
  81. * @param pcb pcb
  82. * @return bool 成功或失败
  83. */
  84. bool kthread_set_worker_private(struct process_control_block *pcb);