diff options
-rw-r--r-- | Documentation/DocBook/kernel-api.tmpl | 2 | ||||
-rw-r--r-- | include/linux/kthread.h | 65 | ||||
-rw-r--r-- | kernel/kthread.c | 61 |
3 files changed, 65 insertions, 63 deletions
diff --git a/Documentation/DocBook/kernel-api.tmpl b/Documentation/DocBook/kernel-api.tmpl index 5a4abe0d5165..8305eb7b8c15 100644 --- a/Documentation/DocBook/kernel-api.tmpl +++ b/Documentation/DocBook/kernel-api.tmpl | |||
@@ -62,6 +62,8 @@ | |||
62 | <sect1><title>Internal Functions</title> | 62 | <sect1><title>Internal Functions</title> |
63 | !Ikernel/exit.c | 63 | !Ikernel/exit.c |
64 | !Ikernel/signal.c | 64 | !Ikernel/signal.c |
65 | !Iinclude/linux/kthread.h | ||
66 | !Ekernel/kthread.c | ||
65 | </sect1> | 67 | </sect1> |
66 | 68 | ||
67 | <sect1><title>Kernel objects manipulation</title> | 69 | <sect1><title>Kernel objects manipulation</title> |
diff --git a/include/linux/kthread.h b/include/linux/kthread.h index ebdd41fd1082..7cce5dfa092f 100644 --- a/include/linux/kthread.h +++ b/include/linux/kthread.h | |||
@@ -4,37 +4,19 @@ | |||
4 | #include <linux/err.h> | 4 | #include <linux/err.h> |
5 | #include <linux/sched.h> | 5 | #include <linux/sched.h> |
6 | 6 | ||
7 | /** | ||
8 | * kthread_create: create a kthread. | ||
9 | * @threadfn: the function to run until signal_pending(current). | ||
10 | * @data: data ptr for @threadfn. | ||
11 | * @namefmt: printf-style name for the thread. | ||
12 | * | ||
13 | * Description: This helper function creates and names a kernel | ||
14 | * thread. The thread will be stopped: use wake_up_process() to start | ||
15 | * it. See also kthread_run(), kthread_create_on_cpu(). | ||
16 | * | ||
17 | * When woken, the thread will run @threadfn() with @data as its | ||
18 | * argument. @threadfn can either call do_exit() directly if it is a | ||
19 | * standalone thread for which noone will call kthread_stop(), or | ||
20 | * return when 'kthread_should_stop()' is true (which means | ||
21 | * kthread_stop() has been called). The return value should be zero | ||
22 | * or a negative error number: it will be passed to kthread_stop(). | ||
23 | * | ||
24 | * Returns a task_struct or ERR_PTR(-ENOMEM). | ||
25 | */ | ||
26 | struct task_struct *kthread_create(int (*threadfn)(void *data), | 7 | struct task_struct *kthread_create(int (*threadfn)(void *data), |
27 | void *data, | 8 | void *data, |
28 | const char namefmt[], ...); | 9 | const char namefmt[], ...); |
29 | 10 | ||
30 | /** | 11 | /** |
31 | * kthread_run: create and wake a thread. | 12 | * kthread_run - create and wake a thread. |
32 | * @threadfn: the function to run until signal_pending(current). | 13 | * @threadfn: the function to run until signal_pending(current). |
33 | * @data: data ptr for @threadfn. | 14 | * @data: data ptr for @threadfn. |
34 | * @namefmt: printf-style name for the thread. | 15 | * @namefmt: printf-style name for the thread. |
35 | * | 16 | * |
36 | * Description: Convenient wrapper for kthread_create() followed by | 17 | * Description: Convenient wrapper for kthread_create() followed by |
37 | * wake_up_process(). Returns the kthread, or ERR_PTR(-ENOMEM). */ | 18 | * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM). |
19 | */ | ||
38 | #define kthread_run(threadfn, data, namefmt, ...) \ | 20 | #define kthread_run(threadfn, data, namefmt, ...) \ |
39 | ({ \ | 21 | ({ \ |
40 | struct task_struct *__k \ | 22 | struct task_struct *__k \ |
@@ -44,50 +26,9 @@ struct task_struct *kthread_create(int (*threadfn)(void *data), | |||
44 | __k; \ | 26 | __k; \ |
45 | }) | 27 | }) |
46 | 28 | ||
47 | /** | ||
48 | * kthread_bind: bind a just-created kthread to a cpu. | ||
49 | * @k: thread created by kthread_create(). | ||
50 | * @cpu: cpu (might not be online, must be possible) for @k to run on. | ||
51 | * | ||
52 | * Description: This function is equivalent to set_cpus_allowed(), | ||
53 | * except that @cpu doesn't need to be online, and the thread must be | ||
54 | * stopped (ie. just returned from kthread_create(). | ||
55 | */ | ||
56 | void kthread_bind(struct task_struct *k, unsigned int cpu); | 29 | void kthread_bind(struct task_struct *k, unsigned int cpu); |
57 | |||
58 | /** | ||
59 | * kthread_stop: stop a thread created by kthread_create(). | ||
60 | * @k: thread created by kthread_create(). | ||
61 | * | ||
62 | * Sets kthread_should_stop() for @k to return true, wakes it, and | ||
63 | * waits for it to exit. Your threadfn() must not call do_exit() | ||
64 | * itself if you use this function! This can also be called after | ||
65 | * kthread_create() instead of calling wake_up_process(): the thread | ||
66 | * will exit without calling threadfn(). | ||
67 | * | ||
68 | * Returns the result of threadfn(), or -EINTR if wake_up_process() | ||
69 | * was never called. */ | ||
70 | int kthread_stop(struct task_struct *k); | 30 | int kthread_stop(struct task_struct *k); |
71 | |||
72 | /** | ||
73 | * kthread_stop_sem: stop a thread created by kthread_create(). | ||
74 | * @k: thread created by kthread_create(). | ||
75 | * @s: semaphore that @k waits on while idle. | ||
76 | * | ||
77 | * Does essentially the same thing as kthread_stop() above, but wakes | ||
78 | * @k by calling up(@s). | ||
79 | * | ||
80 | * Returns the result of threadfn(), or -EINTR if wake_up_process() | ||
81 | * was never called. */ | ||
82 | int kthread_stop_sem(struct task_struct *k, struct semaphore *s); | 31 | int kthread_stop_sem(struct task_struct *k, struct semaphore *s); |
83 | |||
84 | /** | ||
85 | * kthread_should_stop: should this kthread return now? | ||
86 | * | ||
87 | * When someone calls kthread_stop on your kthread, it will be woken | ||
88 | * and this will return true. You should then return, and your return | ||
89 | * value will be passed through to kthread_stop(). | ||
90 | */ | ||
91 | int kthread_should_stop(void); | 32 | int kthread_should_stop(void); |
92 | 33 | ||
93 | #endif /* _LINUX_KTHREAD_H */ | 34 | #endif /* _LINUX_KTHREAD_H */ |
diff --git a/kernel/kthread.c b/kernel/kthread.c index c5f3c6613b6d..24be714b04c7 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c | |||
@@ -45,6 +45,13 @@ struct kthread_stop_info | |||
45 | static DEFINE_MUTEX(kthread_stop_lock); | 45 | static DEFINE_MUTEX(kthread_stop_lock); |
46 | static struct kthread_stop_info kthread_stop_info; | 46 | static struct kthread_stop_info kthread_stop_info; |
47 | 47 | ||
48 | /** | ||
49 | * kthread_should_stop - should this kthread return now? | ||
50 | * | ||
51 | * When someone calls kthread_stop on your kthread, it will be woken | ||
52 | * and this will return true. You should then return, and your return | ||
53 | * value will be passed through to kthread_stop(). | ||
54 | */ | ||
48 | int kthread_should_stop(void) | 55 | int kthread_should_stop(void) |
49 | { | 56 | { |
50 | return (kthread_stop_info.k == current); | 57 | return (kthread_stop_info.k == current); |
@@ -122,6 +129,25 @@ static void keventd_create_kthread(void *_create) | |||
122 | complete(&create->done); | 129 | complete(&create->done); |
123 | } | 130 | } |
124 | 131 | ||
132 | /** | ||
133 | * kthread_create - create a kthread. | ||
134 | * @threadfn: the function to run until signal_pending(current). | ||
135 | * @data: data ptr for @threadfn. | ||
136 | * @namefmt: printf-style name for the thread. | ||
137 | * | ||
138 | * Description: This helper function creates and names a kernel | ||
139 | * thread. The thread will be stopped: use wake_up_process() to start | ||
140 | * it. See also kthread_run(), kthread_create_on_cpu(). | ||
141 | * | ||
142 | * When woken, the thread will run @threadfn() with @data as its | ||
143 | * argument. @threadfn can either call do_exit() directly if it is a | ||
144 | * standalone thread for which noone will call kthread_stop(), or | ||
145 | * return when 'kthread_should_stop()' is true (which means | ||
146 | * kthread_stop() has been called). The return value should be zero | ||
147 | * or a negative error number; it will be passed to kthread_stop(). | ||
148 | * | ||
149 | * Returns a task_struct or ERR_PTR(-ENOMEM). | ||
150 | */ | ||
125 | struct task_struct *kthread_create(int (*threadfn)(void *data), | 151 | struct task_struct *kthread_create(int (*threadfn)(void *data), |
126 | void *data, | 152 | void *data, |
127 | const char namefmt[], | 153 | const char namefmt[], |
@@ -156,6 +182,15 @@ struct task_struct *kthread_create(int (*threadfn)(void *data), | |||
156 | } | 182 | } |
157 | EXPORT_SYMBOL(kthread_create); | 183 | EXPORT_SYMBOL(kthread_create); |
158 | 184 | ||
185 | /** | ||
186 | * kthread_bind - bind a just-created kthread to a cpu. | ||
187 | * @k: thread created by kthread_create(). | ||
188 | * @cpu: cpu (might not be online, must be possible) for @k to run on. | ||
189 | * | ||
190 | * Description: This function is equivalent to set_cpus_allowed(), | ||
191 | * except that @cpu doesn't need to be online, and the thread must be | ||
192 | * stopped (i.e., just returned from kthread_create(). | ||
193 | */ | ||
159 | void kthread_bind(struct task_struct *k, unsigned int cpu) | 194 | void kthread_bind(struct task_struct *k, unsigned int cpu) |
160 | { | 195 | { |
161 | BUG_ON(k->state != TASK_INTERRUPTIBLE); | 196 | BUG_ON(k->state != TASK_INTERRUPTIBLE); |
@@ -166,12 +201,36 @@ void kthread_bind(struct task_struct *k, unsigned int cpu) | |||
166 | } | 201 | } |
167 | EXPORT_SYMBOL(kthread_bind); | 202 | EXPORT_SYMBOL(kthread_bind); |
168 | 203 | ||
204 | /** | ||
205 | * kthread_stop - stop a thread created by kthread_create(). | ||
206 | * @k: thread created by kthread_create(). | ||
207 | * | ||
208 | * Sets kthread_should_stop() for @k to return true, wakes it, and | ||
209 | * waits for it to exit. Your threadfn() must not call do_exit() | ||
210 | * itself if you use this function! This can also be called after | ||
211 | * kthread_create() instead of calling wake_up_process(): the thread | ||
212 | * will exit without calling threadfn(). | ||
213 | * | ||
214 | * Returns the result of threadfn(), or %-EINTR if wake_up_process() | ||
215 | * was never called. | ||
216 | */ | ||
169 | int kthread_stop(struct task_struct *k) | 217 | int kthread_stop(struct task_struct *k) |
170 | { | 218 | { |
171 | return kthread_stop_sem(k, NULL); | 219 | return kthread_stop_sem(k, NULL); |
172 | } | 220 | } |
173 | EXPORT_SYMBOL(kthread_stop); | 221 | EXPORT_SYMBOL(kthread_stop); |
174 | 222 | ||
223 | /** | ||
224 | * kthread_stop_sem - stop a thread created by kthread_create(). | ||
225 | * @k: thread created by kthread_create(). | ||
226 | * @s: semaphore that @k waits on while idle. | ||
227 | * | ||
228 | * Does essentially the same thing as kthread_stop() above, but wakes | ||
229 | * @k by calling up(@s). | ||
230 | * | ||
231 | * Returns the result of threadfn(), or %-EINTR if wake_up_process() | ||
232 | * was never called. | ||
233 | */ | ||
175 | int kthread_stop_sem(struct task_struct *k, struct semaphore *s) | 234 | int kthread_stop_sem(struct task_struct *k, struct semaphore *s) |
176 | { | 235 | { |
177 | int ret; | 236 | int ret; |
@@ -210,5 +269,5 @@ static __init int helper_init(void) | |||
210 | 269 | ||
211 | return 0; | 270 | return 0; |
212 | } | 271 | } |
213 | core_initcall(helper_init); | ||
214 | 272 | ||
273 | core_initcall(helper_init); | ||