kthread.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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((worker).lock), \
  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. /*
  73. * kthread_worker.lock and kthread_work.done need their own lockdep class
  74. * keys if they are defined on stack with lockdep enabled. Use the
  75. * following macros when defining them on stack.
  76. */
  77. #ifdef CONFIG_LOCKDEP
  78. # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
  79. ({ init_kthread_worker(&worker); worker; })
  80. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
  81. struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
  82. # define KTHREAD_WORK_INIT_ONSTACK(work, fn) \
  83. ({ init_kthread_work((&work), fn); work; })
  84. # define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) \
  85. struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn)
  86. #else
  87. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
  88. # define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) DEFINE_KTHREAD_WORK(work, fn)
  89. #endif
  90. extern void __init_kthread_worker(struct kthread_worker *worker,
  91. const char *name, struct lock_class_key *key);
  92. #define init_kthread_worker(worker) \
  93. do { \
  94. static struct lock_class_key __key; \
  95. __init_kthread_worker((worker), "("#worker")->lock", &__key); \
  96. } while (0)
  97. #define init_kthread_work(work, fn) \
  98. do { \
  99. memset((work), 0, sizeof(struct kthread_work)); \
  100. INIT_LIST_HEAD(&(work)->node); \
  101. (work)->func = (fn); \
  102. init_waitqueue_head(&(work)->done); \
  103. } while (0)
  104. int kthread_worker_fn(void *worker_ptr);
  105. bool queue_kthread_work(struct kthread_worker *worker,
  106. struct kthread_work *work);
  107. void flush_kthread_work(struct kthread_work *work);
  108. void flush_kthread_worker(struct kthread_worker *worker);
  109. #endif /* _LINUX_KTHREAD_H */