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
10
11#define __WNOTHREAD 0x20000000
12#define __WALL 0x40000000
13#define __WCLONE 0x80000000
14
15
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
28typedef struct __wait_queue wait_queue_t;
29typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int sync, void *key);
30int default_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
31
32struct __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
40struct wait_bit_key {
41 void *flags;
42 int bit_nr;
43};
44
45struct wait_bit_queue {
46 struct wait_bit_key key;
47 wait_queue_t wait;
48};
49
50struct __wait_queue_head {
51 spinlock_t lock;
52 struct list_head task_list;
53};
54typedef struct __wait_queue_head wait_queue_head_t;
55
56struct task_struct;
57
58
59
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
80extern void init_waitqueue_head(wait_queue_head_t *q);
81
82#ifdef CONFIG_LOCKDEP
83# define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
84 ({ init_waitqueue_head(&name); name; })
85# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
86 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
87#else
88# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
89#endif
90
91static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
92{
93 q->flags = 0;
94 q->private = p;
95 q->func = default_wake_function;
96}
97
98static inline void init_waitqueue_func_entry(wait_queue_t *q,
99 wait_queue_func_t func)
100{
101 q->flags = 0;
102 q->private = NULL;
103 q->func = func;
104}
105
106static inline int waitqueue_active(wait_queue_head_t *q)
107{
108 return !list_empty(&q->task_list);
109}
110
111extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
112extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
113extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
114
115static 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
122
123static 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
129static inline void __remove_wait_queue(wait_queue_head_t *head,
130 wait_queue_t *old)
131{
132 list_del(&old->task_list);
133}
134
135void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
136 int nr_exclusive, int sync, void *key);
137void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
138extern void __wake_up_locked(wait_queue_head_t *q, unsigned int mode);
139extern void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
140void __wake_up_bit(wait_queue_head_t *, void *, int);
141int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
142int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
143void wake_up_bit(void *, int);
144int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
145int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
146wait_queue_head_t *bit_waitqueue(void *, int);
147
148#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
149#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
150#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
151#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL)
152
153#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
154#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
155#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
156#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
157
158#ifdef CONFIG_DEBUG_LOCK_ALLOC
159
160
161
162#define wake_up_nested(x, s) \
163do { \
164 unsigned long flags; \
165 \
166 spin_lock_irqsave_nested(&(x)->lock, flags, (s)); \
167 wake_up_locked(x); \
168 spin_unlock_irqrestore(&(x)->lock, flags); \
169} while (0)
170#else
171#define wake_up_nested(x, s) wake_up(x)
172#endif
173
174#define __wait_event(wq, condition) \
175do { \
176 DEFINE_WAIT(__wait); \
177 \
178 for (;;) { \
179 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
180 if (condition) \
181 break; \
182 schedule(); \
183 } \
184 finish_wait(&wq, &__wait); \
185} while (0)
186
187
188
189
190
191
192
193
194
195
196
197
198
199#define wait_event(wq, condition) \
200do { \
201 if (condition) \
202 break; \
203 __wait_event(wq, condition); \
204} while (0)
205
206#define __wait_event_timeout(wq, condition, ret) \
207do { \
208 DEFINE_WAIT(__wait); \
209 \
210 for (;;) { \
211 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
212 if (condition) \
213 break; \
214 ret = schedule_timeout(ret); \
215 if (!ret) \
216 break; \
217 } \
218 finish_wait(&wq, &__wait); \
219} while (0)
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237#define wait_event_timeout(wq, condition, timeout) \
238({ \
239 long __ret = timeout; \
240 if (!(condition)) \
241 __wait_event_timeout(wq, condition, __ret); \
242 __ret; \
243})
244
245#define __wait_event_interruptible(wq, condition, ret) \
246do { \
247 DEFINE_WAIT(__wait); \
248 \
249 for (;;) { \
250 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
251 if (condition) \
252 break; \
253 if (!signal_pending(current)) { \
254 schedule(); \
255 continue; \
256 } \
257 ret = -ERESTARTSYS; \
258 break; \
259 } \
260 finish_wait(&wq, &__wait); \
261} while (0)
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278#define wait_event_interruptible(wq, condition) \
279({ \
280 int __ret = 0; \
281 if (!(condition)) \
282 __wait_event_interruptible(wq, condition, __ret); \
283 __ret; \
284})
285
286#define __wait_event_interruptible_timeout(wq, condition, ret) \
287do { \
288 DEFINE_WAIT(__wait); \
289 \
290 for (;;) { \
291 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
292 if (condition) \
293 break; \
294 if (!signal_pending(current)) { \
295 ret = schedule_timeout(ret); \
296 if (!ret) \
297 break; \
298 continue; \
299 } \
300 ret = -ERESTARTSYS; \
301 break; \
302 } \
303 finish_wait(&wq, &__wait); \
304} while (0)
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323#define wait_event_interruptible_timeout(wq, condition, timeout) \
324({ \
325 long __ret = timeout; \
326 if (!(condition)) \
327 __wait_event_interruptible_timeout(wq, condition, __ret); \
328 __ret; \
329})
330
331#define __wait_event_interruptible_exclusive(wq, condition, ret) \
332do { \
333 DEFINE_WAIT(__wait); \
334 \
335 for (;;) { \
336 prepare_to_wait_exclusive(&wq, &__wait, \
337 TASK_INTERRUPTIBLE); \
338 if (condition) { \
339 finish_wait(&wq, &__wait); \
340 break; \
341 } \
342 if (!signal_pending(current)) { \
343 schedule(); \
344 continue; \
345 } \
346 ret = -ERESTARTSYS; \
347 abort_exclusive_wait(&wq, &__wait, \
348 TASK_INTERRUPTIBLE, NULL); \
349 break; \
350 } \
351} while (0)
352
353#define wait_event_interruptible_exclusive(wq, condition) \
354({ \
355 int __ret = 0; \
356 if (!(condition)) \
357 __wait_event_interruptible_exclusive(wq, condition, __ret);\
358 __ret; \
359})
360
361#define __wait_event_killable(wq, condition, ret) \
362do { \
363 DEFINE_WAIT(__wait); \
364 \
365 for (;;) { \
366 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
367 if (condition) \
368 break; \
369 if (!fatal_signal_pending(current)) { \
370 schedule(); \
371 continue; \
372 } \
373 ret = -ERESTARTSYS; \
374 break; \
375 } \
376 finish_wait(&wq, &__wait); \
377} while (0)
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394#define wait_event_killable(wq, condition) \
395({ \
396 int __ret = 0; \
397 if (!(condition)) \
398 __wait_event_killable(wq, condition, __ret); \
399 __ret; \
400})
401
402
403
404
405static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
406 wait_queue_t * wait)
407{
408 wait->flags |= WQ_FLAG_EXCLUSIVE;
409 __add_wait_queue_tail(q, wait);
410}
411
412
413
414
415static inline void remove_wait_queue_locked(wait_queue_head_t *q,
416 wait_queue_t * wait)
417{
418 __remove_wait_queue(q, wait);
419}
420
421
422
423
424
425
426extern void sleep_on(wait_queue_head_t *q);
427extern long sleep_on_timeout(wait_queue_head_t *q,
428 signed long timeout);
429extern void interruptible_sleep_on(wait_queue_head_t *q);
430extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
431 signed long timeout);
432
433
434
435
436void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
437void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
438void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
439void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
440 unsigned int mode, void *key);
441int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
442int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
443
444#define DEFINE_WAIT(name) \
445 wait_queue_t name = { \
446 .private = current, \
447 .func = autoremove_wake_function, \
448 .task_list = LIST_HEAD_INIT((name).task_list), \
449 }
450
451#define DEFINE_WAIT_BIT(name, word, bit) \
452 struct wait_bit_queue name = { \
453 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
454 .wait = { \
455 .private = current, \
456 .func = wake_bit_function, \
457 .task_list = \
458 LIST_HEAD_INIT((name).wait.task_list), \
459 }, \
460 }
461
462#define init_wait(wait) \
463 do { \
464 (wait)->private = current; \
465 (wait)->func = autoremove_wake_function; \
466 INIT_LIST_HEAD(&(wait)->task_list); \
467 } while (0)
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483static inline int wait_on_bit(void *word, int bit,
484 int (*action)(void *), unsigned mode)
485{
486 if (!test_bit(bit, word))
487 return 0;
488 return out_of_line_wait_on_bit(word, bit, action, mode);
489}
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507static inline int wait_on_bit_lock(void *word, int bit,
508 int (*action)(void *), unsigned mode)
509{
510 if (!test_and_set_bit(bit, word))
511 return 0;
512 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
513}
514
515#endif
516
517#endif
518