kthread.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef _LINUX_KTHREAD_H
  2. #define _LINUX_KTHREAD_H
  3. /* Simple interface for creating and stopping kernel threads without mess. */
  4. #include <linux/err.h>
  5. #include <linux/sched.h>
  6. struct task_struct *kthread_create(int (*threadfn)(void *data),
  7. void *data,
  8. const char namefmt[], ...)
  9. __attribute__((format(printf, 3, 4)));
  10. /**
  11. * kthread_run - create and wake a thread.
  12. * @threadfn: the function to run until signal_pending(current).
  13. * @data: data ptr for @threadfn.
  14. * @namefmt: printf-style name for the thread.
  15. *
  16. * Description: Convenient wrapper for kthread_create() followed by
  17. * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
  18. */
  19. #define kthread_run(threadfn, data, namefmt, ...) \
  20. ({ \
  21. struct task_struct *__k \
  22. = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
  23. if (!IS_ERR(__k)) \
  24. wake_up_process(__k); \
  25. __k; \
  26. })
  27. void kthread_bind(struct task_struct *k, unsigned int cpu);
  28. int kthread_stop(struct task_struct *k);
  29. int kthread_should_stop(void);
  30. void *kthread_data(struct task_struct *k);
  31. int kthreadd(void *unused);
  32. extern struct task_struct *kthreadd_task;
  33. /*
  34. * Simple work processor based on kthread.
  35. *
  36. * This provides easier way to make use of kthreads. A kthread_work
  37. * can be queued and flushed using queue/flush_kthread_work()
  38. * respectively. Queued kthread_works are processed by a kthread
  39. * running kthread_worker_fn().
  40. *
  41. * A kthread_work can't be freed while it is executing.
  42. */
  43. struct kthread_work;
  44. typedef void (*kthread_work_func_t)(struct kthread_work *work);
  45. struct kthread_worker {
  46. spinlock_t lock;
  47. struct list_head work_list;
  48. struct task_struct *task;
  49. };
  50. struct kthread_work {
  51. struct list_head node;
  52. kthread_work_func_t func;
  53. wait_queue_head_t done;
  54. atomic_t flushing;
  55. int queue_seq;
  56. int done_seq;
  57. };
  58. #define KTHREAD_WORKER_INIT(worker) { \
  59. .lock = SPIN_LOCK_UNLOCKED, \
  60. .work_list = LIST_HEAD_INIT((worker).work_list), \
  61. }
  62. #define KTHREAD_WORK_INIT(work, fn) { \
  63. .node = LIST_HEAD_INIT((work).node), \
  64. .func = (fn), \
  65. .done = __WAIT_QUEUE_HEAD_INITIALIZER((work).done), \
  66. .flushing = ATOMIC_INIT(0), \
  67. }
  68. #define DEFINE_KTHREAD_WORKER(worker) \
  69. struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
  70. #define DEFINE_KTHREAD_WORK(work, fn) \
  71. struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
  72. static inline void init_kthread_worker(struct kthread_worker *worker)
  73. {
  74. *worker = (struct kthread_worker)KTHREAD_WORKER_INIT(*worker);
  75. }
  76. static inline void init_kthread_work(struct kthread_work *work,
  77. kthread_work_func_t fn)
  78. {
  79. *work = (struct kthread_work)KTHREAD_WORK_INIT(*work, fn);
  80. }
  81. int kthread_worker_fn(void *worker_ptr);
  82. bool queue_kthread_work(struct kthread_worker *worker,
  83. struct kthread_work *work);
  84. void flush_kthread_work(struct kthread_work *work);
  85. void flush_kthread_worker(struct kthread_worker *worker);
  86. #endif /* _LINUX_KTHREAD_H */