1 #ifndef _LINUX_KTHREAD_H
2 #define _LINUX_KTHREAD_H
4 /* Simple interface for creating and stopping kernel threads without mess. */
6 #include <linux/lockdep.h>
7 #include <linux/sched.h>
8 #include <linux/spinlock.h>
11 struct task_struct *kthread_create(int (*threadfn)(void *data),
13 const char namefmt[], ...);
16 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
22 * kthread_run - create and wake a thread.
23 * @threadfn: the function to run until signal_pending(current).
24 * @data: data ptr for @threadfn.
25 * @namefmt: printf-style name for the thread.
27 * Description: Convenient wrapper for kthread_create() followed by
28 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
30 #define kthread_run(threadfn, data, namefmt, ...) \
32 struct task_struct *__k \
33 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
35 wake_up_process(__k); \
39 int kthread_stop(struct task_struct *k);
40 bool kthread_should_stop(void);
41 bool kthread_should_park(void);
42 bool kthread_freezable_should_stop(bool *was_frozen);
43 void *kthread_data(struct task_struct *k);
44 void *probe_kthread_data(struct task_struct *k);
45 int kthread_park(struct task_struct *k);
46 void kthread_unpark(struct task_struct *k);
47 void kthread_parkme(void);
49 int kthreadd(void *unused);
50 extern struct task_struct *kthreadd_task;
51 extern int tsk_fork_get_node(struct task_struct *tsk);
54 * Simple work processor based on kthread.
56 * This provides easier way to make use of kthreads. A kthread_work
57 * can be queued and flushed using queue/flush_kthread_work()
58 * respectively. Queued kthread_works are processed by a kthread
59 * running kthread_worker_fn().
62 typedef void (*kthread_work_func_t)(struct kthread_work *work);
64 struct kthread_worker {
66 struct list_head work_list;
67 struct task_struct *task;
68 struct kthread_work *current_work;
72 struct list_head node;
73 kthread_work_func_t func;
74 struct kthread_worker *worker;
77 #define KTHREAD_WORKER_INIT(worker) { \
78 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
79 .work_list = LIST_HEAD_INIT((worker).work_list), \
82 #define KTHREAD_WORK_INIT(work, fn) { \
83 .node = LIST_HEAD_INIT((work).node), \
87 #define DEFINE_KTHREAD_WORKER(worker) \
88 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
90 #define DEFINE_KTHREAD_WORK(work, fn) \
91 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
93 #define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
95 extern void __init_kthread_worker(struct kthread_worker *worker,
96 const char *name, struct lock_class_key *key);
98 #define init_kthread_worker(worker) \
100 static struct lock_class_key __key; \
101 __init_kthread_worker((worker), "("#worker")->lock", &__key); \
104 #define init_kthread_work(work, fn) \
106 memset((work), 0, sizeof(struct kthread_work)); \
107 INIT_LIST_HEAD(&(work)->node); \
108 (work)->func = (fn); \
111 int kthread_worker_fn(void *worker_ptr);
113 bool queue_kthread_work(struct kthread_worker *worker,
114 struct kthread_work *work);
115 void flush_kthread_work(struct kthread_work *work);
116 void flush_kthread_worker(struct kthread_worker *worker);
118 #endif /* _LINUX_KTHREAD_H */