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