1#ifndef _LINUX_WAIT_H
2#define _LINUX_WAIT_H
3
4
5#include <linux/list.h>
6#include <linux/stddef.h>
7#include <linux/spinlock.h>
8#include <asm/current.h>
9#include <uapi/linux/wait.h>
10
11typedef struct __wait_queue wait_queue_t;
12typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
13int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
14
15struct __wait_queue {
16 unsigned int flags;
17#define WQ_FLAG_EXCLUSIVE 0x01
18 void *private;
19 wait_queue_func_t func;
20 struct list_head task_list;
21};
22
23struct wait_bit_key {
24 void *flags;
25 int bit_nr;
26};
27
28struct wait_bit_queue {
29 struct wait_bit_key key;
30 wait_queue_t wait;
31};
32
33struct __wait_queue_head {
34 spinlock_t lock;
35 struct list_head task_list;
36};
37typedef struct __wait_queue_head wait_queue_head_t;
38
39struct task_struct;
40
41
42
43
44
45#define __WAITQUEUE_INITIALIZER(name, tsk) { \
46 .private = tsk, \
47 .func = default_wake_function, \
48 .task_list = { NULL, NULL } }
49
50#define DECLARE_WAITQUEUE(name, tsk) \
51 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
52
53#define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
54 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
55 .task_list = { &(name).task_list, &(name).task_list } }
56
57#define DECLARE_WAIT_QUEUE_HEAD(name) \
58 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
59
60#define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
61 { .flags = word, .bit_nr = bit, }
62
63extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
64
65#define init_waitqueue_head(q) \
66 do { \
67 static struct lock_class_key __key; \
68 \
69 __init_waitqueue_head((q), #q, &__key); \
70 } while (0)
71
72#ifdef CONFIG_LOCKDEP
73# define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
74 ({ init_waitqueue_head(&name); name; })
75# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
76 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
77#else
78# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
79#endif
80
81static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
82{
83 q->flags = 0;
84 q->private = p;
85 q->func = default_wake_function;
86}
87
88static inline void init_waitqueue_func_entry(wait_queue_t *q,
89 wait_queue_func_t func)
90{
91 q->flags = 0;
92 q->private = NULL;
93 q->func = func;
94}
95
96static inline int waitqueue_active(wait_queue_head_t *q)
97{
98 return !list_empty(&q->task_list);
99}
100
101extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
102extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
103extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
104
105static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
106{
107 list_add(&new->task_list, &head->task_list);
108}
109
110
111
112
113static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
114 wait_queue_t *wait)
115{
116 wait->flags |= WQ_FLAG_EXCLUSIVE;
117 __add_wait_queue(q, wait);
118}
119
120static inline void __add_wait_queue_tail(wait_queue_head_t *head,
121 wait_queue_t *new)
122{
123 list_add_tail(&new->task_list, &head->task_list);
124}
125
126static inline void __add_wait_queue_tail_exclusive(wait_queue_head_t *q,
127 wait_queue_t *wait)
128{
129 wait->flags |= WQ_FLAG_EXCLUSIVE;
130 __add_wait_queue_tail(q, wait);
131}
132
133static inline void __remove_wait_queue(wait_queue_head_t *head,
134 wait_queue_t *old)
135{
136 list_del(&old->task_list);
137}
138
139void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
140void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
141void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
142 void *key);
143void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
144void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
145void __wake_up_bit(wait_queue_head_t *, void *, int);
146int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
147int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
148void wake_up_bit(void *, int);
149int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
150int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
151wait_queue_head_t *bit_waitqueue(void *, int);
152
153#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
154#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
155#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
156#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
157#define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
158
159#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
160#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
161#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
162#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
163
164
165
166
167#define wake_up_poll(x, m) \
168 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
169#define wake_up_locked_poll(x, m) \
170 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
171#define wake_up_interruptible_poll(x, m) \
172 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
173#define wake_up_interruptible_sync_poll(x, m) \
174 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
175
176#define __wait_event(wq, condition) \
177do { \
178 DEFINE_WAIT(__wait); \
179 \
180 for (;;) { \
181 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
182 if (condition) \
183 break; \
184 schedule(); \
185 } \
186 finish_wait(&wq, &__wait); \
187} while (0)
188
189
190
191
192
193
194
195
196
197
198
199
200
201#define wait_event(wq, condition) \
202do { \
203 if (condition) \
204 break; \
205 __wait_event(wq, condition); \
206} while (0)
207
208#define __wait_event_timeout(wq, condition, ret) \
209do { \
210 DEFINE_WAIT(__wait); \
211 \
212 for (;;) { \
213 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
214 if (condition) \
215 break; \
216 ret = schedule_timeout(ret); \
217 if (!ret) \
218 break; \
219 } \
220 if (!ret && (condition)) \
221 ret = 1; \
222 finish_wait(&wq, &__wait); \
223} while (0)
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242#define wait_event_timeout(wq, condition, timeout) \
243({ \
244 long __ret = timeout; \
245 if (!(condition)) \
246 __wait_event_timeout(wq, condition, __ret); \
247 __ret; \
248})
249
250#define __wait_event_interruptible(wq, condition, ret) \
251do { \
252 DEFINE_WAIT(__wait); \
253 \
254 for (;;) { \
255 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
256 if (condition) \
257 break; \
258 if (!signal_pending(current)) { \
259 schedule(); \
260 continue; \
261 } \
262 ret = -ERESTARTSYS; \
263 break; \
264 } \
265 finish_wait(&wq, &__wait); \
266} while (0)
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283#define wait_event_interruptible(wq, condition) \
284({ \
285 int __ret = 0; \
286 if (!(condition)) \
287 __wait_event_interruptible(wq, condition, __ret); \
288 __ret; \
289})
290
291#define __wait_event_interruptible_timeout(wq, condition, ret) \
292do { \
293 DEFINE_WAIT(__wait); \
294 \
295 for (;;) { \
296 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
297 if (condition) \
298 break; \
299 if (!signal_pending(current)) { \
300 ret = schedule_timeout(ret); \
301 if (!ret) \
302 break; \
303 continue; \
304 } \
305 ret = -ERESTARTSYS; \
306 break; \
307 } \
308 if (!ret && (condition)) \
309 ret = 1; \
310 finish_wait(&wq, &__wait); \
311} while (0)
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331#define wait_event_interruptible_timeout(wq, condition, timeout) \
332({ \
333 long __ret = timeout; \
334 if (!(condition)) \
335 __wait_event_interruptible_timeout(wq, condition, __ret); \
336 __ret; \
337})
338
339#define __wait_event_hrtimeout(wq, condition, timeout, state) \
340({ \
341 int __ret = 0; \
342 DEFINE_WAIT(__wait); \
343 struct hrtimer_sleeper __t; \
344 \
345 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
346 HRTIMER_MODE_REL); \
347 hrtimer_init_sleeper(&__t, current); \
348 if ((timeout).tv64 != KTIME_MAX) \
349 hrtimer_start_range_ns(&__t.timer, timeout, \
350 current->timer_slack_ns, \
351 HRTIMER_MODE_REL); \
352 \
353 for (;;) { \
354 prepare_to_wait(&wq, &__wait, state); \
355 if (condition) \
356 break; \
357 if (state == TASK_INTERRUPTIBLE && \
358 signal_pending(current)) { \
359 __ret = -ERESTARTSYS; \
360 break; \
361 } \
362 if (!__t.task) { \
363 __ret = -ETIME; \
364 break; \
365 } \
366 schedule(); \
367 } \
368 \
369 hrtimer_cancel(&__t.timer); \
370 destroy_hrtimer_on_stack(&__t.timer); \
371 finish_wait(&wq, &__wait); \
372 __ret; \
373})
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391#define wait_event_hrtimeout(wq, condition, timeout) \
392({ \
393 int __ret = 0; \
394 if (!(condition)) \
395 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
396 TASK_UNINTERRUPTIBLE); \
397 __ret; \
398})
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416#define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
417({ \
418 long __ret = 0; \
419 if (!(condition)) \
420 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
421 TASK_INTERRUPTIBLE); \
422 __ret; \
423})
424
425#define __wait_event_interruptible_exclusive(wq, condition, ret) \
426do { \
427 DEFINE_WAIT(__wait); \
428 \
429 for (;;) { \
430 prepare_to_wait_exclusive(&wq, &__wait, \
431 TASK_INTERRUPTIBLE); \
432 if (condition) { \
433 finish_wait(&wq, &__wait); \
434 break; \
435 } \
436 if (!signal_pending(current)) { \
437 schedule(); \
438 continue; \
439 } \
440 ret = -ERESTARTSYS; \
441 abort_exclusive_wait(&wq, &__wait, \
442 TASK_INTERRUPTIBLE, NULL); \
443 break; \
444 } \
445} while (0)
446
447#define wait_event_interruptible_exclusive(wq, condition) \
448({ \
449 int __ret = 0; \
450 if (!(condition)) \
451 __wait_event_interruptible_exclusive(wq, condition, __ret);\
452 __ret; \
453})
454
455
456#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
457({ \
458 int __ret = 0; \
459 DEFINE_WAIT(__wait); \
460 if (exclusive) \
461 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
462 do { \
463 if (likely(list_empty(&__wait.task_list))) \
464 __add_wait_queue_tail(&(wq), &__wait); \
465 set_current_state(TASK_INTERRUPTIBLE); \
466 if (signal_pending(current)) { \
467 __ret = -ERESTARTSYS; \
468 break; \
469 } \
470 if (irq) \
471 spin_unlock_irq(&(wq).lock); \
472 else \
473 spin_unlock(&(wq).lock); \
474 schedule(); \
475 if (irq) \
476 spin_lock_irq(&(wq).lock); \
477 else \
478 spin_lock(&(wq).lock); \
479 } while (!(condition)); \
480 __remove_wait_queue(&(wq), &__wait); \
481 __set_current_state(TASK_RUNNING); \
482 __ret; \
483})
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509#define wait_event_interruptible_locked(wq, condition) \
510 ((condition) \
511 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536#define wait_event_interruptible_locked_irq(wq, condition) \
537 ((condition) \
538 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567#define wait_event_interruptible_exclusive_locked(wq, condition) \
568 ((condition) \
569 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
599 ((condition) \
600 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
601
602
603
604#define __wait_event_killable(wq, condition, ret) \
605do { \
606 DEFINE_WAIT(__wait); \
607 \
608 for (;;) { \
609 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
610 if (condition) \
611 break; \
612 if (!fatal_signal_pending(current)) { \
613 schedule(); \
614 continue; \
615 } \
616 ret = -ERESTARTSYS; \
617 break; \
618 } \
619 finish_wait(&wq, &__wait); \
620} while (0)
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637#define wait_event_killable(wq, condition) \
638({ \
639 int __ret = 0; \
640 if (!(condition)) \
641 __wait_event_killable(wq, condition, __ret); \
642 __ret; \
643})
644
645
646#define __wait_event_lock_irq(wq, condition, lock, cmd) \
647do { \
648 DEFINE_WAIT(__wait); \
649 \
650 for (;;) { \
651 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
652 if (condition) \
653 break; \
654 spin_unlock_irq(&lock); \
655 cmd; \
656 schedule(); \
657 spin_lock_irq(&lock); \
658 } \
659 finish_wait(&wq, &__wait); \
660} while (0)
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685#define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
686do { \
687 if (condition) \
688 break; \
689 __wait_event_lock_irq(wq, condition, lock, cmd); \
690} while (0)
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712#define wait_event_lock_irq(wq, condition, lock) \
713do { \
714 if (condition) \
715 break; \
716 __wait_event_lock_irq(wq, condition, lock, ); \
717} while (0)
718
719
720#define __wait_event_interruptible_lock_irq(wq, condition, \
721 lock, ret, cmd) \
722do { \
723 DEFINE_WAIT(__wait); \
724 \
725 for (;;) { \
726 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
727 if (condition) \
728 break; \
729 if (signal_pending(current)) { \
730 ret = -ERESTARTSYS; \
731 break; \
732 } \
733 spin_unlock_irq(&lock); \
734 cmd; \
735 schedule(); \
736 spin_lock_irq(&lock); \
737 } \
738 finish_wait(&wq, &__wait); \
739} while (0)
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766#define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
767({ \
768 int __ret = 0; \
769 \
770 if (!(condition)) \
771 __wait_event_interruptible_lock_irq(wq, condition, \
772 lock, __ret, cmd); \
773 __ret; \
774})
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798#define wait_event_interruptible_lock_irq(wq, condition, lock) \
799({ \
800 int __ret = 0; \
801 \
802 if (!(condition)) \
803 __wait_event_interruptible_lock_irq(wq, condition, \
804 lock, __ret, ); \
805 __ret; \
806})
807
808
809
810
811
812
813
814extern void sleep_on(wait_queue_head_t *q);
815extern long sleep_on_timeout(wait_queue_head_t *q,
816 signed long timeout);
817extern void interruptible_sleep_on(wait_queue_head_t *q);
818extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
819 signed long timeout);
820
821
822
823
824void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
825void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
826void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
827void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
828 unsigned int mode, void *key);
829int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
830int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
831
832#define DEFINE_WAIT_FUNC(name, function) \
833 wait_queue_t name = { \
834 .private = current, \
835 .func = function, \
836 .task_list = LIST_HEAD_INIT((name).task_list), \
837 }
838
839#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
840
841#define DEFINE_WAIT_BIT(name, word, bit) \
842 struct wait_bit_queue name = { \
843 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
844 .wait = { \
845 .private = current, \
846 .func = wake_bit_function, \
847 .task_list = \
848 LIST_HEAD_INIT((name).wait.task_list), \
849 }, \
850 }
851
852#define init_wait(wait) \
853 do { \
854 (wait)->private = current; \
855 (wait)->func = autoremove_wake_function; \
856 INIT_LIST_HEAD(&(wait)->task_list); \
857 (wait)->flags = 0; \
858 } while (0)
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874static inline int wait_on_bit(void *word, int bit,
875 int (*action)(void *), unsigned mode)
876{
877 if (!test_bit(bit, word))
878 return 0;
879 return out_of_line_wait_on_bit(word, bit, action, mode);
880}
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898static inline int wait_on_bit_lock(void *word, int bit,
899 int (*action)(void *), unsigned mode)
900{
901 if (!test_and_set_bit(bit, word))
902 return 0;
903 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
904}
905
906#endif
907