kthread.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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, -1, 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. int kthread_stop(struct task_struct *k);
  36. bool kthread_should_stop(void);
  37. bool kthread_should_park(void);
  38. bool kthread_freezable_should_stop(bool *was_frozen);
  39. void *kthread_data(struct task_struct *k);
  40. void *probe_kthread_data(struct task_struct *k);
  41. int kthread_park(struct task_struct *k);
  42. void kthread_unpark(struct task_struct *k);
  43. void kthread_parkme(void);
  44. int kthreadd(void *unused);
  45. extern struct task_struct *kthreadd_task;
  46. extern int tsk_fork_get_node(struct task_struct *tsk);
  47. /*
  48. * Simple work processor based on kthread.
  49. *
  50. * This provides easier way to make use of kthreads. A kthread_work
  51. * can be queued and flushed using queue/flush_kthread_work()
  52. * respectively. Queued kthread_works are processed by a kthread
  53. * running kthread_worker_fn().
  54. */
  55. struct kthread_work;
  56. typedef void (*kthread_work_func_t)(struct kthread_work *work);
  57. struct kthread_worker {
  58. spinlock_t lock;
  59. struct list_head work_list;
  60. struct task_struct *task;
  61. struct kthread_work *current_work;
  62. };
  63. struct kthread_work {
  64. struct list_head node;
  65. kthread_work_func_t func;
  66. struct kthread_worker *worker;
  67. };
  68. #define KTHREAD_WORKER_INIT(worker) { \
  69. .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
  70. .work_list = LIST_HEAD_INIT((worker).work_list), \
  71. }
  72. #define KTHREAD_WORK_INIT(work, fn) { \
  73. .node = LIST_HEAD_INIT((work).node), \
  74. .func = (fn), \
  75. }
  76. #define DEFINE_KTHREAD_WORKER(worker) \
  77. struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
  78. #define DEFINE_KTHREAD_WORK(work, fn) \
  79. struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
  80. /*
  81. * kthread_worker.lock needs its own lockdep class key when defined on
  82. * stack with lockdep enabled. Use the following macros in such cases.
  83. */
  84. #ifdef CONFIG_LOCKDEP
  85. # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
  86. ({ init_kthread_worker(&worker); worker; })
  87. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
  88. struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
  89. #else
  90. # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
  91. #endif
  92. extern void __init_kthread_worker(struct kthread_worker *worker,
  93. const char *name, struct lock_class_key *key);
  94. #define init_kthread_worker(worker) \
  95. do { \
  96. static struct lock_class_key __key; \
  97. __init_kthread_worker((worker), "("#worker")->lock", &__key); \
  98. } while (0)
  99. #define init_kthread_work(work, fn) \
  100. do { \
  101. memset((work), 0, sizeof(struct kthread_work)); \
  102. INIT_LIST_HEAD(&(work)->node); \
  103. (work)->func = (fn); \
  104. } while (0)
  105. int kthread_worker_fn(void *worker_ptr);
  106. bool queue_kthread_work(struct kthread_worker *worker,
  107. struct kthread_work *work);
  108. void flush_kthread_work(struct kthread_work *work);
  109. void flush_kthread_worker(struct kthread_worker *worker);
  110. #endif /* _LINUX_KTHREAD_H */