Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_WAIT_H
0003 #define _LINUX_WAIT_H
0004 /*
0005  * Linux wait queue related types and methods
0006  */
0007 #include <linux/list.h>
0008 #include <linux/stddef.h>
0009 #include <linux/spinlock.h>
0010 
0011 #include <asm/current.h>
0012 #include <uapi/linux/wait.h>
0013 
0014 typedef struct wait_queue_entry wait_queue_entry_t;
0015 
0016 typedef int (*wait_queue_func_t)(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
0017 int default_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
0018 
0019 /* wait_queue_entry::flags */
0020 #define WQ_FLAG_EXCLUSIVE   0x01
0021 #define WQ_FLAG_WOKEN       0x02
0022 #define WQ_FLAG_BOOKMARK    0x04
0023 #define WQ_FLAG_CUSTOM      0x08
0024 #define WQ_FLAG_DONE        0x10
0025 #define WQ_FLAG_PRIORITY    0x20
0026 
0027 /*
0028  * A single wait-queue entry structure:
0029  */
0030 struct wait_queue_entry {
0031     unsigned int        flags;
0032     void            *private;
0033     wait_queue_func_t   func;
0034     struct list_head    entry;
0035 };
0036 
0037 struct wait_queue_head {
0038     spinlock_t      lock;
0039     struct list_head    head;
0040 };
0041 typedef struct wait_queue_head wait_queue_head_t;
0042 
0043 struct task_struct;
0044 
0045 /*
0046  * Macros for declaration and initialisaton of the datatypes
0047  */
0048 
0049 #define __WAITQUEUE_INITIALIZER(name, tsk) {                    \
0050     .private    = tsk,                          \
0051     .func       = default_wake_function,                \
0052     .entry      = { NULL, NULL } }
0053 
0054 #define DECLARE_WAITQUEUE(name, tsk)                        \
0055     struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)
0056 
0057 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) {                   \
0058     .lock       = __SPIN_LOCK_UNLOCKED(name.lock),          \
0059     .head       = LIST_HEAD_INIT(name.head) }
0060 
0061 #define DECLARE_WAIT_QUEUE_HEAD(name) \
0062     struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
0063 
0064 extern void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *);
0065 
0066 #define init_waitqueue_head(wq_head)                        \
0067     do {                                    \
0068         static struct lock_class_key __key;             \
0069                                         \
0070         __init_waitqueue_head((wq_head), #wq_head, &__key);     \
0071     } while (0)
0072 
0073 #ifdef CONFIG_LOCKDEP
0074 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
0075     ({ init_waitqueue_head(&name); name; })
0076 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
0077     struct wait_queue_head name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
0078 #else
0079 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
0080 #endif
0081 
0082 static inline void init_waitqueue_entry(struct wait_queue_entry *wq_entry, struct task_struct *p)
0083 {
0084     wq_entry->flags     = 0;
0085     wq_entry->private   = p;
0086     wq_entry->func      = default_wake_function;
0087 }
0088 
0089 static inline void
0090 init_waitqueue_func_entry(struct wait_queue_entry *wq_entry, wait_queue_func_t func)
0091 {
0092     wq_entry->flags     = 0;
0093     wq_entry->private   = NULL;
0094     wq_entry->func      = func;
0095 }
0096 
0097 /**
0098  * waitqueue_active -- locklessly test for waiters on the queue
0099  * @wq_head: the waitqueue to test for waiters
0100  *
0101  * returns true if the wait list is not empty
0102  *
0103  * NOTE: this function is lockless and requires care, incorrect usage _will_
0104  * lead to sporadic and non-obvious failure.
0105  *
0106  * Use either while holding wait_queue_head::lock or when used for wakeups
0107  * with an extra smp_mb() like::
0108  *
0109  *      CPU0 - waker                    CPU1 - waiter
0110  *
0111  *                                      for (;;) {
0112  *      @cond = true;                     prepare_to_wait(&wq_head, &wait, state);
0113  *      smp_mb();                         // smp_mb() from set_current_state()
0114  *      if (waitqueue_active(wq_head))         if (@cond)
0115  *        wake_up(wq_head);                      break;
0116  *                                        schedule();
0117  *                                      }
0118  *                                      finish_wait(&wq_head, &wait);
0119  *
0120  * Because without the explicit smp_mb() it's possible for the
0121  * waitqueue_active() load to get hoisted over the @cond store such that we'll
0122  * observe an empty wait list while the waiter might not observe @cond.
0123  *
0124  * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
0125  * which (when the lock is uncontended) are of roughly equal cost.
0126  */
0127 static inline int waitqueue_active(struct wait_queue_head *wq_head)
0128 {
0129     return !list_empty(&wq_head->head);
0130 }
0131 
0132 /**
0133  * wq_has_single_sleeper - check if there is only one sleeper
0134  * @wq_head: wait queue head
0135  *
0136  * Returns true of wq_head has only one sleeper on the list.
0137  *
0138  * Please refer to the comment for waitqueue_active.
0139  */
0140 static inline bool wq_has_single_sleeper(struct wait_queue_head *wq_head)
0141 {
0142     return list_is_singular(&wq_head->head);
0143 }
0144 
0145 /**
0146  * wq_has_sleeper - check if there are any waiting processes
0147  * @wq_head: wait queue head
0148  *
0149  * Returns true if wq_head has waiting processes
0150  *
0151  * Please refer to the comment for waitqueue_active.
0152  */
0153 static inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
0154 {
0155     /*
0156      * We need to be sure we are in sync with the
0157      * add_wait_queue modifications to the wait queue.
0158      *
0159      * This memory barrier should be paired with one on the
0160      * waiting side.
0161      */
0162     smp_mb();
0163     return waitqueue_active(wq_head);
0164 }
0165 
0166 extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
0167 extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
0168 extern void add_wait_queue_priority(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
0169 extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
0170 
0171 static inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
0172 {
0173     struct list_head *head = &wq_head->head;
0174     struct wait_queue_entry *wq;
0175 
0176     list_for_each_entry(wq, &wq_head->head, entry) {
0177         if (!(wq->flags & WQ_FLAG_PRIORITY))
0178             break;
0179         head = &wq->entry;
0180     }
0181     list_add(&wq_entry->entry, head);
0182 }
0183 
0184 /*
0185  * Used for wake-one threads:
0186  */
0187 static inline void
0188 __add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
0189 {
0190     wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
0191     __add_wait_queue(wq_head, wq_entry);
0192 }
0193 
0194 static inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
0195 {
0196     list_add_tail(&wq_entry->entry, &wq_head->head);
0197 }
0198 
0199 static inline void
0200 __add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
0201 {
0202     wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
0203     __add_wait_queue_entry_tail(wq_head, wq_entry);
0204 }
0205 
0206 static inline void
0207 __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
0208 {
0209     list_del(&wq_entry->entry);
0210 }
0211 
0212 void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
0213 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
0214 void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
0215         unsigned int mode, void *key, wait_queue_entry_t *bookmark);
0216 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
0217 void __wake_up_locked_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
0218 void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
0219 void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode);
0220 void __wake_up_pollfree(struct wait_queue_head *wq_head);
0221 
0222 #define wake_up(x)          __wake_up(x, TASK_NORMAL, 1, NULL)
0223 #define wake_up_nr(x, nr)       __wake_up(x, TASK_NORMAL, nr, NULL)
0224 #define wake_up_all(x)          __wake_up(x, TASK_NORMAL, 0, NULL)
0225 #define wake_up_locked(x)       __wake_up_locked((x), TASK_NORMAL, 1)
0226 #define wake_up_all_locked(x)       __wake_up_locked((x), TASK_NORMAL, 0)
0227 
0228 #define wake_up_interruptible(x)    __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
0229 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
0230 #define wake_up_interruptible_all(x)    __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
0231 #define wake_up_interruptible_sync(x)   __wake_up_sync((x), TASK_INTERRUPTIBLE)
0232 
0233 /*
0234  * Wakeup macros to be used to report events to the targets.
0235  */
0236 #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
0237 #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
0238 #define wake_up_poll(x, m)                          \
0239     __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
0240 #define wake_up_locked_poll(x, m)                       \
0241     __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
0242 #define wake_up_interruptible_poll(x, m)                    \
0243     __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
0244 #define wake_up_interruptible_sync_poll(x, m)                   \
0245     __wake_up_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
0246 #define wake_up_interruptible_sync_poll_locked(x, m)                \
0247     __wake_up_locked_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
0248 
0249 /**
0250  * wake_up_pollfree - signal that a polled waitqueue is going away
0251  * @wq_head: the wait queue head
0252  *
0253  * In the very rare cases where a ->poll() implementation uses a waitqueue whose
0254  * lifetime is tied to a task rather than to the 'struct file' being polled,
0255  * this function must be called before the waitqueue is freed so that
0256  * non-blocking polls (e.g. epoll) are notified that the queue is going away.
0257  *
0258  * The caller must also RCU-delay the freeing of the wait_queue_head, e.g. via
0259  * an explicit synchronize_rcu() or call_rcu(), or via SLAB_TYPESAFE_BY_RCU.
0260  */
0261 static inline void wake_up_pollfree(struct wait_queue_head *wq_head)
0262 {
0263     /*
0264      * For performance reasons, we don't always take the queue lock here.
0265      * Therefore, we might race with someone removing the last entry from
0266      * the queue, and proceed while they still hold the queue lock.
0267      * However, rcu_read_lock() is required to be held in such cases, so we
0268      * can safely proceed with an RCU-delayed free.
0269      */
0270     if (waitqueue_active(wq_head))
0271         __wake_up_pollfree(wq_head);
0272 }
0273 
0274 #define ___wait_cond_timeout(condition)                     \
0275 ({                                      \
0276     bool __cond = (condition);                      \
0277     if (__cond && !__ret)                           \
0278         __ret = 1;                          \
0279     __cond || !__ret;                           \
0280 })
0281 
0282 #define ___wait_is_interruptible(state)                     \
0283     (!__builtin_constant_p(state) ||                    \
0284         state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE)      \
0285 
0286 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
0287 
0288 /*
0289  * The below macro ___wait_event() has an explicit shadow of the __ret
0290  * variable when used from the wait_event_*() macros.
0291  *
0292  * This is so that both can use the ___wait_cond_timeout() construct
0293  * to wrap the condition.
0294  *
0295  * The type inconsistency of the wait_event_*() __ret variable is also
0296  * on purpose; we use long where we can return timeout values and int
0297  * otherwise.
0298  */
0299 
0300 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd)       \
0301 ({                                      \
0302     __label__ __out;                            \
0303     struct wait_queue_entry __wq_entry;                 \
0304     long __ret = ret;   /* explicit shadow */               \
0305                                         \
0306     init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0);    \
0307     for (;;) {                              \
0308         long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
0309                                         \
0310         if (condition)                          \
0311             break;                          \
0312                                         \
0313         if (___wait_is_interruptible(state) && __int) {         \
0314             __ret = __int;                      \
0315             goto __out;                     \
0316         }                               \
0317                                         \
0318         cmd;                                \
0319     }                                   \
0320     finish_wait(&wq_head, &__wq_entry);                 \
0321 __out:  __ret;                                  \
0322 })
0323 
0324 #define __wait_event(wq_head, condition)                    \
0325     (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
0326                 schedule())
0327 
0328 /**
0329  * wait_event - sleep until a condition gets true
0330  * @wq_head: the waitqueue to wait on
0331  * @condition: a C expression for the event to wait for
0332  *
0333  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
0334  * @condition evaluates to true. The @condition is checked each time
0335  * the waitqueue @wq_head is woken up.
0336  *
0337  * wake_up() has to be called after changing any variable that could
0338  * change the result of the wait condition.
0339  */
0340 #define wait_event(wq_head, condition)                      \
0341 do {                                        \
0342     might_sleep();                              \
0343     if (condition)                              \
0344         break;                              \
0345     __wait_event(wq_head, condition);                   \
0346 } while (0)
0347 
0348 #define __io_wait_event(wq_head, condition)                 \
0349     (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
0350                 io_schedule())
0351 
0352 /*
0353  * io_wait_event() -- like wait_event() but with io_schedule()
0354  */
0355 #define io_wait_event(wq_head, condition)                   \
0356 do {                                        \
0357     might_sleep();                              \
0358     if (condition)                              \
0359         break;                              \
0360     __io_wait_event(wq_head, condition);                    \
0361 } while (0)
0362 
0363 #define __wait_event_freezable(wq_head, condition)              \
0364     ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,     \
0365                 freezable_schedule())
0366 
0367 /**
0368  * wait_event_freezable - sleep (or freeze) until a condition gets true
0369  * @wq_head: the waitqueue to wait on
0370  * @condition: a C expression for the event to wait for
0371  *
0372  * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
0373  * to system load) until the @condition evaluates to true. The
0374  * @condition is checked each time the waitqueue @wq_head is woken up.
0375  *
0376  * wake_up() has to be called after changing any variable that could
0377  * change the result of the wait condition.
0378  */
0379 #define wait_event_freezable(wq_head, condition)                \
0380 ({                                      \
0381     int __ret = 0;                              \
0382     might_sleep();                              \
0383     if (!(condition))                           \
0384         __ret = __wait_event_freezable(wq_head, condition);     \
0385     __ret;                                  \
0386 })
0387 
0388 #define __wait_event_timeout(wq_head, condition, timeout)           \
0389     ___wait_event(wq_head, ___wait_cond_timeout(condition),         \
0390               TASK_UNINTERRUPTIBLE, 0, timeout,             \
0391               __ret = schedule_timeout(__ret))
0392 
0393 /**
0394  * wait_event_timeout - sleep until a condition gets true or a timeout elapses
0395  * @wq_head: the waitqueue to wait on
0396  * @condition: a C expression for the event to wait for
0397  * @timeout: timeout, in jiffies
0398  *
0399  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
0400  * @condition evaluates to true. The @condition is checked each time
0401  * the waitqueue @wq_head is woken up.
0402  *
0403  * wake_up() has to be called after changing any variable that could
0404  * change the result of the wait condition.
0405  *
0406  * Returns:
0407  * 0 if the @condition evaluated to %false after the @timeout elapsed,
0408  * 1 if the @condition evaluated to %true after the @timeout elapsed,
0409  * or the remaining jiffies (at least 1) if the @condition evaluated
0410  * to %true before the @timeout elapsed.
0411  */
0412 #define wait_event_timeout(wq_head, condition, timeout)             \
0413 ({                                      \
0414     long __ret = timeout;                           \
0415     might_sleep();                              \
0416     if (!___wait_cond_timeout(condition))                   \
0417         __ret = __wait_event_timeout(wq_head, condition, timeout);  \
0418     __ret;                                  \
0419 })
0420 
0421 #define __wait_event_freezable_timeout(wq_head, condition, timeout)     \
0422     ___wait_event(wq_head, ___wait_cond_timeout(condition),         \
0423               TASK_INTERRUPTIBLE, 0, timeout,               \
0424               __ret = freezable_schedule_timeout(__ret))
0425 
0426 /*
0427  * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
0428  * increasing load and is freezable.
0429  */
0430 #define wait_event_freezable_timeout(wq_head, condition, timeout)       \
0431 ({                                      \
0432     long __ret = timeout;                           \
0433     might_sleep();                              \
0434     if (!___wait_cond_timeout(condition))                   \
0435         __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
0436     __ret;                                  \
0437 })
0438 
0439 #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2)      \
0440     (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
0441                 cmd1; schedule(); cmd2)
0442 /*
0443  * Just like wait_event_cmd(), except it sets exclusive flag
0444  */
0445 #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2)        \
0446 do {                                        \
0447     if (condition)                              \
0448         break;                              \
0449     __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2);     \
0450 } while (0)
0451 
0452 #define __wait_event_cmd(wq_head, condition, cmd1, cmd2)            \
0453     (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
0454                 cmd1; schedule(); cmd2)
0455 
0456 /**
0457  * wait_event_cmd - sleep until a condition gets true
0458  * @wq_head: the waitqueue to wait on
0459  * @condition: a C expression for the event to wait for
0460  * @cmd1: the command will be executed before sleep
0461  * @cmd2: the command will be executed after sleep
0462  *
0463  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
0464  * @condition evaluates to true. The @condition is checked each time
0465  * the waitqueue @wq_head is woken up.
0466  *
0467  * wake_up() has to be called after changing any variable that could
0468  * change the result of the wait condition.
0469  */
0470 #define wait_event_cmd(wq_head, condition, cmd1, cmd2)              \
0471 do {                                        \
0472     if (condition)                              \
0473         break;                              \
0474     __wait_event_cmd(wq_head, condition, cmd1, cmd2);           \
0475 } while (0)
0476 
0477 #define __wait_event_interruptible(wq_head, condition)              \
0478     ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,     \
0479               schedule())
0480 
0481 /**
0482  * wait_event_interruptible - sleep until a condition gets true
0483  * @wq_head: the waitqueue to wait on
0484  * @condition: a C expression for the event to wait for
0485  *
0486  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
0487  * @condition evaluates to true or a signal is received.
0488  * The @condition is checked each time the waitqueue @wq_head is woken up.
0489  *
0490  * wake_up() has to be called after changing any variable that could
0491  * change the result of the wait condition.
0492  *
0493  * The function will return -ERESTARTSYS if it was interrupted by a
0494  * signal and 0 if @condition evaluated to true.
0495  */
0496 #define wait_event_interruptible(wq_head, condition)                \
0497 ({                                      \
0498     int __ret = 0;                              \
0499     might_sleep();                              \
0500     if (!(condition))                           \
0501         __ret = __wait_event_interruptible(wq_head, condition);     \
0502     __ret;                                  \
0503 })
0504 
0505 #define __wait_event_interruptible_timeout(wq_head, condition, timeout)     \
0506     ___wait_event(wq_head, ___wait_cond_timeout(condition),         \
0507               TASK_INTERRUPTIBLE, 0, timeout,               \
0508               __ret = schedule_timeout(__ret))
0509 
0510 /**
0511  * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
0512  * @wq_head: the waitqueue to wait on
0513  * @condition: a C expression for the event to wait for
0514  * @timeout: timeout, in jiffies
0515  *
0516  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
0517  * @condition evaluates to true or a signal is received.
0518  * The @condition is checked each time the waitqueue @wq_head is woken up.
0519  *
0520  * wake_up() has to be called after changing any variable that could
0521  * change the result of the wait condition.
0522  *
0523  * Returns:
0524  * 0 if the @condition evaluated to %false after the @timeout elapsed,
0525  * 1 if the @condition evaluated to %true after the @timeout elapsed,
0526  * the remaining jiffies (at least 1) if the @condition evaluated
0527  * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
0528  * interrupted by a signal.
0529  */
0530 #define wait_event_interruptible_timeout(wq_head, condition, timeout)       \
0531 ({                                      \
0532     long __ret = timeout;                           \
0533     might_sleep();                              \
0534     if (!___wait_cond_timeout(condition))                   \
0535         __ret = __wait_event_interruptible_timeout(wq_head,     \
0536                         condition, timeout);        \
0537     __ret;                                  \
0538 })
0539 
0540 #define __wait_event_hrtimeout(wq_head, condition, timeout, state)      \
0541 ({                                      \
0542     int __ret = 0;                              \
0543     struct hrtimer_sleeper __t;                     \
0544                                         \
0545     hrtimer_init_sleeper_on_stack(&__t, CLOCK_MONOTONIC,            \
0546                       HRTIMER_MODE_REL);            \
0547     if ((timeout) != KTIME_MAX) {                       \
0548         hrtimer_set_expires_range_ns(&__t.timer, timeout,       \
0549                     current->timer_slack_ns);       \
0550         hrtimer_sleeper_start_expires(&__t, HRTIMER_MODE_REL);      \
0551     }                                   \
0552                                         \
0553     __ret = ___wait_event(wq_head, condition, state, 0, 0,          \
0554         if (!__t.task) {                        \
0555             __ret = -ETIME;                     \
0556             break;                          \
0557         }                               \
0558         schedule());                            \
0559                                         \
0560     hrtimer_cancel(&__t.timer);                     \
0561     destroy_hrtimer_on_stack(&__t.timer);                   \
0562     __ret;                                  \
0563 })
0564 
0565 /**
0566  * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
0567  * @wq_head: the waitqueue to wait on
0568  * @condition: a C expression for the event to wait for
0569  * @timeout: timeout, as a ktime_t
0570  *
0571  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
0572  * @condition evaluates to true or a signal is received.
0573  * The @condition is checked each time the waitqueue @wq_head is woken up.
0574  *
0575  * wake_up() has to be called after changing any variable that could
0576  * change the result of the wait condition.
0577  *
0578  * The function returns 0 if @condition became true, or -ETIME if the timeout
0579  * elapsed.
0580  */
0581 #define wait_event_hrtimeout(wq_head, condition, timeout)           \
0582 ({                                      \
0583     int __ret = 0;                              \
0584     might_sleep();                              \
0585     if (!(condition))                           \
0586         __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
0587                            TASK_UNINTERRUPTIBLE);       \
0588     __ret;                                  \
0589 })
0590 
0591 /**
0592  * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
0593  * @wq: the waitqueue to wait on
0594  * @condition: a C expression for the event to wait for
0595  * @timeout: timeout, as a ktime_t
0596  *
0597  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
0598  * @condition evaluates to true or a signal is received.
0599  * The @condition is checked each time the waitqueue @wq is woken up.
0600  *
0601  * wake_up() has to be called after changing any variable that could
0602  * change the result of the wait condition.
0603  *
0604  * The function returns 0 if @condition became true, -ERESTARTSYS if it was
0605  * interrupted by a signal, or -ETIME if the timeout elapsed.
0606  */
0607 #define wait_event_interruptible_hrtimeout(wq, condition, timeout)      \
0608 ({                                      \
0609     long __ret = 0;                             \
0610     might_sleep();                              \
0611     if (!(condition))                           \
0612         __ret = __wait_event_hrtimeout(wq, condition, timeout,      \
0613                            TASK_INTERRUPTIBLE);     \
0614     __ret;                                  \
0615 })
0616 
0617 #define __wait_event_interruptible_exclusive(wq, condition)         \
0618     ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,          \
0619               schedule())
0620 
0621 #define wait_event_interruptible_exclusive(wq, condition)           \
0622 ({                                      \
0623     int __ret = 0;                              \
0624     might_sleep();                              \
0625     if (!(condition))                           \
0626         __ret = __wait_event_interruptible_exclusive(wq, condition);    \
0627     __ret;                                  \
0628 })
0629 
0630 #define __wait_event_killable_exclusive(wq, condition)              \
0631     ___wait_event(wq, condition, TASK_KILLABLE, 1, 0,           \
0632               schedule())
0633 
0634 #define wait_event_killable_exclusive(wq, condition)                \
0635 ({                                      \
0636     int __ret = 0;                              \
0637     might_sleep();                              \
0638     if (!(condition))                           \
0639         __ret = __wait_event_killable_exclusive(wq, condition);     \
0640     __ret;                                  \
0641 })
0642 
0643 
0644 #define __wait_event_freezable_exclusive(wq, condition)             \
0645     ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,          \
0646             freezable_schedule())
0647 
0648 #define wait_event_freezable_exclusive(wq, condition)               \
0649 ({                                      \
0650     int __ret = 0;                              \
0651     might_sleep();                              \
0652     if (!(condition))                           \
0653         __ret = __wait_event_freezable_exclusive(wq, condition);    \
0654     __ret;                                  \
0655 })
0656 
0657 /**
0658  * wait_event_idle - wait for a condition without contributing to system load
0659  * @wq_head: the waitqueue to wait on
0660  * @condition: a C expression for the event to wait for
0661  *
0662  * The process is put to sleep (TASK_IDLE) until the
0663  * @condition evaluates to true.
0664  * The @condition is checked each time the waitqueue @wq_head is woken up.
0665  *
0666  * wake_up() has to be called after changing any variable that could
0667  * change the result of the wait condition.
0668  *
0669  */
0670 #define wait_event_idle(wq_head, condition)                 \
0671 do {                                        \
0672     might_sleep();                              \
0673     if (!(condition))                           \
0674         ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \
0675 } while (0)
0676 
0677 /**
0678  * wait_event_idle_exclusive - wait for a condition with contributing to system load
0679  * @wq_head: the waitqueue to wait on
0680  * @condition: a C expression for the event to wait for
0681  *
0682  * The process is put to sleep (TASK_IDLE) until the
0683  * @condition evaluates to true.
0684  * The @condition is checked each time the waitqueue @wq_head is woken up.
0685  *
0686  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
0687  * set thus if other processes wait on the same list, when this
0688  * process is woken further processes are not considered.
0689  *
0690  * wake_up() has to be called after changing any variable that could
0691  * change the result of the wait condition.
0692  *
0693  */
0694 #define wait_event_idle_exclusive(wq_head, condition)               \
0695 do {                                        \
0696     might_sleep();                              \
0697     if (!(condition))                           \
0698         ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \
0699 } while (0)
0700 
0701 #define __wait_event_idle_timeout(wq_head, condition, timeout)          \
0702     ___wait_event(wq_head, ___wait_cond_timeout(condition),         \
0703               TASK_IDLE, 0, timeout,                    \
0704               __ret = schedule_timeout(__ret))
0705 
0706 /**
0707  * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
0708  * @wq_head: the waitqueue to wait on
0709  * @condition: a C expression for the event to wait for
0710  * @timeout: timeout, in jiffies
0711  *
0712  * The process is put to sleep (TASK_IDLE) until the
0713  * @condition evaluates to true. The @condition is checked each time
0714  * the waitqueue @wq_head is woken up.
0715  *
0716  * wake_up() has to be called after changing any variable that could
0717  * change the result of the wait condition.
0718  *
0719  * Returns:
0720  * 0 if the @condition evaluated to %false after the @timeout elapsed,
0721  * 1 if the @condition evaluated to %true after the @timeout elapsed,
0722  * or the remaining jiffies (at least 1) if the @condition evaluated
0723  * to %true before the @timeout elapsed.
0724  */
0725 #define wait_event_idle_timeout(wq_head, condition, timeout)            \
0726 ({                                      \
0727     long __ret = timeout;                           \
0728     might_sleep();                              \
0729     if (!___wait_cond_timeout(condition))                   \
0730         __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \
0731     __ret;                                  \
0732 })
0733 
0734 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout)    \
0735     ___wait_event(wq_head, ___wait_cond_timeout(condition),         \
0736               TASK_IDLE, 1, timeout,                    \
0737               __ret = schedule_timeout(__ret))
0738 
0739 /**
0740  * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
0741  * @wq_head: the waitqueue to wait on
0742  * @condition: a C expression for the event to wait for
0743  * @timeout: timeout, in jiffies
0744  *
0745  * The process is put to sleep (TASK_IDLE) until the
0746  * @condition evaluates to true. The @condition is checked each time
0747  * the waitqueue @wq_head is woken up.
0748  *
0749  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
0750  * set thus if other processes wait on the same list, when this
0751  * process is woken further processes are not considered.
0752  *
0753  * wake_up() has to be called after changing any variable that could
0754  * change the result of the wait condition.
0755  *
0756  * Returns:
0757  * 0 if the @condition evaluated to %false after the @timeout elapsed,
0758  * 1 if the @condition evaluated to %true after the @timeout elapsed,
0759  * or the remaining jiffies (at least 1) if the @condition evaluated
0760  * to %true before the @timeout elapsed.
0761  */
0762 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout)      \
0763 ({                                      \
0764     long __ret = timeout;                           \
0765     might_sleep();                              \
0766     if (!___wait_cond_timeout(condition))                   \
0767         __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
0768     __ret;                                  \
0769 })
0770 
0771 extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
0772 extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
0773 
0774 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn)     \
0775 ({                                      \
0776     int __ret;                              \
0777     DEFINE_WAIT(__wait);                            \
0778     if (exclusive)                              \
0779         __wait.flags |= WQ_FLAG_EXCLUSIVE;              \
0780     do {                                    \
0781         __ret = fn(&(wq), &__wait);                 \
0782         if (__ret)                          \
0783             break;                          \
0784     } while (!(condition));                         \
0785     __remove_wait_queue(&(wq), &__wait);                    \
0786     __set_current_state(TASK_RUNNING);                  \
0787     __ret;                                  \
0788 })
0789 
0790 
0791 /**
0792  * wait_event_interruptible_locked - sleep until a condition gets true
0793  * @wq: the waitqueue to wait on
0794  * @condition: a C expression for the event to wait for
0795  *
0796  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
0797  * @condition evaluates to true or a signal is received.
0798  * The @condition is checked each time the waitqueue @wq is woken up.
0799  *
0800  * It must be called with wq.lock being held.  This spinlock is
0801  * unlocked while sleeping but @condition testing is done while lock
0802  * is held and when this macro exits the lock is held.
0803  *
0804  * The lock is locked/unlocked using spin_lock()/spin_unlock()
0805  * functions which must match the way they are locked/unlocked outside
0806  * of this macro.
0807  *
0808  * wake_up_locked() has to be called after changing any variable that could
0809  * change the result of the wait condition.
0810  *
0811  * The function will return -ERESTARTSYS if it was interrupted by a
0812  * signal and 0 if @condition evaluated to true.
0813  */
0814 #define wait_event_interruptible_locked(wq, condition)              \
0815     ((condition)                                \
0816      ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
0817 
0818 /**
0819  * wait_event_interruptible_locked_irq - sleep until a condition gets true
0820  * @wq: the waitqueue to wait on
0821  * @condition: a C expression for the event to wait for
0822  *
0823  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
0824  * @condition evaluates to true or a signal is received.
0825  * The @condition is checked each time the waitqueue @wq is woken up.
0826  *
0827  * It must be called with wq.lock being held.  This spinlock is
0828  * unlocked while sleeping but @condition testing is done while lock
0829  * is held and when this macro exits the lock is held.
0830  *
0831  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
0832  * functions which must match the way they are locked/unlocked outside
0833  * of this macro.
0834  *
0835  * wake_up_locked() has to be called after changing any variable that could
0836  * change the result of the wait condition.
0837  *
0838  * The function will return -ERESTARTSYS if it was interrupted by a
0839  * signal and 0 if @condition evaluated to true.
0840  */
0841 #define wait_event_interruptible_locked_irq(wq, condition)          \
0842     ((condition)                                \
0843      ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
0844 
0845 /**
0846  * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
0847  * @wq: the waitqueue to wait on
0848  * @condition: a C expression for the event to wait for
0849  *
0850  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
0851  * @condition evaluates to true or a signal is received.
0852  * The @condition is checked each time the waitqueue @wq is woken up.
0853  *
0854  * It must be called with wq.lock being held.  This spinlock is
0855  * unlocked while sleeping but @condition testing is done while lock
0856  * is held and when this macro exits the lock is held.
0857  *
0858  * The lock is locked/unlocked using spin_lock()/spin_unlock()
0859  * functions which must match the way they are locked/unlocked outside
0860  * of this macro.
0861  *
0862  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
0863  * set thus when other process waits process on the list if this
0864  * process is awaken further processes are not considered.
0865  *
0866  * wake_up_locked() has to be called after changing any variable that could
0867  * change the result of the wait condition.
0868  *
0869  * The function will return -ERESTARTSYS if it was interrupted by a
0870  * signal and 0 if @condition evaluated to true.
0871  */
0872 #define wait_event_interruptible_exclusive_locked(wq, condition)        \
0873     ((condition)                                \
0874      ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
0875 
0876 /**
0877  * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
0878  * @wq: the waitqueue to wait on
0879  * @condition: a C expression for the event to wait for
0880  *
0881  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
0882  * @condition evaluates to true or a signal is received.
0883  * The @condition is checked each time the waitqueue @wq is woken up.
0884  *
0885  * It must be called with wq.lock being held.  This spinlock is
0886  * unlocked while sleeping but @condition testing is done while lock
0887  * is held and when this macro exits the lock is held.
0888  *
0889  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
0890  * functions which must match the way they are locked/unlocked outside
0891  * of this macro.
0892  *
0893  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
0894  * set thus when other process waits process on the list if this
0895  * process is awaken further processes are not considered.
0896  *
0897  * wake_up_locked() has to be called after changing any variable that could
0898  * change the result of the wait condition.
0899  *
0900  * The function will return -ERESTARTSYS if it was interrupted by a
0901  * signal and 0 if @condition evaluated to true.
0902  */
0903 #define wait_event_interruptible_exclusive_locked_irq(wq, condition)        \
0904     ((condition)                                \
0905      ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
0906 
0907 
0908 #define __wait_event_killable(wq, condition)                    \
0909     ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
0910 
0911 /**
0912  * wait_event_killable - sleep until a condition gets true
0913  * @wq_head: the waitqueue to wait on
0914  * @condition: a C expression for the event to wait for
0915  *
0916  * The process is put to sleep (TASK_KILLABLE) until the
0917  * @condition evaluates to true or a signal is received.
0918  * The @condition is checked each time the waitqueue @wq_head is woken up.
0919  *
0920  * wake_up() has to be called after changing any variable that could
0921  * change the result of the wait condition.
0922  *
0923  * The function will return -ERESTARTSYS if it was interrupted by a
0924  * signal and 0 if @condition evaluated to true.
0925  */
0926 #define wait_event_killable(wq_head, condition)                 \
0927 ({                                      \
0928     int __ret = 0;                              \
0929     might_sleep();                              \
0930     if (!(condition))                           \
0931         __ret = __wait_event_killable(wq_head, condition);      \
0932     __ret;                                  \
0933 })
0934 
0935 #define __wait_event_killable_timeout(wq_head, condition, timeout)      \
0936     ___wait_event(wq_head, ___wait_cond_timeout(condition),         \
0937               TASK_KILLABLE, 0, timeout,                \
0938               __ret = schedule_timeout(__ret))
0939 
0940 /**
0941  * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
0942  * @wq_head: the waitqueue to wait on
0943  * @condition: a C expression for the event to wait for
0944  * @timeout: timeout, in jiffies
0945  *
0946  * The process is put to sleep (TASK_KILLABLE) until the
0947  * @condition evaluates to true or a kill signal is received.
0948  * The @condition is checked each time the waitqueue @wq_head is woken up.
0949  *
0950  * wake_up() has to be called after changing any variable that could
0951  * change the result of the wait condition.
0952  *
0953  * Returns:
0954  * 0 if the @condition evaluated to %false after the @timeout elapsed,
0955  * 1 if the @condition evaluated to %true after the @timeout elapsed,
0956  * the remaining jiffies (at least 1) if the @condition evaluated
0957  * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
0958  * interrupted by a kill signal.
0959  *
0960  * Only kill signals interrupt this process.
0961  */
0962 #define wait_event_killable_timeout(wq_head, condition, timeout)        \
0963 ({                                      \
0964     long __ret = timeout;                           \
0965     might_sleep();                              \
0966     if (!___wait_cond_timeout(condition))                   \
0967         __ret = __wait_event_killable_timeout(wq_head,          \
0968                         condition, timeout);        \
0969     __ret;                                  \
0970 })
0971 
0972 
0973 #define __wait_event_lock_irq(wq_head, condition, lock, cmd)            \
0974     (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
0975                 spin_unlock_irq(&lock);             \
0976                 cmd;                        \
0977                 schedule();                     \
0978                 spin_lock_irq(&lock))
0979 
0980 /**
0981  * wait_event_lock_irq_cmd - sleep until a condition gets true. The
0982  *               condition is checked under the lock. This
0983  *               is expected to be called with the lock
0984  *               taken.
0985  * @wq_head: the waitqueue to wait on
0986  * @condition: a C expression for the event to wait for
0987  * @lock: a locked spinlock_t, which will be released before cmd
0988  *    and schedule() and reacquired afterwards.
0989  * @cmd: a command which is invoked outside the critical section before
0990  *   sleep
0991  *
0992  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
0993  * @condition evaluates to true. The @condition is checked each time
0994  * the waitqueue @wq_head is woken up.
0995  *
0996  * wake_up() has to be called after changing any variable that could
0997  * change the result of the wait condition.
0998  *
0999  * This is supposed to be called while holding the lock. The lock is
1000  * dropped before invoking the cmd and going to sleep and is reacquired
1001  * afterwards.
1002  */
1003 #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd)          \
1004 do {                                        \
1005     if (condition)                              \
1006         break;                              \
1007     __wait_event_lock_irq(wq_head, condition, lock, cmd);           \
1008 } while (0)
1009 
1010 /**
1011  * wait_event_lock_irq - sleep until a condition gets true. The
1012  *           condition is checked under the lock. This
1013  *           is expected to be called with the lock
1014  *           taken.
1015  * @wq_head: the waitqueue to wait on
1016  * @condition: a C expression for the event to wait for
1017  * @lock: a locked spinlock_t, which will be released before schedule()
1018  *    and reacquired afterwards.
1019  *
1020  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
1021  * @condition evaluates to true. The @condition is checked each time
1022  * the waitqueue @wq_head is woken up.
1023  *
1024  * wake_up() has to be called after changing any variable that could
1025  * change the result of the wait condition.
1026  *
1027  * This is supposed to be called while holding the lock. The lock is
1028  * dropped before going to sleep and is reacquired afterwards.
1029  */
1030 #define wait_event_lock_irq(wq_head, condition, lock)               \
1031 do {                                        \
1032     if (condition)                              \
1033         break;                              \
1034     __wait_event_lock_irq(wq_head, condition, lock, );          \
1035 } while (0)
1036 
1037 
1038 #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd)  \
1039     ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,     \
1040               spin_unlock_irq(&lock);                   \
1041               cmd;                          \
1042               schedule();                       \
1043               spin_lock_irq(&lock))
1044 
1045 /**
1046  * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
1047  *      The condition is checked under the lock. This is expected to
1048  *      be called with the lock taken.
1049  * @wq_head: the waitqueue to wait on
1050  * @condition: a C expression for the event to wait for
1051  * @lock: a locked spinlock_t, which will be released before cmd and
1052  *    schedule() and reacquired afterwards.
1053  * @cmd: a command which is invoked outside the critical section before
1054  *   sleep
1055  *
1056  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1057  * @condition evaluates to true or a signal is received. The @condition is
1058  * checked each time the waitqueue @wq_head is woken up.
1059  *
1060  * wake_up() has to be called after changing any variable that could
1061  * change the result of the wait condition.
1062  *
1063  * This is supposed to be called while holding the lock. The lock is
1064  * dropped before invoking the cmd and going to sleep and is reacquired
1065  * afterwards.
1066  *
1067  * The macro will return -ERESTARTSYS if it was interrupted by a signal
1068  * and 0 if @condition evaluated to true.
1069  */
1070 #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd)    \
1071 ({                                      \
1072     int __ret = 0;                              \
1073     if (!(condition))                           \
1074         __ret = __wait_event_interruptible_lock_irq(wq_head,        \
1075                         condition, lock, cmd);      \
1076     __ret;                                  \
1077 })
1078 
1079 /**
1080  * wait_event_interruptible_lock_irq - sleep until a condition gets true.
1081  *      The condition is checked under the lock. This is expected
1082  *      to be called with the lock taken.
1083  * @wq_head: the waitqueue to wait on
1084  * @condition: a C expression for the event to wait for
1085  * @lock: a locked spinlock_t, which will be released before schedule()
1086  *    and reacquired afterwards.
1087  *
1088  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1089  * @condition evaluates to true or signal is received. The @condition is
1090  * checked each time the waitqueue @wq_head is woken up.
1091  *
1092  * wake_up() has to be called after changing any variable that could
1093  * change the result of the wait condition.
1094  *
1095  * This is supposed to be called while holding the lock. The lock is
1096  * dropped before going to sleep and is reacquired afterwards.
1097  *
1098  * The macro will return -ERESTARTSYS if it was interrupted by a signal
1099  * and 0 if @condition evaluated to true.
1100  */
1101 #define wait_event_interruptible_lock_irq(wq_head, condition, lock)     \
1102 ({                                      \
1103     int __ret = 0;                              \
1104     if (!(condition))                           \
1105         __ret = __wait_event_interruptible_lock_irq(wq_head,        \
1106                         condition, lock,);      \
1107     __ret;                                  \
1108 })
1109 
1110 #define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state) \
1111     ___wait_event(wq_head, ___wait_cond_timeout(condition),         \
1112               state, 0, timeout,                    \
1113               spin_unlock_irq(&lock);                   \
1114               __ret = schedule_timeout(__ret);              \
1115               spin_lock_irq(&lock));
1116 
1117 /**
1118  * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
1119  *      true or a timeout elapses. The condition is checked under
1120  *      the lock. This is expected to be called with the lock taken.
1121  * @wq_head: the waitqueue to wait on
1122  * @condition: a C expression for the event to wait for
1123  * @lock: a locked spinlock_t, which will be released before schedule()
1124  *    and reacquired afterwards.
1125  * @timeout: timeout, in jiffies
1126  *
1127  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1128  * @condition evaluates to true or signal is received. The @condition is
1129  * checked each time the waitqueue @wq_head is woken up.
1130  *
1131  * wake_up() has to be called after changing any variable that could
1132  * change the result of the wait condition.
1133  *
1134  * This is supposed to be called while holding the lock. The lock is
1135  * dropped before going to sleep and is reacquired afterwards.
1136  *
1137  * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
1138  * was interrupted by a signal, and the remaining jiffies otherwise
1139  * if the condition evaluated to true before the timeout elapsed.
1140  */
1141 #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
1142                           timeout)          \
1143 ({                                      \
1144     long __ret = timeout;                           \
1145     if (!___wait_cond_timeout(condition))                   \
1146         __ret = __wait_event_lock_irq_timeout(              \
1147                     wq_head, condition, lock, timeout,  \
1148                     TASK_INTERRUPTIBLE);            \
1149     __ret;                                  \
1150 })
1151 
1152 #define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout)      \
1153 ({                                      \
1154     long __ret = timeout;                           \
1155     if (!___wait_cond_timeout(condition))                   \
1156         __ret = __wait_event_lock_irq_timeout(              \
1157                     wq_head, condition, lock, timeout,  \
1158                     TASK_UNINTERRUPTIBLE);          \
1159     __ret;                                  \
1160 })
1161 
1162 /*
1163  * Waitqueues which are removed from the waitqueue_head at wakeup time
1164  */
1165 void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1166 bool prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1167 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1168 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
1169 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
1170 int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1171 int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1172 
1173 #define DEFINE_WAIT_FUNC(name, function)                    \
1174     struct wait_queue_entry name = {                    \
1175         .private    = current,                  \
1176         .func       = function,                 \
1177         .entry      = LIST_HEAD_INIT((name).entry),         \
1178     }
1179 
1180 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1181 
1182 #define init_wait(wait)                             \
1183     do {                                    \
1184         (wait)->private = current;                  \
1185         (wait)->func = autoremove_wake_function;            \
1186         INIT_LIST_HEAD(&(wait)->entry);                 \
1187         (wait)->flags = 0;                      \
1188     } while (0)
1189 
1190 typedef int (*task_call_f)(struct task_struct *p, void *arg);
1191 extern int task_call_func(struct task_struct *p, task_call_f func, void *arg);
1192 
1193 #endif /* _LINUX_WAIT_H */