kthread.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. int kthreadd(void *unused);
  31. extern struct task_struct *kthreadd_task;
  32. /*
  33. * Simple work processor based on kthread.
  34. *
  35. * This provides easier way to make use of kthreads. A kthread_work
  36. * can be queued and flushed using queue/flush_kthread_work()
  37. * respectively. Queued kthread_works are processed by a kthread
  38. * running kthread_worker_fn().
  39. *
  40. * A kthread_work can't be freed while it is executing.
  41. */
  42. struct kthread_work;
  43. typedef void (*kthread_work_func_t)(struct kthread_work *work);
  44. struct kthread_worker {
  45. spinlock_t lock;
  46. struct list_head work_list;
  47. struct task_struct *task;
  48. };
  49. struct kthread_work {
  50. struct list_head node;
  51. kthread_work_func_t func;
  52. wait_queue_head_t done;
  53. atomic_t flushing;
  54. int queue_seq;
  55. int done_seq;
  56. };
  57. #define KTHREAD_WORKER_INIT(worker) { \
  58. .lock = SPIN_LOCK_UNLOCKED, \
  59. .work_list = LIST_HEAD_INIT((worker).work_list), \
  60. }
  61. #define KTHREAD_WORK_INIT(work, fn) { \
  62. .node = LIST_HEAD_INIT((work).node), \
  63. .func = (fn), \
  64. .done = __WAIT_QUEUE_HEAD_INITIALIZER((work).done), \
  65. .flushing = ATOMIC_INIT(0), \
  66. }
  67. #define DEFINE_KTHREAD_WORKER(worker) \
  68. struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
  69. #define DEFINE_KTHREAD_WORK(work, fn) \
  70. struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
  71. static inline void init_kthread_worker(struct kthread_worker *worker)
  72. {
  73. *worker = (struct kthread_worker)KTHREAD_WORKER_INIT(*worker);
  74. }
  75. static inline void init_kthread_work(struct kthread_work *work,
  76. kthread_work_func_t fn)
  77. {
  78. *work = (struct kthread_work)KTHREAD_WORK_INIT(*work, fn);
  79. }
  80. int kthread_worker_fn(void *worker_ptr);
  81. bool queue_kthread_work(struct kthread_worker *worker,
  82. struct kthread_work *work);
  83. void flush_kthread_work(struct kthread_work *work);
  84. void flush_kthread_worker(struct kthread_worker *worker);
  85. #endif /* _LINUX_KTHREAD_H */