• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _LINUX_WAIT_H
2 #define _LINUX_WAIT_H
3 
4 #define WNOHANG		0x00000001
5 #define WUNTRACED	0x00000002
6 #define WSTOPPED	WUNTRACED
7 #define WEXITED		0x00000004
8 #define WCONTINUED	0x00000008
9 #define WNOWAIT		0x01000000	/* Don't reap, just poll status.  */
10 
11 #define __WNOTHREAD	0x20000000	/* Don't wait on children of other threads in this group */
12 #define __WALL		0x40000000	/* Wait on all children, regardless of type */
13 #define __WCLONE	0x80000000	/* Wait only on non-SIGCHLD children */
14 
15 /* First argument to waitid: */
16 #define P_ALL		0
17 #define P_PID		1
18 #define P_PGID		2
19 
20 #ifdef __KERNEL__
21 
22 #include <linux/list.h>
23 #include <linux/stddef.h>
24 #include <linux/spinlock.h>
25 #include <asm/system.h>
26 #include <asm/current.h>
27 
28 typedef struct __wait_queue wait_queue_t;
29 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int sync, void *key);
30 int default_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
31 
32 struct __wait_queue {
33 	unsigned int flags;
34 #define WQ_FLAG_EXCLUSIVE	0x01
35 	void *private;
36 	wait_queue_func_t func;
37 	struct list_head task_list;
38 };
39 
40 struct wait_bit_key {
41 	void *flags;
42 	int bit_nr;
43 };
44 
45 struct wait_bit_queue {
46 	struct wait_bit_key key;
47 	wait_queue_t wait;
48 };
49 
50 struct __wait_queue_head {
51 	spinlock_t lock;
52 	struct list_head task_list;
53 };
54 typedef struct __wait_queue_head wait_queue_head_t;
55 
56 struct task_struct;
57 
58 /*
59  * Macros for declaration and initialisaton of the datatypes
60  */
61 
62 #define __WAITQUEUE_INITIALIZER(name, tsk) {				\
63 	.private	= tsk,						\
64 	.func		= default_wake_function,			\
65 	.task_list	= { NULL, NULL } }
66 
67 #define DECLARE_WAITQUEUE(name, tsk)					\
68 	wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
69 
70 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) {				\
71 	.lock		= __SPIN_LOCK_UNLOCKED(name.lock),		\
72 	.task_list	= { &(name).task_list, &(name).task_list } }
73 
74 #define DECLARE_WAIT_QUEUE_HEAD(name) \
75 	wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
76 
77 #define __WAIT_BIT_KEY_INITIALIZER(word, bit)				\
78 	{ .flags = word, .bit_nr = bit, }
79 
80 extern void init_waitqueue_head(wait_queue_head_t *q);
81 
init_waitqueue_entry(wait_queue_t * q,struct task_struct * p)82 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
83 {
84 	q->flags = 0;
85 	q->private = p;
86 	q->func = default_wake_function;
87 }
88 
init_waitqueue_func_entry(wait_queue_t * q,wait_queue_func_t func)89 static inline void init_waitqueue_func_entry(wait_queue_t *q,
90 					wait_queue_func_t func)
91 {
92 	q->flags = 0;
93 	q->private = NULL;
94 	q->func = func;
95 }
96 
waitqueue_active(wait_queue_head_t * q)97 static inline int waitqueue_active(wait_queue_head_t *q)
98 {
99 	return !list_empty(&q->task_list);
100 }
101 
102 /*
103  * Used to distinguish between sync and async io wait context:
104  * sync i/o typically specifies a NULL wait queue entry or a wait
105  * queue entry bound to a task (current task) to wake up.
106  * aio specifies a wait queue entry with an async notification
107  * callback routine, not associated with any task.
108  */
109 #define is_sync_wait(wait)	(!(wait) || ((wait)->private))
110 
111 extern void FASTCALL(add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));
112 extern void FASTCALL(add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait));
113 extern void FASTCALL(remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));
114 
__add_wait_queue(wait_queue_head_t * head,wait_queue_t * new)115 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
116 {
117 	list_add(&new->task_list, &head->task_list);
118 }
119 
120 /*
121  * Used for wake-one threads:
122  */
__add_wait_queue_tail(wait_queue_head_t * head,wait_queue_t * new)123 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
124 						wait_queue_t *new)
125 {
126 	list_add_tail(&new->task_list, &head->task_list);
127 }
128 
__remove_wait_queue(wait_queue_head_t * head,wait_queue_t * old)129 static inline void __remove_wait_queue(wait_queue_head_t *head,
130 							wait_queue_t *old)
131 {
132 	list_del(&old->task_list);
133 }
134 
135 void FASTCALL(__wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key));
136 extern void FASTCALL(__wake_up_locked(wait_queue_head_t *q, unsigned int mode));
137 extern void FASTCALL(__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr));
138 void FASTCALL(__wake_up_bit(wait_queue_head_t *, void *, int));
139 int FASTCALL(__wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned));
140 int FASTCALL(__wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned));
141 void FASTCALL(wake_up_bit(void *, int));
142 int FASTCALL(out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned));
143 int FASTCALL(out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned));
144 wait_queue_head_t *FASTCALL(bit_waitqueue(void *, int));
145 
146 #define wake_up(x)			__wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1, NULL)
147 #define wake_up_nr(x, nr)		__wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, nr, NULL)
148 #define wake_up_all(x)			__wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 0, NULL)
149 #define wake_up_interruptible(x)	__wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
150 #define wake_up_interruptible_nr(x, nr)	__wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
151 #define wake_up_interruptible_all(x)	__wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
152 #define	wake_up_locked(x)		__wake_up_locked((x), TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE)
153 #define wake_up_interruptible_sync(x)   __wake_up_sync((x),TASK_INTERRUPTIBLE, 1)
154 
155 #define __wait_event(wq, condition) 					\
156 do {									\
157 	DEFINE_WAIT(__wait);						\
158 									\
159 	for (;;) {							\
160 		prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);	\
161 		if (condition)						\
162 			break;						\
163 		schedule();						\
164 	}								\
165 	finish_wait(&wq, &__wait);					\
166 } while (0)
167 
168 /**
169  * wait_event - sleep until a condition gets true
170  * @wq: the waitqueue to wait on
171  * @condition: a C expression for the event to wait for
172  *
173  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
174  * @condition evaluates to true. The @condition is checked each time
175  * the waitqueue @wq is woken up.
176  *
177  * wake_up() has to be called after changing any variable that could
178  * change the result of the wait condition.
179  */
180 #define wait_event(wq, condition) 					\
181 do {									\
182 	if (condition)	 						\
183 		break;							\
184 	__wait_event(wq, condition);					\
185 } while (0)
186 
187 #define __wait_event_timeout(wq, condition, ret)			\
188 do {									\
189 	DEFINE_WAIT(__wait);						\
190 									\
191 	for (;;) {							\
192 		prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);	\
193 		if (condition)						\
194 			break;						\
195 		ret = schedule_timeout(ret);				\
196 		if (!ret)						\
197 			break;						\
198 	}								\
199 	finish_wait(&wq, &__wait);					\
200 } while (0)
201 
202 /**
203  * wait_event_timeout - sleep until a condition gets true or a timeout elapses
204  * @wq: the waitqueue to wait on
205  * @condition: a C expression for the event to wait for
206  * @timeout: timeout, in jiffies
207  *
208  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
209  * @condition evaluates to true. The @condition is checked each time
210  * the waitqueue @wq is woken up.
211  *
212  * wake_up() has to be called after changing any variable that could
213  * change the result of the wait condition.
214  *
215  * The function returns 0 if the @timeout elapsed, and the remaining
216  * jiffies if the condition evaluated to true before the timeout elapsed.
217  */
218 #define wait_event_timeout(wq, condition, timeout)			\
219 ({									\
220 	long __ret = timeout;						\
221 	if (!(condition)) 						\
222 		__wait_event_timeout(wq, condition, __ret);		\
223 	__ret;								\
224 })
225 
226 #define __wait_event_interruptible(wq, condition, ret)			\
227 do {									\
228 	DEFINE_WAIT(__wait);						\
229 									\
230 	for (;;) {							\
231 		prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);	\
232 		if (condition)						\
233 			break;						\
234 		if (!signal_pending(current)) {				\
235 			schedule();					\
236 			continue;					\
237 		}							\
238 		ret = -ERESTARTSYS;					\
239 		break;							\
240 	}								\
241 	finish_wait(&wq, &__wait);					\
242 } while (0)
243 
244 /**
245  * wait_event_interruptible - sleep until a condition gets true
246  * @wq: the waitqueue to wait on
247  * @condition: a C expression for the event to wait for
248  *
249  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
250  * @condition evaluates to true or a signal is received.
251  * The @condition is checked each time the waitqueue @wq is woken up.
252  *
253  * wake_up() has to be called after changing any variable that could
254  * change the result of the wait condition.
255  *
256  * The function will return -ERESTARTSYS if it was interrupted by a
257  * signal and 0 if @condition evaluated to true.
258  */
259 #define wait_event_interruptible(wq, condition)				\
260 ({									\
261 	int __ret = 0;							\
262 	if (!(condition))						\
263 		__wait_event_interruptible(wq, condition, __ret);	\
264 	__ret;								\
265 })
266 
267 #define __wait_event_interruptible_timeout(wq, condition, ret)		\
268 do {									\
269 	DEFINE_WAIT(__wait);						\
270 									\
271 	for (;;) {							\
272 		prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);	\
273 		if (condition)						\
274 			break;						\
275 		if (!signal_pending(current)) {				\
276 			ret = schedule_timeout(ret);			\
277 			if (!ret)					\
278 				break;					\
279 			continue;					\
280 		}							\
281 		ret = -ERESTARTSYS;					\
282 		break;							\
283 	}								\
284 	finish_wait(&wq, &__wait);					\
285 } while (0)
286 
287 /**
288  * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
289  * @wq: the waitqueue to wait on
290  * @condition: a C expression for the event to wait for
291  * @timeout: timeout, in jiffies
292  *
293  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
294  * @condition evaluates to true or a signal is received.
295  * The @condition is checked each time the waitqueue @wq is woken up.
296  *
297  * wake_up() has to be called after changing any variable that could
298  * change the result of the wait condition.
299  *
300  * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
301  * was interrupted by a signal, and the remaining jiffies otherwise
302  * if the condition evaluated to true before the timeout elapsed.
303  */
304 #define wait_event_interruptible_timeout(wq, condition, timeout)	\
305 ({									\
306 	long __ret = timeout;						\
307 	if (!(condition))						\
308 		__wait_event_interruptible_timeout(wq, condition, __ret); \
309 	__ret;								\
310 })
311 
312 #define __wait_event_interruptible_exclusive(wq, condition, ret)	\
313 do {									\
314 	DEFINE_WAIT(__wait);						\
315 									\
316 	for (;;) {							\
317 		prepare_to_wait_exclusive(&wq, &__wait,			\
318 					TASK_INTERRUPTIBLE);		\
319 		if (condition)						\
320 			break;						\
321 		if (!signal_pending(current)) {				\
322 			schedule();					\
323 			continue;					\
324 		}							\
325 		ret = -ERESTARTSYS;					\
326 		break;							\
327 	}								\
328 	finish_wait(&wq, &__wait);					\
329 } while (0)
330 
331 #define wait_event_interruptible_exclusive(wq, condition)		\
332 ({									\
333 	int __ret = 0;							\
334 	if (!(condition))						\
335 		__wait_event_interruptible_exclusive(wq, condition, __ret);\
336 	__ret;								\
337 })
338 
339 /*
340  * Must be called with the spinlock in the wait_queue_head_t held.
341  */
add_wait_queue_exclusive_locked(wait_queue_head_t * q,wait_queue_t * wait)342 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
343 						   wait_queue_t * wait)
344 {
345 	wait->flags |= WQ_FLAG_EXCLUSIVE;
346 	__add_wait_queue_tail(q,  wait);
347 }
348 
349 /*
350  * Must be called with the spinlock in the wait_queue_head_t held.
351  */
remove_wait_queue_locked(wait_queue_head_t * q,wait_queue_t * wait)352 static inline void remove_wait_queue_locked(wait_queue_head_t *q,
353 					    wait_queue_t * wait)
354 {
355 	__remove_wait_queue(q,  wait);
356 }
357 
358 /*
359  * These are the old interfaces to sleep waiting for an event.
360  * They are racy.  DO NOT use them, use the wait_event* interfaces above.
361  * We plan to remove these interfaces during 2.7.
362  */
363 extern void FASTCALL(sleep_on(wait_queue_head_t *q));
364 extern long FASTCALL(sleep_on_timeout(wait_queue_head_t *q,
365 				      signed long timeout));
366 extern void FASTCALL(interruptible_sleep_on(wait_queue_head_t *q));
367 extern long FASTCALL(interruptible_sleep_on_timeout(wait_queue_head_t *q,
368 						    signed long timeout));
369 
370 /*
371  * Waitqueues which are removed from the waitqueue_head at wakeup time
372  */
373 void FASTCALL(prepare_to_wait(wait_queue_head_t *q,
374 				wait_queue_t *wait, int state));
375 void FASTCALL(prepare_to_wait_exclusive(wait_queue_head_t *q,
376 				wait_queue_t *wait, int state));
377 void FASTCALL(finish_wait(wait_queue_head_t *q, wait_queue_t *wait));
378 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
379 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
380 
381 #define DEFINE_WAIT(name)						\
382 	wait_queue_t name = {						\
383 		.private	= current,				\
384 		.func		= autoremove_wake_function,		\
385 		.task_list	= LIST_HEAD_INIT((name).task_list),	\
386 	}
387 
388 #define DEFINE_WAIT_BIT(name, word, bit)				\
389 	struct wait_bit_queue name = {					\
390 		.key = __WAIT_BIT_KEY_INITIALIZER(word, bit),		\
391 		.wait	= {						\
392 			.private	= current,			\
393 			.func		= wake_bit_function,		\
394 			.task_list	=				\
395 				LIST_HEAD_INIT((name).wait.task_list),	\
396 		},							\
397 	}
398 
399 #define init_wait(wait)							\
400 	do {								\
401 		(wait)->private = current;				\
402 		(wait)->func = autoremove_wake_function;		\
403 		INIT_LIST_HEAD(&(wait)->task_list);			\
404 	} while (0)
405 
406 /**
407  * wait_on_bit - wait for a bit to be cleared
408  * @word: the word being waited on, a kernel virtual address
409  * @bit: the bit of the word being waited on
410  * @action: the function used to sleep, which may take special actions
411  * @mode: the task state to sleep in
412  *
413  * There is a standard hashed waitqueue table for generic use. This
414  * is the part of the hashtable's accessor API that waits on a bit.
415  * For instance, if one were to have waiters on a bitflag, one would
416  * call wait_on_bit() in threads waiting for the bit to clear.
417  * One uses wait_on_bit() where one is waiting for the bit to clear,
418  * but has no intention of setting it.
419  */
wait_on_bit(void * word,int bit,int (* action)(void *),unsigned mode)420 static inline int wait_on_bit(void *word, int bit,
421 				int (*action)(void *), unsigned mode)
422 {
423 	if (!test_bit(bit, word))
424 		return 0;
425 	return out_of_line_wait_on_bit(word, bit, action, mode);
426 }
427 
428 /**
429  * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
430  * @word: the word being waited on, a kernel virtual address
431  * @bit: the bit of the word being waited on
432  * @action: the function used to sleep, which may take special actions
433  * @mode: the task state to sleep in
434  *
435  * There is a standard hashed waitqueue table for generic use. This
436  * is the part of the hashtable's accessor API that waits on a bit
437  * when one intends to set it, for instance, trying to lock bitflags.
438  * For instance, if one were to have waiters trying to set bitflag
439  * and waiting for it to clear before setting it, one would call
440  * wait_on_bit() in threads waiting to be able to set the bit.
441  * One uses wait_on_bit_lock() where one is waiting for the bit to
442  * clear with the intention of setting it, and when done, clearing it.
443  */
wait_on_bit_lock(void * word,int bit,int (* action)(void *),unsigned mode)444 static inline int wait_on_bit_lock(void *word, int bit,
445 				int (*action)(void *), unsigned mode)
446 {
447 	if (!test_and_set_bit(bit, word))
448 		return 0;
449 	return out_of_line_wait_on_bit_lock(word, bit, action, mode);
450 }
451 
452 #endif /* __KERNEL__ */
453 
454 #endif
455