kthread.h 5.8 KB

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