kthread.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 *probe_kthread_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/flush_kthread_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. struct kthread_worker {
  59. spinlock_t lock;
  60. struct list_head work_list;
  61. struct task_struct *task;
  62. struct kthread_work *current_work;
  63. };
  64. struct kthread_work {
  65. struct list_head node;
  66. kthread_work_func_t func;
  67. struct kthread_worker *worker;
  68. };
  69. #define KTHREAD_WORKER_INIT(worker) { \
  70. .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
  71. .work_list = LIST_HEAD_INIT((worker).work_list), \
  72. }
  73. #define KTHREAD_WORK_INIT(work, fn) { \
  74. .node = LIST_HEAD_INIT((work).node), \
  75. .func = (fn), \
  76. }
  77. #define DEFINE_KTHREAD_WORKER(worker) \
  78. struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
  79. #define DEFINE_KTHREAD_WORK(work, fn) \
  80. struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
  81. /*
  82. * kthread_worker.lock needs its own lockdep class key when defined on
  83. * stack with lockdep enabled. Use the following macros in such cases.
  84. */
  85. #ifdef CONFIG_LOCKDEP
  86. # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
  87. ({ init_kthread_worker(&worker); worker; })
  88. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
  89. struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
  90. #else
  91. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
  92. #endif
  93. extern void __init_kthread_worker(struct kthread_worker *worker,
  94. const char *name, struct lock_class_key *key);
  95. #define init_kthread_worker(worker) \
  96. do { \
  97. static struct lock_class_key __key; \
  98. __init_kthread_worker((worker), "("#worker")->lock", &__key); \
  99. } while (0)
  100. #define init_kthread_work(work, fn) \
  101. do { \
  102. memset((work), 0, sizeof(struct kthread_work)); \
  103. INIT_LIST_HEAD(&(work)->node); \
  104. (work)->func = (fn); \
  105. } while (0)
  106. int kthread_worker_fn(void *worker_ptr);
  107. bool queue_kthread_work(struct kthread_worker *worker,
  108. struct kthread_work *work);
  109. void flush_kthread_work(struct kthread_work *work);
  110. void flush_kthread_worker(struct kthread_worker *worker);
  111. #endif /* _LINUX_KTHREAD_H */