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)
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.
23 * Description: Convenient wrapper for kthread_create() followed by
24 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
26 #define kthread_run(threadfn, data, namefmt, ...) \
28 struct task_struct *__k \
29 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
31 wake_up_process(__k); \
35 void kthread_bind(struct task_struct
*k
, unsigned int cpu
);
36 int kthread_stop(struct task_struct
*k
);
37 int kthread_should_stop(void);
38 bool kthread_freezable_should_stop(bool *was_frozen
);
39 void *kthread_data(struct task_struct
*k
);
41 int kthreadd(void *unused
);
42 extern struct task_struct
*kthreadd_task
;
43 extern int tsk_fork_get_node(struct task_struct
*tsk
);
46 * Simple work processor based on kthread.
48 * This provides easier way to make use of kthreads. A kthread_work
49 * can be queued and flushed using queue/flush_kthread_work()
50 * respectively. Queued kthread_works are processed by a kthread
51 * running kthread_worker_fn().
53 * A kthread_work can't be freed while it is executing.
56 typedef void (*kthread_work_func_t
)(struct kthread_work
*work
);
58 struct kthread_worker
{
60 struct list_head work_list
;
61 struct task_struct
*task
;
65 struct list_head node
;
66 kthread_work_func_t func
;
67 wait_queue_head_t done
;
73 #define KTHREAD_WORKER_INIT(worker) { \
74 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
75 .work_list = LIST_HEAD_INIT((worker).work_list), \
78 #define KTHREAD_WORK_INIT(work, fn) { \
79 .node = LIST_HEAD_INIT((work).node), \
81 .done = __WAIT_QUEUE_HEAD_INITIALIZER((work).done), \
82 .flushing = ATOMIC_INIT(0), \
85 #define DEFINE_KTHREAD_WORKER(worker) \
86 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
88 #define DEFINE_KTHREAD_WORK(work, fn) \
89 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
92 * kthread_worker.lock and kthread_work.done need their own lockdep class
93 * keys if they are defined on stack with lockdep enabled. Use the
94 * following macros when defining them on stack.
97 # define KTHREAD_WORKER_INIT_ONSTACK(worker) \
98 ({ init_kthread_worker(&worker); worker; })
99 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
100 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
101 # define KTHREAD_WORK_INIT_ONSTACK(work, fn) \
102 ({ init_kthread_work((&work), fn); work; })
103 # define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) \
104 struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn)
106 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
107 # define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) DEFINE_KTHREAD_WORK(work, fn)
110 extern void __init_kthread_worker(struct kthread_worker
*worker
,
111 const char *name
, struct lock_class_key
*key
);
113 #define init_kthread_worker(worker) \
115 static struct lock_class_key __key; \
116 __init_kthread_worker((worker), "("#worker")->lock", &__key); \
119 #define init_kthread_work(work, fn) \
121 memset((work), 0, sizeof(struct kthread_work)); \
122 INIT_LIST_HEAD(&(work)->node); \
123 (work)->func = (fn); \
124 init_waitqueue_head(&(work)->done); \
127 int kthread_worker_fn(void *worker_ptr
);
129 bool queue_kthread_work(struct kthread_worker
*worker
,
130 struct kthread_work
*work
);
131 void flush_kthread_work(struct kthread_work
*work
);
132 void flush_kthread_worker(struct kthread_worker
*worker
);
134 #endif /* _LINUX_KTHREAD_H */