kthread.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. __printf(4, 5)
  7. struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
  8. void *data,
  9. int node,
  10. const char namefmt[], ...);
  11. /**
  12. * kthread_create - create a kthread on the current node
  13. * @threadfn: the function to run in the thread
  14. * @data: data pointer for @threadfn()
  15. * @namefmt: printf-style format string for the thread name
  16. * @...: arguments for @namefmt.
  17. *
  18. * This macro will create a kthread on the current node, leaving it in
  19. * the stopped state. This is just a helper for kthread_create_on_node();
  20. * see the documentation there for more details.
  21. */
  22. #define kthread_create(threadfn, data, namefmt, arg...) \
  23. kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
  24. struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
  25. void *data,
  26. unsigned int cpu,
  27. const char *namefmt);
  28. /**
  29. * kthread_run - create and wake a thread.
  30. * @threadfn: the function to run until signal_pending(current).
  31. * @data: data ptr for @threadfn.
  32. * @namefmt: printf-style name for the thread.
  33. *
  34. * Description: Convenient wrapper for kthread_create() followed by
  35. * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
  36. */
  37. #define kthread_run(threadfn, data, namefmt, ...) \
  38. ({ \
  39. struct task_struct *__k \
  40. = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
  41. if (!IS_ERR(__k)) \
  42. wake_up_process(__k); \
  43. __k; \
  44. })
  45. void kthread_bind(struct task_struct *k, unsigned int cpu);
  46. void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
  47. int kthread_stop(struct task_struct *k);
  48. bool kthread_should_stop(void);
  49. bool kthread_should_park(void);
  50. bool kthread_freezable_should_stop(bool *was_frozen);
  51. void *kthread_data(struct task_struct *k);
  52. void *kthread_probe_data(struct task_struct *k);
  53. int kthread_park(struct task_struct *k);
  54. void kthread_unpark(struct task_struct *k);
  55. void kthread_parkme(void);
  56. int kthreadd(void *unused);
  57. extern struct task_struct *kthreadd_task;
  58. extern int tsk_fork_get_node(struct task_struct *tsk);
  59. /*
  60. * Simple work processor based on kthread.
  61. *
  62. * This provides easier way to make use of kthreads. A kthread_work
  63. * can be queued and flushed using queue/kthread_flush_work()
  64. * respectively. Queued kthread_works are processed by a kthread
  65. * running kthread_worker_fn().
  66. */
  67. struct kthread_work;
  68. typedef void (*kthread_work_func_t)(struct kthread_work *work);
  69. void kthread_delayed_work_timer_fn(unsigned long __data);
  70. enum {
  71. KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
  72. };
  73. struct kthread_worker {
  74. unsigned int flags;
  75. spinlock_t lock;
  76. struct list_head work_list;
  77. struct list_head delayed_work_list;
  78. struct task_struct *task;
  79. struct kthread_work *current_work;
  80. };
  81. struct kthread_work {
  82. struct list_head node;
  83. kthread_work_func_t func;
  84. struct kthread_worker *worker;
  85. /* Number of canceling calls that are running at the moment. */
  86. int canceling;
  87. };
  88. struct kthread_delayed_work {
  89. struct kthread_work work;
  90. struct timer_list timer;
  91. };
  92. #define KTHREAD_WORKER_INIT(worker) { \
  93. .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
  94. .work_list = LIST_HEAD_INIT((worker).work_list), \
  95. .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
  96. }
  97. #define KTHREAD_WORK_INIT(work, fn) { \
  98. .node = LIST_HEAD_INIT((work).node), \
  99. .func = (fn), \
  100. }
  101. #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
  102. .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
  103. .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn, \
  104. 0, (unsigned long)&(dwork), \
  105. TIMER_IRQSAFE), \
  106. }
  107. #define DEFINE_KTHREAD_WORKER(worker) \
  108. struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
  109. #define DEFINE_KTHREAD_WORK(work, fn) \
  110. struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
  111. #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
  112. struct kthread_delayed_work dwork = \
  113. KTHREAD_DELAYED_WORK_INIT(dwork, fn)
  114. /*
  115. * kthread_worker.lock needs its own lockdep class key when defined on
  116. * stack with lockdep enabled. Use the following macros in such cases.
  117. */
  118. #ifdef CONFIG_LOCKDEP
  119. # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
  120. ({ kthread_init_worker(&worker); worker; })
  121. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
  122. struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
  123. #else
  124. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
  125. #endif
  126. extern void __kthread_init_worker(struct kthread_worker *worker,
  127. const char *name, struct lock_class_key *key);
  128. #define kthread_init_worker(worker) \
  129. do { \
  130. static struct lock_class_key __key; \
  131. __kthread_init_worker((worker), "("#worker")->lock", &__key); \
  132. } while (0)
  133. #define kthread_init_work(work, fn) \
  134. do { \
  135. memset((work), 0, sizeof(struct kthread_work)); \
  136. INIT_LIST_HEAD(&(work)->node); \
  137. (work)->func = (fn); \
  138. } while (0)
  139. #define kthread_init_delayed_work(dwork, fn) \
  140. do { \
  141. kthread_init_work(&(dwork)->work, (fn)); \
  142. __setup_timer(&(dwork)->timer, \
  143. kthread_delayed_work_timer_fn, \
  144. (unsigned long)(dwork), \
  145. TIMER_IRQSAFE); \
  146. } while (0)
  147. int kthread_worker_fn(void *worker_ptr);
  148. __printf(2, 3)
  149. struct kthread_worker *
  150. kthread_create_worker(unsigned int flags, const char namefmt[], ...);
  151. struct kthread_worker *
  152. kthread_create_worker_on_cpu(int cpu, unsigned int flags,
  153. const char namefmt[], ...);
  154. bool kthread_queue_work(struct kthread_worker *worker,
  155. struct kthread_work *work);
  156. bool kthread_queue_delayed_work(struct kthread_worker *worker,
  157. struct kthread_delayed_work *dwork,
  158. unsigned long delay);
  159. bool kthread_mod_delayed_work(struct kthread_worker *worker,
  160. struct kthread_delayed_work *dwork,
  161. unsigned long delay);
  162. void kthread_flush_work(struct kthread_work *work);
  163. void kthread_flush_worker(struct kthread_worker *worker);
  164. bool kthread_cancel_work_sync(struct kthread_work *work);
  165. bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
  166. void kthread_destroy_worker(struct kthread_worker *worker);
  167. #endif /* _LINUX_KTHREAD_H */