blob: 0714b24c0e454c38750e4509a6bc5ee17e394673 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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
Joe Perchesb9075fa2011-10-31 17:11:33 -07007__printf(4, 5)
Eric Dumazet207205a2011-03-22 16:30:44 -07008struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
9 void *data,
10 int node,
Joe Perchesb9075fa2011-10-31 17:11:33 -070011 const char namefmt[], ...);
Eric Dumazet207205a2011-03-22 16:30:44 -070012
13#define kthread_create(threadfn, data, namefmt, arg...) \
14 kthread_create_on_node(threadfn, data, -1, namefmt, ##arg)
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -070018 * kthread_run - create and wake a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * @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
Randy Dunlap9e37bd32006-06-25 05:49:19 -070024 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
25 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070035void kthread_bind(struct task_struct *k, unsigned int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036int kthread_stop(struct task_struct *k);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037int kthread_should_stop(void);
Tejun Heo8a32c442011-11-21 12:32:23 -080038bool kthread_freezable_should_stop(bool *was_frozen);
Tejun Heo82805ab2010-06-29 10:07:09 +020039void *kthread_data(struct task_struct *k);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Eric W. Biederman73c27992007-05-09 02:34:32 -070041int kthreadd(void *unused);
42extern struct task_struct *kthreadd_task;
Eric Dumazet207205a2011-03-22 16:30:44 -070043extern int tsk_fork_get_node(struct task_struct *tsk);
Eric W. Biederman73c27992007-05-09 02:34:32 -070044
Tejun Heob56c0d82010-06-29 10:07:09 +020045/*
46 * Simple work processor based on kthread.
47 *
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().
52 *
53 * A kthread_work can't be freed while it is executing.
54 */
55struct kthread_work;
56typedef void (*kthread_work_func_t)(struct kthread_work *work);
57
58struct kthread_worker {
59 spinlock_t lock;
60 struct list_head work_list;
61 struct task_struct *task;
62};
63
64struct kthread_work {
65 struct list_head node;
66 kthread_work_func_t func;
67 wait_queue_head_t done;
68 atomic_t flushing;
69 int queue_seq;
70 int done_seq;
71};
72
73#define KTHREAD_WORKER_INIT(worker) { \
Thomas Gleixner92578c0b2011-01-23 15:24:55 +010074 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
Tejun Heob56c0d82010-06-29 10:07:09 +020075 .work_list = LIST_HEAD_INIT((worker).work_list), \
76 }
77
78#define KTHREAD_WORK_INIT(work, fn) { \
79 .node = LIST_HEAD_INIT((work).node), \
80 .func = (fn), \
81 .done = __WAIT_QUEUE_HEAD_INITIALIZER((work).done), \
82 .flushing = ATOMIC_INIT(0), \
83 }
84
85#define DEFINE_KTHREAD_WORKER(worker) \
86 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
87
88#define DEFINE_KTHREAD_WORK(work, fn) \
89 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
90
Yong Zhang4f32e9b2010-12-22 10:27:53 +010091/*
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.
95 */
96#ifdef CONFIG_LOCKDEP
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)
105#else
106# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
107# define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) DEFINE_KTHREAD_WORK(work, fn)
108#endif
Tejun Heob56c0d82010-06-29 10:07:09 +0200109
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100110extern void __init_kthread_worker(struct kthread_worker *worker,
111 const char *name, struct lock_class_key *key);
112
113#define init_kthread_worker(worker) \
114 do { \
115 static struct lock_class_key __key; \
116 __init_kthread_worker((worker), "("#worker")->lock", &__key); \
117 } while (0)
118
119#define init_kthread_work(work, fn) \
120 do { \
121 memset((work), 0, sizeof(struct kthread_work)); \
122 INIT_LIST_HEAD(&(work)->node); \
123 (work)->func = (fn); \
124 init_waitqueue_head(&(work)->done); \
125 } while (0)
Tejun Heob56c0d82010-06-29 10:07:09 +0200126
127int kthread_worker_fn(void *worker_ptr);
128
129bool queue_kthread_work(struct kthread_worker *worker,
130 struct kthread_work *work);
131void flush_kthread_work(struct kthread_work *work);
132void flush_kthread_worker(struct kthread_worker *worker);
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#endif /* _LINUX_KTHREAD_H */