kthread.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_on_node(int (*threadfn)(void *data),
  7. void *data,
  8. int node,
  9. const char namefmt[], ...)
  10. __attribute__((format(printf, 4, 5)));
  11. #define kthread_create(threadfn, data, namefmt, arg...) \
  12. kthread_create_on_node(threadfn, data, -1, namefmt, ##arg)
  13. /**
  14. * kthread_run - create and wake a thread.
  15. * @threadfn: the function to run until signal_pending(current).
  16. * @data: data ptr for @threadfn.
  17. * @namefmt: printf-style name for the thread.
  18. *
  19. * Description: Convenient wrapper for kthread_create() followed by
  20. * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
  21. */
  22. #define kthread_run(threadfn, data, namefmt, ...) \
  23. ({ \
  24. struct task_struct *__k \
  25. = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
  26. if (!IS_ERR(__k)) \
  27. wake_up_process(__k); \
  28. __k; \
  29. })
  30. void kthread_bind(struct task_struct *k, unsigned int cpu);
  31. int kthread_stop(struct task_struct *k);
  32. int kthread_should_stop(void);
  33. void *kthread_data(struct task_struct *k);
  34. int kthreadd(void *unused);
  35. extern struct task_struct *kthreadd_task;
  36. extern int tsk_fork_get_node(struct task_struct *tsk);
  37. /*
  38. * Simple work processor based on kthread.
  39. *
  40. * This provides easier way to make use of kthreads. A kthread_work
  41. * can be queued and flushed using queue/flush_kthread_work()
  42. * respectively. Queued kthread_works are processed by a kthread
  43. * running kthread_worker_fn().
  44. *
  45. * A kthread_work can't be freed while it is executing.
  46. */
  47. struct kthread_work;
  48. typedef void (*kthread_work_func_t)(struct kthread_work *work);
  49. struct kthread_worker {
  50. spinlock_t lock;
  51. struct list_head work_list;
  52. struct task_struct *task;
  53. };
  54. struct kthread_work {
  55. struct list_head node;
  56. kthread_work_func_t func;
  57. wait_queue_head_t done;
  58. atomic_t flushing;
  59. int queue_seq;
  60. int done_seq;
  61. };
  62. #define KTHREAD_WORKER_INIT(worker) { \
  63. .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
  64. .work_list = LIST_HEAD_INIT((worker).work_list), \
  65. }
  66. #define KTHREAD_WORK_INIT(work, fn) { \
  67. .node = LIST_HEAD_INIT((work).node), \
  68. .func = (fn), \
  69. .done = __WAIT_QUEUE_HEAD_INITIALIZER((work).done), \
  70. .flushing = ATOMIC_INIT(0), \
  71. }
  72. #define DEFINE_KTHREAD_WORKER(worker) \
  73. struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
  74. #define DEFINE_KTHREAD_WORK(work, fn) \
  75. struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
  76. /*
  77. * kthread_worker.lock and kthread_work.done need their own lockdep class
  78. * keys if they are defined on stack with lockdep enabled. Use the
  79. * following macros when defining them on stack.
  80. */
  81. #ifdef CONFIG_LOCKDEP
  82. # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
  83. ({ init_kthread_worker(&worker); worker; })
  84. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
  85. struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
  86. # define KTHREAD_WORK_INIT_ONSTACK(work, fn) \
  87. ({ init_kthread_work((&work), fn); work; })
  88. # define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) \
  89. struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn)
  90. #else
  91. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
  92. # define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) DEFINE_KTHREAD_WORK(work, fn)
  93. #endif
  94. extern void __init_kthread_worker(struct kthread_worker *worker,
  95. const char *name, struct lock_class_key *key);
  96. #define init_kthread_worker(worker) \
  97. do { \
  98. static struct lock_class_key __key; \
  99. __init_kthread_worker((worker), "("#worker")->lock", &__key); \
  100. } while (0)
  101. #define init_kthread_work(work, fn) \
  102. do { \
  103. memset((work), 0, sizeof(struct kthread_work)); \
  104. INIT_LIST_HEAD(&(work)->node); \
  105. (work)->func = (fn); \
  106. init_waitqueue_head(&(work)->done); \
  107. } while (0)
  108. int kthread_worker_fn(void *worker_ptr);
  109. bool queue_kthread_work(struct kthread_worker *worker,
  110. struct kthread_work *work);
  111. void flush_kthread_work(struct kthread_work *work);
  112. void flush_kthread_worker(struct kthread_worker *worker);
  113. #endif /* _LINUX_KTHREAD_H */