blob: 8d816646f7665eab86dd9fbd10adf8256c624c9e [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
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000017struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
18 void *data,
19 unsigned int cpu,
20 const char *namefmt);
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -070023 * kthread_run - create and wake a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * @threadfn: the function to run until signal_pending(current).
25 * @data: data ptr for @threadfn.
26 * @namefmt: printf-style name for the thread.
27 *
28 * Description: Convenient wrapper for kthread_create() followed by
Randy Dunlap9e37bd32006-06-25 05:49:19 -070029 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define kthread_run(threadfn, data, namefmt, ...) \
32({ \
33 struct task_struct *__k \
34 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
35 if (!IS_ERR(__k)) \
36 wake_up_process(__k); \
37 __k; \
38})
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040void kthread_bind(struct task_struct *k, unsigned int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041int kthread_stop(struct task_struct *k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000042bool kthread_should_stop(void);
43bool kthread_should_park(void);
Tejun Heo8a32c442011-11-21 12:32:23 -080044bool kthread_freezable_should_stop(bool *was_frozen);
Tejun Heo82805ab2010-06-29 10:07:09 +020045void *kthread_data(struct task_struct *k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000046int kthread_park(struct task_struct *k);
47void kthread_unpark(struct task_struct *k);
48void kthread_parkme(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Eric W. Biederman73c27992007-05-09 02:34:32 -070050int kthreadd(void *unused);
51extern struct task_struct *kthreadd_task;
Eric Dumazet207205a2011-03-22 16:30:44 -070052extern int tsk_fork_get_node(struct task_struct *tsk);
Eric W. Biederman73c27992007-05-09 02:34:32 -070053
Tejun Heob56c0d82010-06-29 10:07:09 +020054/*
55 * Simple work processor based on kthread.
56 *
57 * This provides easier way to make use of kthreads. A kthread_work
58 * can be queued and flushed using queue/flush_kthread_work()
59 * respectively. Queued kthread_works are processed by a kthread
60 * running kthread_worker_fn().
Tejun Heob56c0d82010-06-29 10:07:09 +020061 */
62struct kthread_work;
63typedef void (*kthread_work_func_t)(struct kthread_work *work);
64
65struct kthread_worker {
66 spinlock_t lock;
67 struct list_head work_list;
68 struct task_struct *task;
Tejun Heo46f3d972012-07-19 13:52:53 -070069 struct kthread_work *current_work;
Tejun Heob56c0d82010-06-29 10:07:09 +020070};
71
72struct kthread_work {
73 struct list_head node;
74 kthread_work_func_t func;
75 wait_queue_head_t done;
Tejun Heo46f3d972012-07-19 13:52:53 -070076 struct kthread_worker *worker;
Tejun Heob56c0d82010-06-29 10:07:09 +020077};
78
79#define KTHREAD_WORKER_INIT(worker) { \
Thomas Gleixner92578c0b2011-01-23 15:24:55 +010080 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
Tejun Heob56c0d82010-06-29 10:07:09 +020081 .work_list = LIST_HEAD_INIT((worker).work_list), \
82 }
83
84#define KTHREAD_WORK_INIT(work, fn) { \
85 .node = LIST_HEAD_INIT((work).node), \
86 .func = (fn), \
87 .done = __WAIT_QUEUE_HEAD_INITIALIZER((work).done), \
Tejun Heob56c0d82010-06-29 10:07:09 +020088 }
89
90#define DEFINE_KTHREAD_WORKER(worker) \
91 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
92
93#define DEFINE_KTHREAD_WORK(work, fn) \
94 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
95
Yong Zhang4f32e9b2010-12-22 10:27:53 +010096/*
97 * kthread_worker.lock and kthread_work.done need their own lockdep class
98 * keys if they are defined on stack with lockdep enabled. Use the
99 * following macros when defining them on stack.
100 */
101#ifdef CONFIG_LOCKDEP
102# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
103 ({ init_kthread_worker(&worker); worker; })
104# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
105 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
106# define KTHREAD_WORK_INIT_ONSTACK(work, fn) \
107 ({ init_kthread_work((&work), fn); work; })
108# define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) \
109 struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn)
110#else
111# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
112# define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) DEFINE_KTHREAD_WORK(work, fn)
113#endif
Tejun Heob56c0d82010-06-29 10:07:09 +0200114
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100115extern void __init_kthread_worker(struct kthread_worker *worker,
116 const char *name, struct lock_class_key *key);
117
118#define init_kthread_worker(worker) \
119 do { \
120 static struct lock_class_key __key; \
121 __init_kthread_worker((worker), "("#worker")->lock", &__key); \
122 } while (0)
123
124#define init_kthread_work(work, fn) \
125 do { \
126 memset((work), 0, sizeof(struct kthread_work)); \
127 INIT_LIST_HEAD(&(work)->node); \
128 (work)->func = (fn); \
129 init_waitqueue_head(&(work)->done); \
130 } while (0)
Tejun Heob56c0d82010-06-29 10:07:09 +0200131
132int kthread_worker_fn(void *worker_ptr);
133
134bool queue_kthread_work(struct kthread_worker *worker,
135 struct kthread_work *work);
136void flush_kthread_work(struct kthread_work *work);
137void flush_kthread_worker(struct kthread_worker *worker);
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#endif /* _LINUX_KTHREAD_H */