1 #ifndef _LINUX_KTHREAD_H
2 #define _LINUX_KTHREAD_H
3 /* Simple interface for creating and stopping kernel threads without mess. */
5 #include <linux/sched.h>
8 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
11 const char namefmt[], ...);
13 #define kthread_create(threadfn, data, namefmt, arg...) \
14 kthread_create_on_node(threadfn, data, -1, namefmt, ##arg)
17 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
23 * kthread_run - create and wake a thread.
24 * @threadfn: the function to run until signal_pending(current).
25 * @data: data ptr for @threadfn.
26 * @namefmt: printf-style name for the thread.
28 * Description: Convenient wrapper for kthread_create() followed by
29 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
31 #define kthread_run(threadfn, data, namefmt, ...) \
33 struct task_struct *__k \
34 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
36 wake_up_process(__k); \
40 void kthread_bind(struct task_struct *k, unsigned int cpu);
41 int kthread_stop(struct task_struct *k);
42 bool kthread_should_stop(void);
43 bool kthread_should_park(void);
44 bool kthread_freezable_should_stop(bool *was_frozen);
45 void *kthread_data(struct task_struct *k);
46 void *probe_kthread_data(struct task_struct *k);
47 int kthread_park(struct task_struct *k);
48 void kthread_unpark(struct task_struct *k);
49 void kthread_parkme(void);
51 int kthreadd(void *unused);
52 extern struct task_struct *kthreadd_task;
53 extern int tsk_fork_get_node(struct task_struct *tsk);
56 * Simple work processor based on kthread.
58 * This provides easier way to make use of kthreads. A kthread_work
59 * can be queued and flushed using queue/flush_kthread_work()
60 * respectively. Queued kthread_works are processed by a kthread
61 * running kthread_worker_fn().
64 typedef void (*kthread_work_func_t)(struct kthread_work *work);
66 struct kthread_worker {
68 struct list_head work_list;
69 struct task_struct *task;
70 struct kthread_work *current_work;
74 struct list_head node;
75 kthread_work_func_t func;
76 wait_queue_head_t done;
77 struct kthread_worker *worker;
80 #define KTHREAD_WORKER_INIT(worker) { \
81 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
82 .work_list = LIST_HEAD_INIT((worker).work_list), \
85 #define KTHREAD_WORK_INIT(work, fn) { \
86 .node = LIST_HEAD_INIT((work).node), \
88 .done = __WAIT_QUEUE_HEAD_INITIALIZER((work).done), \
91 #define DEFINE_KTHREAD_WORKER(worker) \
92 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
94 #define DEFINE_KTHREAD_WORK(work, fn) \
95 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
98 * kthread_worker.lock and kthread_work.done need their own lockdep class
99 * keys if they are defined on stack with lockdep enabled. Use the
100 * following macros when defining them on stack.
102 #ifdef CONFIG_LOCKDEP
103 # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
104 ({ init_kthread_worker(&worker); worker; })
105 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
106 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
107 # define KTHREAD_WORK_INIT_ONSTACK(work, fn) \
108 ({ init_kthread_work((&work), fn); work; })
109 # define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) \
110 struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn)
112 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
113 # define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) DEFINE_KTHREAD_WORK(work, fn)
116 extern void __init_kthread_worker(struct kthread_worker *worker,
117 const char *name, struct lock_class_key *key);
119 #define init_kthread_worker(worker) \
121 static struct lock_class_key __key; \
122 __init_kthread_worker((worker), "("#worker")->lock", &__key); \
125 #define init_kthread_work(work, fn) \
127 memset((work), 0, sizeof(struct kthread_work)); \
128 INIT_LIST_HEAD(&(work)->node); \
129 (work)->func = (fn); \
130 init_waitqueue_head(&(work)->done); \
133 int kthread_worker_fn(void *worker_ptr);
135 bool queue_kthread_work(struct kthread_worker *worker,
136 struct kthread_work *work);
137 void flush_kthread_work(struct kthread_work *work);
138 void flush_kthread_worker(struct kthread_worker *worker);
140 #endif /* _LINUX_KTHREAD_H */