kthread.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. struct process_control_block *kthread_create_on_node(int (*thread_fn)(void *data),
  7. void *data,
  8. int node,
  9. const char name_fmt[], ...);
  10. /**
  11. * @brief 在当前结点上创建一个内核线程
  12. *
  13. * @param thread_fn 该内核线程要执行的函数
  14. * @param data 传递给 thread_fn 的参数数据
  15. * @param name_fmt printf-style format string for the thread name
  16. * @param arg name_fmt的参数
  17. *
  18. * 请注意,该宏会创建一个内核线程,并将其设置为停止状态
  19. */
  20. #define kthread_create(thread_fn, data, name_fmt, arg...) \
  21. kthread_create_on_node(thread_fn, data, NUMA_NO_NODE, name_fmt, ##arg)
  22. /**
  23. * @brief 创建内核线程,并将其唤醒
  24. *
  25. * @param thread_fn 该内核线程要执行的函数
  26. * @param data 传递给 thread_fn 的参数数据
  27. * @param name_fmt printf-style format string for the thread name
  28. * @param arg name_fmt的参数
  29. */
  30. #define kthread_run(thread_fn, data, name_fmt, ...) \
  31. ({ \
  32. struct process_control_block *__kt = kthread_create(thread_fn, data, name_fmt, ##__VA_ARGS__); \
  33. if (!IS_ERR(__kt)) \
  34. process_wakeup(__kt); \
  35. __kt; \
  36. })
  37. /**
  38. * @brief 向kthread发送停止信号,请求其结束
  39. *
  40. * @param pcb 内核线程的pcb
  41. * @return int 错误码
  42. */
  43. int kthread_stop(struct process_control_block * pcb);
  44. /**
  45. * @brief 内核线程调用该函数,检查自身的标志位,判断自己是否应该执行完任务后退出
  46. *
  47. * @return true 内核线程应该退出
  48. * @return false 无需退出
  49. */
  50. bool kthread_should_stop(void);
  51. /**
  52. * @brief 让当前内核线程退出,并返回result参数给kthread_stop()函数
  53. *
  54. * @param result 返回值
  55. */
  56. void kthread_exit(long result);
  57. /**
  58. * @brief 初始化kthread机制(只应被process_init调用)
  59. *
  60. * @return int 错误码
  61. */
  62. int kthread_mechanism_init();
  63. /**
  64. * @brief 设置pcb中的worker_private字段(只应被设置一次)
  65. *
  66. * @param pcb pcb
  67. * @return bool 成功或失败
  68. */
  69. bool kthread_set_worker_private(struct process_control_block *pcb);