GNU Linux-libre 5.4.241-gnu1
[releases.git] / include / linux / wait.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_WAIT_H
3 #define _LINUX_WAIT_H
4 /*
5  * Linux wait queue related types and methods
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
14 typedef struct wait_queue_entry wait_queue_entry_t;
15
16 typedef int (*wait_queue_func_t)(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
17 int default_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
18
19 /* wait_queue_entry::flags */
20 #define WQ_FLAG_EXCLUSIVE       0x01
21 #define WQ_FLAG_WOKEN           0x02
22 #define WQ_FLAG_BOOKMARK        0x04
23
24 /*
25  * A single wait-queue entry structure:
26  */
27 struct wait_queue_entry {
28         unsigned int            flags;
29         void                    *private;
30         wait_queue_func_t       func;
31         struct list_head        entry;
32 };
33
34 struct wait_queue_head {
35         spinlock_t              lock;
36         struct list_head        head;
37 };
38 typedef struct wait_queue_head wait_queue_head_t;
39
40 struct task_struct;
41
42 /*
43  * Macros for declaration and initialisaton of the datatypes
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
61 extern 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
79 static 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
86 static inline void
87 init_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  * waitqueue_active -- locklessly test for waiters on the queue
96  * @wq_head: the waitqueue to test for waiters
97  *
98  * returns true if the wait list is not empty
99  *
100  * NOTE: this function is lockless and requires care, incorrect usage _will_
101  * lead to sporadic and non-obvious failure.
102  *
103  * Use either while holding wait_queue_head::lock or when used for wakeups
104  * with an extra smp_mb() like::
105  *
106  *      CPU0 - waker                    CPU1 - waiter
107  *
108  *                                      for (;;) {
109  *      @cond = true;                     prepare_to_wait(&wq_head, &wait, state);
110  *      smp_mb();                         // smp_mb() from set_current_state()
111  *      if (waitqueue_active(wq_head))         if (@cond)
112  *        wake_up(wq_head);                      break;
113  *                                        schedule();
114  *                                      }
115  *                                      finish_wait(&wq_head, &wait);
116  *
117  * Because without the explicit smp_mb() it's possible for the
118  * waitqueue_active() load to get hoisted over the @cond store such that we'll
119  * observe an empty wait list while the waiter might not observe @cond.
120  *
121  * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
122  * which (when the lock is uncontended) are of roughly equal cost.
123  */
124 static inline int waitqueue_active(struct wait_queue_head *wq_head)
125 {
126         return !list_empty(&wq_head->head);
127 }
128
129 /**
130  * wq_has_single_sleeper - check if there is only one sleeper
131  * @wq_head: wait queue head
132  *
133  * Returns true of wq_head has only one sleeper on the list.
134  *
135  * Please refer to the comment for waitqueue_active.
136  */
137 static inline bool wq_has_single_sleeper(struct wait_queue_head *wq_head)
138 {
139         return list_is_singular(&wq_head->head);
140 }
141
142 /**
143  * wq_has_sleeper - check if there are any waiting processes
144  * @wq_head: wait queue head
145  *
146  * Returns true if wq_head has waiting processes
147  *
148  * Please refer to the comment for waitqueue_active.
149  */
150 static inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
151 {
152         /*
153          * We need to be sure we are in sync with the
154          * add_wait_queue modifications to the wait queue.
155          *
156          * This memory barrier should be paired with one on the
157          * waiting side.
158          */
159         smp_mb();
160         return waitqueue_active(wq_head);
161 }
162
163 extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
164 extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
165 extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
166
167 static inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
168 {
169         list_add(&wq_entry->entry, &wq_head->head);
170 }
171
172 /*
173  * Used for wake-one threads:
174  */
175 static inline void
176 __add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
177 {
178         wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
179         __add_wait_queue(wq_head, wq_entry);
180 }
181
182 static inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
183 {
184         list_add_tail(&wq_entry->entry, &wq_head->head);
185 }
186
187 static inline void
188 __add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
189 {
190         wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
191         __add_wait_queue_entry_tail(wq_head, wq_entry);
192 }
193
194 static inline void
195 __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
196 {
197         list_del(&wq_entry->entry);
198 }
199
200 void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
201 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
202 void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
203                 unsigned int mode, void *key, wait_queue_entry_t *bookmark);
204 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
205 void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
206 void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode, int nr);
207 void __wake_up_pollfree(struct wait_queue_head *wq_head);
208
209 #define wake_up(x)                      __wake_up(x, TASK_NORMAL, 1, NULL)
210 #define wake_up_nr(x, nr)               __wake_up(x, TASK_NORMAL, nr, NULL)
211 #define wake_up_all(x)                  __wake_up(x, TASK_NORMAL, 0, NULL)
212 #define wake_up_locked(x)               __wake_up_locked((x), TASK_NORMAL, 1)
213 #define wake_up_all_locked(x)           __wake_up_locked((x), TASK_NORMAL, 0)
214
215 #define wake_up_interruptible(x)        __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
216 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
217 #define wake_up_interruptible_all(x)    __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
218 #define wake_up_interruptible_sync(x)   __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
219
220 /*
221  * Wakeup macros to be used to report events to the targets.
222  */
223 #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
224 #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
225 #define wake_up_poll(x, m)                                                      \
226         __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
227 #define wake_up_locked_poll(x, m)                                               \
228         __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
229 #define wake_up_interruptible_poll(x, m)                                        \
230         __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
231 #define wake_up_interruptible_sync_poll(x, m)                                   \
232         __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, poll_to_key(m))
233
234 /**
235  * wake_up_pollfree - signal that a polled waitqueue is going away
236  * @wq_head: the wait queue head
237  *
238  * In the very rare cases where a ->poll() implementation uses a waitqueue whose
239  * lifetime is tied to a task rather than to the 'struct file' being polled,
240  * this function must be called before the waitqueue is freed so that
241  * non-blocking polls (e.g. epoll) are notified that the queue is going away.
242  *
243  * The caller must also RCU-delay the freeing of the wait_queue_head, e.g. via
244  * an explicit synchronize_rcu() or call_rcu(), or via SLAB_TYPESAFE_BY_RCU.
245  */
246 static inline void wake_up_pollfree(struct wait_queue_head *wq_head)
247 {
248         /*
249          * For performance reasons, we don't always take the queue lock here.
250          * Therefore, we might race with someone removing the last entry from
251          * the queue, and proceed while they still hold the queue lock.
252          * However, rcu_read_lock() is required to be held in such cases, so we
253          * can safely proceed with an RCU-delayed free.
254          */
255         if (waitqueue_active(wq_head))
256                 __wake_up_pollfree(wq_head);
257 }
258
259 #define ___wait_cond_timeout(condition)                                         \
260 ({                                                                              \
261         bool __cond = (condition);                                              \
262         if (__cond && !__ret)                                                   \
263                 __ret = 1;                                                      \
264         __cond || !__ret;                                                       \
265 })
266
267 #define ___wait_is_interruptible(state)                                         \
268         (!__builtin_constant_p(state) ||                                        \
269                 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE)          \
270
271 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
272
273 /*
274  * The below macro ___wait_event() has an explicit shadow of the __ret
275  * variable when used from the wait_event_*() macros.
276  *
277  * This is so that both can use the ___wait_cond_timeout() construct
278  * to wrap the condition.
279  *
280  * The type inconsistency of the wait_event_*() __ret variable is also
281  * on purpose; we use long where we can return timeout values and int
282  * otherwise.
283  */
284
285 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd)           \
286 ({                                                                              \
287         __label__ __out;                                                        \
288         struct wait_queue_entry __wq_entry;                                     \
289         long __ret = ret;       /* explicit shadow */                           \
290                                                                                 \
291         init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0);        \
292         for (;;) {                                                              \
293                 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
294                                                                                 \
295                 if (condition)                                                  \
296                         break;                                                  \
297                                                                                 \
298                 if (___wait_is_interruptible(state) && __int) {                 \
299                         __ret = __int;                                          \
300                         goto __out;                                             \
301                 }                                                               \
302                                                                                 \
303                 cmd;                                                            \
304         }                                                                       \
305         finish_wait(&wq_head, &__wq_entry);                                     \
306 __out:  __ret;                                                                  \
307 })
308
309 #define __wait_event(wq_head, condition)                                        \
310         (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,     \
311                             schedule())
312
313 /**
314  * wait_event - sleep until a condition gets true
315  * @wq_head: the waitqueue to wait on
316  * @condition: a C expression for the event to wait for
317  *
318  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
319  * @condition evaluates to true. The @condition is checked each time
320  * the waitqueue @wq_head is woken up.
321  *
322  * wake_up() has to be called after changing any variable that could
323  * change the result of the wait condition.
324  */
325 #define wait_event(wq_head, condition)                                          \
326 do {                                                                            \
327         might_sleep();                                                          \
328         if (condition)                                                          \
329                 break;                                                          \
330         __wait_event(wq_head, condition);                                       \
331 } while (0)
332
333 #define __io_wait_event(wq_head, condition)                                     \
334         (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,     \
335                             io_schedule())
336
337 /*
338  * io_wait_event() -- like wait_event() but with io_schedule()
339  */
340 #define io_wait_event(wq_head, condition)                                       \
341 do {                                                                            \
342         might_sleep();                                                          \
343         if (condition)                                                          \
344                 break;                                                          \
345         __io_wait_event(wq_head, condition);                                    \
346 } while (0)
347
348 #define __wait_event_freezable(wq_head, condition)                              \
349         ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,             \
350                             freezable_schedule())
351
352 /**
353  * wait_event_freezable - sleep (or freeze) until a condition gets true
354  * @wq_head: the waitqueue to wait on
355  * @condition: a C expression for the event to wait for
356  *
357  * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
358  * to system load) until the @condition evaluates to true. The
359  * @condition is checked each time the waitqueue @wq_head is woken up.
360  *
361  * wake_up() has to be called after changing any variable that could
362  * change the result of the wait condition.
363  */
364 #define wait_event_freezable(wq_head, condition)                                \
365 ({                                                                              \
366         int __ret = 0;                                                          \
367         might_sleep();                                                          \
368         if (!(condition))                                                       \
369                 __ret = __wait_event_freezable(wq_head, condition);             \
370         __ret;                                                                  \
371 })
372
373 #define __wait_event_timeout(wq_head, condition, timeout)                       \
374         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
375                       TASK_UNINTERRUPTIBLE, 0, timeout,                         \
376                       __ret = schedule_timeout(__ret))
377
378 /**
379  * wait_event_timeout - sleep until a condition gets true or a timeout elapses
380  * @wq_head: the waitqueue to wait on
381  * @condition: a C expression for the event to wait for
382  * @timeout: timeout, in jiffies
383  *
384  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
385  * @condition evaluates to true. The @condition is checked each time
386  * the waitqueue @wq_head is woken up.
387  *
388  * wake_up() has to be called after changing any variable that could
389  * change the result of the wait condition.
390  *
391  * Returns:
392  * 0 if the @condition evaluated to %false after the @timeout elapsed,
393  * 1 if the @condition evaluated to %true after the @timeout elapsed,
394  * or the remaining jiffies (at least 1) if the @condition evaluated
395  * to %true before the @timeout elapsed.
396  */
397 #define wait_event_timeout(wq_head, condition, timeout)                         \
398 ({                                                                              \
399         long __ret = timeout;                                                   \
400         might_sleep();                                                          \
401         if (!___wait_cond_timeout(condition))                                   \
402                 __ret = __wait_event_timeout(wq_head, condition, timeout);      \
403         __ret;                                                                  \
404 })
405
406 #define __wait_event_freezable_timeout(wq_head, condition, timeout)             \
407         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
408                       TASK_INTERRUPTIBLE, 0, timeout,                           \
409                       __ret = freezable_schedule_timeout(__ret))
410
411 /*
412  * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
413  * increasing load and is freezable.
414  */
415 #define wait_event_freezable_timeout(wq_head, condition, timeout)               \
416 ({                                                                              \
417         long __ret = timeout;                                                   \
418         might_sleep();                                                          \
419         if (!___wait_cond_timeout(condition))                                   \
420                 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
421         __ret;                                                                  \
422 })
423
424 #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2)              \
425         (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0,     \
426                             cmd1; schedule(); cmd2)
427 /*
428  * Just like wait_event_cmd(), except it sets exclusive flag
429  */
430 #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2)                \
431 do {                                                                            \
432         if (condition)                                                          \
433                 break;                                                          \
434         __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2);             \
435 } while (0)
436
437 #define __wait_event_cmd(wq_head, condition, cmd1, cmd2)                        \
438         (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,     \
439                             cmd1; schedule(); cmd2)
440
441 /**
442  * wait_event_cmd - sleep until a condition gets true
443  * @wq_head: the waitqueue to wait on
444  * @condition: a C expression for the event to wait for
445  * @cmd1: the command will be executed before sleep
446  * @cmd2: the command will be executed after sleep
447  *
448  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
449  * @condition evaluates to true. The @condition is checked each time
450  * the waitqueue @wq_head is woken up.
451  *
452  * wake_up() has to be called after changing any variable that could
453  * change the result of the wait condition.
454  */
455 #define wait_event_cmd(wq_head, condition, cmd1, cmd2)                          \
456 do {                                                                            \
457         if (condition)                                                          \
458                 break;                                                          \
459         __wait_event_cmd(wq_head, condition, cmd1, cmd2);                       \
460 } while (0)
461
462 #define __wait_event_interruptible(wq_head, condition)                          \
463         ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,             \
464                       schedule())
465
466 /**
467  * wait_event_interruptible - sleep until a condition gets true
468  * @wq_head: the waitqueue to wait on
469  * @condition: a C expression for the event to wait for
470  *
471  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
472  * @condition evaluates to true or a signal is received.
473  * The @condition is checked each time the waitqueue @wq_head is woken up.
474  *
475  * wake_up() has to be called after changing any variable that could
476  * change the result of the wait condition.
477  *
478  * The function will return -ERESTARTSYS if it was interrupted by a
479  * signal and 0 if @condition evaluated to true.
480  */
481 #define wait_event_interruptible(wq_head, condition)                            \
482 ({                                                                              \
483         int __ret = 0;                                                          \
484         might_sleep();                                                          \
485         if (!(condition))                                                       \
486                 __ret = __wait_event_interruptible(wq_head, condition);         \
487         __ret;                                                                  \
488 })
489
490 #define __wait_event_interruptible_timeout(wq_head, condition, timeout)         \
491         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
492                       TASK_INTERRUPTIBLE, 0, timeout,                           \
493                       __ret = schedule_timeout(__ret))
494
495 /**
496  * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
497  * @wq_head: the waitqueue to wait on
498  * @condition: a C expression for the event to wait for
499  * @timeout: timeout, in jiffies
500  *
501  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
502  * @condition evaluates to true or a signal is received.
503  * The @condition is checked each time the waitqueue @wq_head is woken up.
504  *
505  * wake_up() has to be called after changing any variable that could
506  * change the result of the wait condition.
507  *
508  * Returns:
509  * 0 if the @condition evaluated to %false after the @timeout elapsed,
510  * 1 if the @condition evaluated to %true after the @timeout elapsed,
511  * the remaining jiffies (at least 1) if the @condition evaluated
512  * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
513  * interrupted by a signal.
514  */
515 #define wait_event_interruptible_timeout(wq_head, condition, timeout)           \
516 ({                                                                              \
517         long __ret = timeout;                                                   \
518         might_sleep();                                                          \
519         if (!___wait_cond_timeout(condition))                                   \
520                 __ret = __wait_event_interruptible_timeout(wq_head,             \
521                                                 condition, timeout);            \
522         __ret;                                                                  \
523 })
524
525 #define __wait_event_hrtimeout(wq_head, condition, timeout, state)              \
526 ({                                                                              \
527         int __ret = 0;                                                          \
528         struct hrtimer_sleeper __t;                                             \
529                                                                                 \
530         hrtimer_init_sleeper_on_stack(&__t, CLOCK_MONOTONIC,                    \
531                                       HRTIMER_MODE_REL);                        \
532         if ((timeout) != KTIME_MAX) {                                           \
533                 hrtimer_set_expires_range_ns(&__t.timer, timeout,               \
534                                         current->timer_slack_ns);               \
535                 hrtimer_sleeper_start_expires(&__t, HRTIMER_MODE_REL);          \
536         }                                                                       \
537                                                                                 \
538         __ret = ___wait_event(wq_head, condition, state, 0, 0,                  \
539                 if (!__t.task) {                                                \
540                         __ret = -ETIME;                                         \
541                         break;                                                  \
542                 }                                                               \
543                 schedule());                                                    \
544                                                                                 \
545         hrtimer_cancel(&__t.timer);                                             \
546         destroy_hrtimer_on_stack(&__t.timer);                                   \
547         __ret;                                                                  \
548 })
549
550 /**
551  * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
552  * @wq_head: the waitqueue to wait on
553  * @condition: a C expression for the event to wait for
554  * @timeout: timeout, as a ktime_t
555  *
556  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
557  * @condition evaluates to true or a signal is received.
558  * The @condition is checked each time the waitqueue @wq_head is woken up.
559  *
560  * wake_up() has to be called after changing any variable that could
561  * change the result of the wait condition.
562  *
563  * The function returns 0 if @condition became true, or -ETIME if the timeout
564  * elapsed.
565  */
566 #define wait_event_hrtimeout(wq_head, condition, timeout)                       \
567 ({                                                                              \
568         int __ret = 0;                                                          \
569         might_sleep();                                                          \
570         if (!(condition))                                                       \
571                 __ret = __wait_event_hrtimeout(wq_head, condition, timeout,     \
572                                                TASK_UNINTERRUPTIBLE);           \
573         __ret;                                                                  \
574 })
575
576 /**
577  * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
578  * @wq: the waitqueue to wait on
579  * @condition: a C expression for the event to wait for
580  * @timeout: timeout, as a ktime_t
581  *
582  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
583  * @condition evaluates to true or a signal is received.
584  * The @condition is checked each time the waitqueue @wq is woken up.
585  *
586  * wake_up() has to be called after changing any variable that could
587  * change the result of the wait condition.
588  *
589  * The function returns 0 if @condition became true, -ERESTARTSYS if it was
590  * interrupted by a signal, or -ETIME if the timeout elapsed.
591  */
592 #define wait_event_interruptible_hrtimeout(wq, condition, timeout)              \
593 ({                                                                              \
594         long __ret = 0;                                                         \
595         might_sleep();                                                          \
596         if (!(condition))                                                       \
597                 __ret = __wait_event_hrtimeout(wq, condition, timeout,          \
598                                                TASK_INTERRUPTIBLE);             \
599         __ret;                                                                  \
600 })
601
602 #define __wait_event_interruptible_exclusive(wq, condition)                     \
603         ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,                  \
604                       schedule())
605
606 #define wait_event_interruptible_exclusive(wq, condition)                       \
607 ({                                                                              \
608         int __ret = 0;                                                          \
609         might_sleep();                                                          \
610         if (!(condition))                                                       \
611                 __ret = __wait_event_interruptible_exclusive(wq, condition);    \
612         __ret;                                                                  \
613 })
614
615 #define __wait_event_killable_exclusive(wq, condition)                          \
616         ___wait_event(wq, condition, TASK_KILLABLE, 1, 0,                       \
617                       schedule())
618
619 #define wait_event_killable_exclusive(wq, condition)                            \
620 ({                                                                              \
621         int __ret = 0;                                                          \
622         might_sleep();                                                          \
623         if (!(condition))                                                       \
624                 __ret = __wait_event_killable_exclusive(wq, condition);         \
625         __ret;                                                                  \
626 })
627
628
629 #define __wait_event_freezable_exclusive(wq, condition)                         \
630         ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,                  \
631                         freezable_schedule())
632
633 #define wait_event_freezable_exclusive(wq, condition)                           \
634 ({                                                                              \
635         int __ret = 0;                                                          \
636         might_sleep();                                                          \
637         if (!(condition))                                                       \
638                 __ret = __wait_event_freezable_exclusive(wq, condition);        \
639         __ret;                                                                  \
640 })
641
642 /**
643  * wait_event_idle - wait for a condition without contributing to system load
644  * @wq_head: the waitqueue to wait on
645  * @condition: a C expression for the event to wait for
646  *
647  * The process is put to sleep (TASK_IDLE) until the
648  * @condition evaluates to true.
649  * The @condition is checked each time the waitqueue @wq_head is woken up.
650  *
651  * wake_up() has to be called after changing any variable that could
652  * change the result of the wait condition.
653  *
654  */
655 #define wait_event_idle(wq_head, condition)                                     \
656 do {                                                                            \
657         might_sleep();                                                          \
658         if (!(condition))                                                       \
659                 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \
660 } while (0)
661
662 /**
663  * wait_event_idle_exclusive - wait for a condition with contributing to system load
664  * @wq_head: the waitqueue to wait on
665  * @condition: a C expression for the event to wait for
666  *
667  * The process is put to sleep (TASK_IDLE) until the
668  * @condition evaluates to true.
669  * The @condition is checked each time the waitqueue @wq_head is woken up.
670  *
671  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
672  * set thus if other processes wait on the same list, when this
673  * process is woken further processes are not considered.
674  *
675  * wake_up() has to be called after changing any variable that could
676  * change the result of the wait condition.
677  *
678  */
679 #define wait_event_idle_exclusive(wq_head, condition)                           \
680 do {                                                                            \
681         might_sleep();                                                          \
682         if (!(condition))                                                       \
683                 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \
684 } while (0)
685
686 #define __wait_event_idle_timeout(wq_head, condition, timeout)                  \
687         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
688                       TASK_IDLE, 0, timeout,                                    \
689                       __ret = schedule_timeout(__ret))
690
691 /**
692  * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
693  * @wq_head: the waitqueue to wait on
694  * @condition: a C expression for the event to wait for
695  * @timeout: timeout, in jiffies
696  *
697  * The process is put to sleep (TASK_IDLE) until the
698  * @condition evaluates to true. The @condition is checked each time
699  * the waitqueue @wq_head is woken up.
700  *
701  * wake_up() has to be called after changing any variable that could
702  * change the result of the wait condition.
703  *
704  * Returns:
705  * 0 if the @condition evaluated to %false after the @timeout elapsed,
706  * 1 if the @condition evaluated to %true after the @timeout elapsed,
707  * or the remaining jiffies (at least 1) if the @condition evaluated
708  * to %true before the @timeout elapsed.
709  */
710 #define wait_event_idle_timeout(wq_head, condition, timeout)                    \
711 ({                                                                              \
712         long __ret = timeout;                                                   \
713         might_sleep();                                                          \
714         if (!___wait_cond_timeout(condition))                                   \
715                 __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \
716         __ret;                                                                  \
717 })
718
719 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout)        \
720         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
721                       TASK_IDLE, 1, timeout,                                    \
722                       __ret = schedule_timeout(__ret))
723
724 /**
725  * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
726  * @wq_head: the waitqueue to wait on
727  * @condition: a C expression for the event to wait for
728  * @timeout: timeout, in jiffies
729  *
730  * The process is put to sleep (TASK_IDLE) until the
731  * @condition evaluates to true. The @condition is checked each time
732  * the waitqueue @wq_head is woken up.
733  *
734  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
735  * set thus if other processes wait on the same list, when this
736  * process is woken further processes are not considered.
737  *
738  * wake_up() has to be called after changing any variable that could
739  * change the result of the wait condition.
740  *
741  * Returns:
742  * 0 if the @condition evaluated to %false after the @timeout elapsed,
743  * 1 if the @condition evaluated to %true after the @timeout elapsed,
744  * or the remaining jiffies (at least 1) if the @condition evaluated
745  * to %true before the @timeout elapsed.
746  */
747 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout)          \
748 ({                                                                              \
749         long __ret = timeout;                                                   \
750         might_sleep();                                                          \
751         if (!___wait_cond_timeout(condition))                                   \
752                 __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
753         __ret;                                                                  \
754 })
755
756 extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
757 extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
758
759 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn)         \
760 ({                                                                              \
761         int __ret;                                                              \
762         DEFINE_WAIT(__wait);                                                    \
763         if (exclusive)                                                          \
764                 __wait.flags |= WQ_FLAG_EXCLUSIVE;                              \
765         do {                                                                    \
766                 __ret = fn(&(wq), &__wait);                                     \
767                 if (__ret)                                                      \
768                         break;                                                  \
769         } while (!(condition));                                                 \
770         __remove_wait_queue(&(wq), &__wait);                                    \
771         __set_current_state(TASK_RUNNING);                                      \
772         __ret;                                                                  \
773 })
774
775
776 /**
777  * wait_event_interruptible_locked - sleep until a condition gets true
778  * @wq: the waitqueue to wait on
779  * @condition: a C expression for the event to wait for
780  *
781  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
782  * @condition evaluates to true or a signal is received.
783  * The @condition is checked each time the waitqueue @wq is woken up.
784  *
785  * It must be called with wq.lock being held.  This spinlock is
786  * unlocked while sleeping but @condition testing is done while lock
787  * is held and when this macro exits the lock is held.
788  *
789  * The lock is locked/unlocked using spin_lock()/spin_unlock()
790  * functions which must match the way they are locked/unlocked outside
791  * of this macro.
792  *
793  * wake_up_locked() has to be called after changing any variable that could
794  * change the result of the wait condition.
795  *
796  * The function will return -ERESTARTSYS if it was interrupted by a
797  * signal and 0 if @condition evaluated to true.
798  */
799 #define wait_event_interruptible_locked(wq, condition)                          \
800         ((condition)                                                            \
801          ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
802
803 /**
804  * wait_event_interruptible_locked_irq - sleep until a condition gets true
805  * @wq: the waitqueue to wait on
806  * @condition: a C expression for the event to wait for
807  *
808  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
809  * @condition evaluates to true or a signal is received.
810  * The @condition is checked each time the waitqueue @wq is woken up.
811  *
812  * It must be called with wq.lock being held.  This spinlock is
813  * unlocked while sleeping but @condition testing is done while lock
814  * is held and when this macro exits the lock is held.
815  *
816  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
817  * functions which must match the way they are locked/unlocked outside
818  * of this macro.
819  *
820  * wake_up_locked() has to be called after changing any variable that could
821  * change the result of the wait condition.
822  *
823  * The function will return -ERESTARTSYS if it was interrupted by a
824  * signal and 0 if @condition evaluated to true.
825  */
826 #define wait_event_interruptible_locked_irq(wq, condition)                      \
827         ((condition)                                                            \
828          ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
829
830 /**
831  * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
832  * @wq: the waitqueue to wait on
833  * @condition: a C expression for the event to wait for
834  *
835  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
836  * @condition evaluates to true or a signal is received.
837  * The @condition is checked each time the waitqueue @wq is woken up.
838  *
839  * It must be called with wq.lock being held.  This spinlock is
840  * unlocked while sleeping but @condition testing is done while lock
841  * is held and when this macro exits the lock is held.
842  *
843  * The lock is locked/unlocked using spin_lock()/spin_unlock()
844  * functions which must match the way they are locked/unlocked outside
845  * of this macro.
846  *
847  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
848  * set thus when other process waits process on the list if this
849  * process is awaken further processes are not considered.
850  *
851  * wake_up_locked() has to be called after changing any variable that could
852  * change the result of the wait condition.
853  *
854  * The function will return -ERESTARTSYS if it was interrupted by a
855  * signal and 0 if @condition evaluated to true.
856  */
857 #define wait_event_interruptible_exclusive_locked(wq, condition)                \
858         ((condition)                                                            \
859          ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
860
861 /**
862  * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
863  * @wq: the waitqueue to wait on
864  * @condition: a C expression for the event to wait for
865  *
866  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
867  * @condition evaluates to true or a signal is received.
868  * The @condition is checked each time the waitqueue @wq is woken up.
869  *
870  * It must be called with wq.lock being held.  This spinlock is
871  * unlocked while sleeping but @condition testing is done while lock
872  * is held and when this macro exits the lock is held.
873  *
874  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
875  * functions which must match the way they are locked/unlocked outside
876  * of this macro.
877  *
878  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
879  * set thus when other process waits process on the list if this
880  * process is awaken further processes are not considered.
881  *
882  * wake_up_locked() has to be called after changing any variable that could
883  * change the result of the wait condition.
884  *
885  * The function will return -ERESTARTSYS if it was interrupted by a
886  * signal and 0 if @condition evaluated to true.
887  */
888 #define wait_event_interruptible_exclusive_locked_irq(wq, condition)            \
889         ((condition)                                                            \
890          ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
891
892
893 #define __wait_event_killable(wq, condition)                                    \
894         ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
895
896 /**
897  * wait_event_killable - sleep until a condition gets true
898  * @wq_head: the waitqueue to wait on
899  * @condition: a C expression for the event to wait for
900  *
901  * The process is put to sleep (TASK_KILLABLE) until the
902  * @condition evaluates to true or a signal is received.
903  * The @condition is checked each time the waitqueue @wq_head is woken up.
904  *
905  * wake_up() has to be called after changing any variable that could
906  * change the result of the wait condition.
907  *
908  * The function will return -ERESTARTSYS if it was interrupted by a
909  * signal and 0 if @condition evaluated to true.
910  */
911 #define wait_event_killable(wq_head, condition)                                 \
912 ({                                                                              \
913         int __ret = 0;                                                          \
914         might_sleep();                                                          \
915         if (!(condition))                                                       \
916                 __ret = __wait_event_killable(wq_head, condition);              \
917         __ret;                                                                  \
918 })
919
920 #define __wait_event_killable_timeout(wq_head, condition, timeout)              \
921         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
922                       TASK_KILLABLE, 0, timeout,                                \
923                       __ret = schedule_timeout(__ret))
924
925 /**
926  * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
927  * @wq_head: the waitqueue to wait on
928  * @condition: a C expression for the event to wait for
929  * @timeout: timeout, in jiffies
930  *
931  * The process is put to sleep (TASK_KILLABLE) until the
932  * @condition evaluates to true or a kill signal is received.
933  * The @condition is checked each time the waitqueue @wq_head is woken up.
934  *
935  * wake_up() has to be called after changing any variable that could
936  * change the result of the wait condition.
937  *
938  * Returns:
939  * 0 if the @condition evaluated to %false after the @timeout elapsed,
940  * 1 if the @condition evaluated to %true after the @timeout elapsed,
941  * the remaining jiffies (at least 1) if the @condition evaluated
942  * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
943  * interrupted by a kill signal.
944  *
945  * Only kill signals interrupt this process.
946  */
947 #define wait_event_killable_timeout(wq_head, condition, timeout)                \
948 ({                                                                              \
949         long __ret = timeout;                                                   \
950         might_sleep();                                                          \
951         if (!___wait_cond_timeout(condition))                                   \
952                 __ret = __wait_event_killable_timeout(wq_head,                  \
953                                                 condition, timeout);            \
954         __ret;                                                                  \
955 })
956
957
958 #define __wait_event_lock_irq(wq_head, condition, lock, cmd)                    \
959         (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,     \
960                             spin_unlock_irq(&lock);                             \
961                             cmd;                                                \
962                             schedule();                                         \
963                             spin_lock_irq(&lock))
964
965 /**
966  * wait_event_lock_irq_cmd - sleep until a condition gets true. The
967  *                           condition is checked under the lock. This
968  *                           is expected to be called with the lock
969  *                           taken.
970  * @wq_head: the waitqueue to wait on
971  * @condition: a C expression for the event to wait for
972  * @lock: a locked spinlock_t, which will be released before cmd
973  *        and schedule() and reacquired afterwards.
974  * @cmd: a command which is invoked outside the critical section before
975  *       sleep
976  *
977  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
978  * @condition evaluates to true. The @condition is checked each time
979  * the waitqueue @wq_head is woken up.
980  *
981  * wake_up() has to be called after changing any variable that could
982  * change the result of the wait condition.
983  *
984  * This is supposed to be called while holding the lock. The lock is
985  * dropped before invoking the cmd and going to sleep and is reacquired
986  * afterwards.
987  */
988 #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd)                  \
989 do {                                                                            \
990         if (condition)                                                          \
991                 break;                                                          \
992         __wait_event_lock_irq(wq_head, condition, lock, cmd);                   \
993 } while (0)
994
995 /**
996  * wait_event_lock_irq - sleep until a condition gets true. The
997  *                       condition is checked under the lock. This
998  *                       is expected to be called with the lock
999  *                       taken.
1000  * @wq_head: the waitqueue to wait on
1001  * @condition: a C expression for the event to wait for
1002  * @lock: a locked spinlock_t, which will be released before schedule()
1003  *        and reacquired afterwards.
1004  *
1005  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
1006  * @condition evaluates to true. The @condition is checked each time
1007  * the waitqueue @wq_head is woken up.
1008  *
1009  * wake_up() has to be called after changing any variable that could
1010  * change the result of the wait condition.
1011  *
1012  * This is supposed to be called while holding the lock. The lock is
1013  * dropped before going to sleep and is reacquired afterwards.
1014  */
1015 #define wait_event_lock_irq(wq_head, condition, lock)                           \
1016 do {                                                                            \
1017         if (condition)                                                          \
1018                 break;                                                          \
1019         __wait_event_lock_irq(wq_head, condition, lock, );                      \
1020 } while (0)
1021
1022
1023 #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd)      \
1024         ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,             \
1025                       spin_unlock_irq(&lock);                                   \
1026                       cmd;                                                      \
1027                       schedule();                                               \
1028                       spin_lock_irq(&lock))
1029
1030 /**
1031  * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
1032  *              The condition is checked under the lock. This is expected to
1033  *              be called with the lock taken.
1034  * @wq_head: the waitqueue to wait on
1035  * @condition: a C expression for the event to wait for
1036  * @lock: a locked spinlock_t, which will be released before cmd and
1037  *        schedule() and reacquired afterwards.
1038  * @cmd: a command which is invoked outside the critical section before
1039  *       sleep
1040  *
1041  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1042  * @condition evaluates to true or a signal is received. The @condition is
1043  * checked each time the waitqueue @wq_head is woken up.
1044  *
1045  * wake_up() has to be called after changing any variable that could
1046  * change the result of the wait condition.
1047  *
1048  * This is supposed to be called while holding the lock. The lock is
1049  * dropped before invoking the cmd and going to sleep and is reacquired
1050  * afterwards.
1051  *
1052  * The macro will return -ERESTARTSYS if it was interrupted by a signal
1053  * and 0 if @condition evaluated to true.
1054  */
1055 #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd)    \
1056 ({                                                                              \
1057         int __ret = 0;                                                          \
1058         if (!(condition))                                                       \
1059                 __ret = __wait_event_interruptible_lock_irq(wq_head,            \
1060                                                 condition, lock, cmd);          \
1061         __ret;                                                                  \
1062 })
1063
1064 /**
1065  * wait_event_interruptible_lock_irq - sleep until a condition gets true.
1066  *              The condition is checked under the lock. This is expected
1067  *              to be called with the lock taken.
1068  * @wq_head: the waitqueue to wait on
1069  * @condition: a C expression for the event to wait for
1070  * @lock: a locked spinlock_t, which will be released before schedule()
1071  *        and reacquired afterwards.
1072  *
1073  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1074  * @condition evaluates to true or signal is received. The @condition is
1075  * checked each time the waitqueue @wq_head is woken up.
1076  *
1077  * wake_up() has to be called after changing any variable that could
1078  * change the result of the wait condition.
1079  *
1080  * This is supposed to be called while holding the lock. The lock is
1081  * dropped before going to sleep and is reacquired afterwards.
1082  *
1083  * The macro will return -ERESTARTSYS if it was interrupted by a signal
1084  * and 0 if @condition evaluated to true.
1085  */
1086 #define wait_event_interruptible_lock_irq(wq_head, condition, lock)             \
1087 ({                                                                              \
1088         int __ret = 0;                                                          \
1089         if (!(condition))                                                       \
1090                 __ret = __wait_event_interruptible_lock_irq(wq_head,            \
1091                                                 condition, lock,);              \
1092         __ret;                                                                  \
1093 })
1094
1095 #define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state) \
1096         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
1097                       state, 0, timeout,                                        \
1098                       spin_unlock_irq(&lock);                                   \
1099                       __ret = schedule_timeout(__ret);                          \
1100                       spin_lock_irq(&lock));
1101
1102 /**
1103  * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
1104  *              true or a timeout elapses. The condition is checked under
1105  *              the lock. This is expected to be called with the lock taken.
1106  * @wq_head: the waitqueue to wait on
1107  * @condition: a C expression for the event to wait for
1108  * @lock: a locked spinlock_t, which will be released before schedule()
1109  *        and reacquired afterwards.
1110  * @timeout: timeout, in jiffies
1111  *
1112  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1113  * @condition evaluates to true or signal is received. The @condition is
1114  * checked each time the waitqueue @wq_head is woken up.
1115  *
1116  * wake_up() has to be called after changing any variable that could
1117  * change the result of the wait condition.
1118  *
1119  * This is supposed to be called while holding the lock. The lock is
1120  * dropped before going to sleep and is reacquired afterwards.
1121  *
1122  * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
1123  * was interrupted by a signal, and the remaining jiffies otherwise
1124  * if the condition evaluated to true before the timeout elapsed.
1125  */
1126 #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock,     \
1127                                                   timeout)                      \
1128 ({                                                                              \
1129         long __ret = timeout;                                                   \
1130         if (!___wait_cond_timeout(condition))                                   \
1131                 __ret = __wait_event_lock_irq_timeout(                          \
1132                                         wq_head, condition, lock, timeout,      \
1133                                         TASK_INTERRUPTIBLE);                    \
1134         __ret;                                                                  \
1135 })
1136
1137 #define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout)          \
1138 ({                                                                              \
1139         long __ret = timeout;                                                   \
1140         if (!___wait_cond_timeout(condition))                                   \
1141                 __ret = __wait_event_lock_irq_timeout(                          \
1142                                         wq_head, condition, lock, timeout,      \
1143                                         TASK_UNINTERRUPTIBLE);                  \
1144         __ret;                                                                  \
1145 })
1146
1147 /*
1148  * Waitqueues which are removed from the waitqueue_head at wakeup time
1149  */
1150 void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1151 bool prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1152 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1153 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
1154 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
1155 int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1156 int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1157
1158 #define DEFINE_WAIT_FUNC(name, function)                                        \
1159         struct wait_queue_entry name = {                                        \
1160                 .private        = current,                                      \
1161                 .func           = function,                                     \
1162                 .entry          = LIST_HEAD_INIT((name).entry),                 \
1163         }
1164
1165 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1166
1167 #define init_wait(wait)                                                         \
1168         do {                                                                    \
1169                 (wait)->private = current;                                      \
1170                 (wait)->func = autoremove_wake_function;                        \
1171                 INIT_LIST_HEAD(&(wait)->entry);                                 \
1172                 (wait)->flags = 0;                                              \
1173         } while (0)
1174
1175 #endif /* _LINUX_WAIT_H */