~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/wait.h

Version: ~ [ linux-4.16-rc2 ] ~ [ linux-4.15.4 ] ~ [ linux-4.14.20 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.82 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.116 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.49 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.95 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.54 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.99 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.27.62 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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_sleeper - check if there are any waiting processes
131  * @wq_head: wait queue head
132  *
133  * Returns true if wq_head has waiting processes
134  *
135  * Please refer to the comment for waitqueue_active.
136  */
137 static inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
138 {
139         /*
140          * We need to be sure we are in sync with the
141          * add_wait_queue modifications to the wait queue.
142          *
143          * This memory barrier should be paired with one on the
144          * waiting side.
145          */
146         smp_mb();
147         return waitqueue_active(wq_head);
148 }
149 
150 extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
151 extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
152 extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
153 
154 static inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
155 {
156         list_add(&wq_entry->entry, &wq_head->head);
157 }
158 
159 /*
160  * Used for wake-one threads:
161  */
162 static inline void
163 __add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
164 {
165         wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
166         __add_wait_queue(wq_head, wq_entry);
167 }
168 
169 static inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
170 {
171         list_add_tail(&wq_entry->entry, &wq_head->head);
172 }
173 
174 static inline void
175 __add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
176 {
177         wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
178         __add_wait_queue_entry_tail(wq_head, wq_entry);
179 }
180 
181 static inline void
182 __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
183 {
184         list_del(&wq_entry->entry);
185 }
186 
187 void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
188 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
189 void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
190                 unsigned int mode, void *key, wait_queue_entry_t *bookmark);
191 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
192 void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
193 void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode, int nr);
194 
195 #define wake_up(x)                      __wake_up(x, TASK_NORMAL, 1, NULL)
196 #define wake_up_nr(x, nr)               __wake_up(x, TASK_NORMAL, nr, NULL)
197 #define wake_up_all(x)                  __wake_up(x, TASK_NORMAL, 0, NULL)
198 #define wake_up_locked(x)               __wake_up_locked((x), TASK_NORMAL, 1)
199 #define wake_up_all_locked(x)           __wake_up_locked((x), TASK_NORMAL, 0)
200 
201 #define wake_up_interruptible(x)        __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
202 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
203 #define wake_up_interruptible_all(x)    __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
204 #define wake_up_interruptible_sync(x)   __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
205 
206 /*
207  * Wakeup macros to be used to report events to the targets.
208  */
209 #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
210 #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
211 #define wake_up_poll(x, m)                                                      \
212         __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
213 #define wake_up_locked_poll(x, m)                                               \
214         __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
215 #define wake_up_interruptible_poll(x, m)                                        \
216         __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
217 #define wake_up_interruptible_sync_poll(x, m)                                   \
218         __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, poll_to_key(m))
219 
220 #define ___wait_cond_timeout(condition)                                         \
221 ({                                                                              \
222         bool __cond = (condition);                                              \
223         if (__cond && !__ret)                                                   \
224                 __ret = 1;                                                      \
225         __cond || !__ret;                                                       \
226 })
227 
228 #define ___wait_is_interruptible(state)                                         \
229         (!__builtin_constant_p(state) ||                                        \
230                 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE)          \
231 
232 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
233 
234 /*
235  * The below macro ___wait_event() has an explicit shadow of the __ret
236  * variable when used from the wait_event_*() macros.
237  *
238  * This is so that both can use the ___wait_cond_timeout() construct
239  * to wrap the condition.
240  *
241  * The type inconsistency of the wait_event_*() __ret variable is also
242  * on purpose; we use long where we can return timeout values and int
243  * otherwise.
244  */
245 
246 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd)           \
247 ({                                                                              \
248         __label__ __out;                                                        \
249         struct wait_queue_entry __wq_entry;                                     \
250         long __ret = ret;       /* explicit shadow */                           \
251                                                                                 \
252         init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0);        \
253         for (;;) {                                                              \
254                 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
255                                                                                 \
256                 if (condition)                                                  \
257                         break;                                                  \
258                                                                                 \
259                 if (___wait_is_interruptible(state) && __int) {                 \
260                         __ret = __int;                                          \
261                         goto __out;                                             \
262                 }                                                               \
263                                                                                 \
264                 cmd;                                                            \
265         }                                                                       \
266         finish_wait(&wq_head, &__wq_entry);                                     \
267 __out:  __ret;                                                                  \
268 })
269 
270 #define __wait_event(wq_head, condition)                                        \
271         (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,     \
272                             schedule())
273 
274 /**
275  * wait_event - sleep until a condition gets true
276  * @wq_head: the waitqueue to wait on
277  * @condition: a C expression for the event to wait for
278  *
279  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
280  * @condition evaluates to true. The @condition is checked each time
281  * the waitqueue @wq_head is woken up.
282  *
283  * wake_up() has to be called after changing any variable that could
284  * change the result of the wait condition.
285  */
286 #define wait_event(wq_head, condition)                                          \
287 do {                                                                            \
288         might_sleep();                                                          \
289         if (condition)                                                          \
290                 break;                                                          \
291         __wait_event(wq_head, condition);                                       \
292 } while (0)
293 
294 #define __io_wait_event(wq_head, condition)                                     \
295         (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,     \
296                             io_schedule())
297 
298 /*
299  * io_wait_event() -- like wait_event() but with io_schedule()
300  */
301 #define io_wait_event(wq_head, condition)                                       \
302 do {                                                                            \
303         might_sleep();                                                          \
304         if (condition)                                                          \
305                 break;                                                          \
306         __io_wait_event(wq_head, condition);                                    \
307 } while (0)
308 
309 #define __wait_event_freezable(wq_head, condition)                              \
310         ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,             \
311                             schedule(); try_to_freeze())
312 
313 /**
314  * wait_event_freezable - sleep (or freeze) 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_INTERRUPTIBLE -- so as not to contribute
319  * to system load) until the @condition evaluates to true. The
320  * @condition is checked each time 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_freezable(wq_head, condition)                                \
326 ({                                                                              \
327         int __ret = 0;                                                          \
328         might_sleep();                                                          \
329         if (!(condition))                                                       \
330                 __ret = __wait_event_freezable(wq_head, condition);             \
331         __ret;                                                                  \
332 })
333 
334 #define __wait_event_timeout(wq_head, condition, timeout)                       \
335         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
336                       TASK_UNINTERRUPTIBLE, 0, timeout,                         \
337                       __ret = schedule_timeout(__ret))
338 
339 /**
340  * wait_event_timeout - sleep until a condition gets true or a timeout elapses
341  * @wq_head: the waitqueue to wait on
342  * @condition: a C expression for the event to wait for
343  * @timeout: timeout, in jiffies
344  *
345  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
346  * @condition evaluates to true. The @condition is checked each time
347  * the waitqueue @wq_head is woken up.
348  *
349  * wake_up() has to be called after changing any variable that could
350  * change the result of the wait condition.
351  *
352  * Returns:
353  * 0 if the @condition evaluated to %false after the @timeout elapsed,
354  * 1 if the @condition evaluated to %true after the @timeout elapsed,
355  * or the remaining jiffies (at least 1) if the @condition evaluated
356  * to %true before the @timeout elapsed.
357  */
358 #define wait_event_timeout(wq_head, condition, timeout)                         \
359 ({                                                                              \
360         long __ret = timeout;                                                   \
361         might_sleep();                                                          \
362         if (!___wait_cond_timeout(condition))                                   \
363                 __ret = __wait_event_timeout(wq_head, condition, timeout);      \
364         __ret;                                                                  \
365 })
366 
367 #define __wait_event_freezable_timeout(wq_head, condition, timeout)             \
368         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
369                       TASK_INTERRUPTIBLE, 0, timeout,                           \
370                       __ret = schedule_timeout(__ret); try_to_freeze())
371 
372 /*
373  * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
374  * increasing load and is freezable.
375  */
376 #define wait_event_freezable_timeout(wq_head, condition, timeout)               \
377 ({                                                                              \
378         long __ret = timeout;                                                   \
379         might_sleep();                                                          \
380         if (!___wait_cond_timeout(condition))                                   \
381                 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
382         __ret;                                                                  \
383 })
384 
385 #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2)              \
386         (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0,     \
387                             cmd1; schedule(); cmd2)
388 /*
389  * Just like wait_event_cmd(), except it sets exclusive flag
390  */
391 #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2)                \
392 do {                                                                            \
393         if (condition)                                                          \
394                 break;                                                          \
395         __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2);             \
396 } while (0)
397 
398 #define __wait_event_cmd(wq_head, condition, cmd1, cmd2)                        \
399         (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,     \
400                             cmd1; schedule(); cmd2)
401 
402 /**
403  * wait_event_cmd - sleep until a condition gets true
404  * @wq_head: the waitqueue to wait on
405  * @condition: a C expression for the event to wait for
406  * @cmd1: the command will be executed before sleep
407  * @cmd2: the command will be executed after sleep
408  *
409  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
410  * @condition evaluates to true. The @condition is checked each time
411  * the waitqueue @wq_head is woken up.
412  *
413  * wake_up() has to be called after changing any variable that could
414  * change the result of the wait condition.
415  */
416 #define wait_event_cmd(wq_head, condition, cmd1, cmd2)                          \
417 do {                                                                            \
418         if (condition)                                                          \
419                 break;                                                          \
420         __wait_event_cmd(wq_head, condition, cmd1, cmd2);                       \
421 } while (0)
422 
423 #define __wait_event_interruptible(wq_head, condition)                          \
424         ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,             \
425                       schedule())
426 
427 /**
428  * wait_event_interruptible - sleep until a condition gets true
429  * @wq_head: the waitqueue to wait on
430  * @condition: a C expression for the event to wait for
431  *
432  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
433  * @condition evaluates to true or a signal is received.
434  * The @condition is checked each time the waitqueue @wq_head is woken up.
435  *
436  * wake_up() has to be called after changing any variable that could
437  * change the result of the wait condition.
438  *
439  * The function will return -ERESTARTSYS if it was interrupted by a
440  * signal and 0 if @condition evaluated to true.
441  */
442 #define wait_event_interruptible(wq_head, condition)                            \
443 ({                                                                              \
444         int __ret = 0;                                                          \
445         might_sleep();                                                          \
446         if (!(condition))                                                       \
447                 __ret = __wait_event_interruptible(wq_head, condition);         \
448         __ret;                                                                  \
449 })
450 
451 #define __wait_event_interruptible_timeout(wq_head, condition, timeout)         \
452         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
453                       TASK_INTERRUPTIBLE, 0, timeout,                           \
454                       __ret = schedule_timeout(__ret))
455 
456 /**
457  * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
458  * @wq_head: the waitqueue to wait on
459  * @condition: a C expression for the event to wait for
460  * @timeout: timeout, in jiffies
461  *
462  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
463  * @condition evaluates to true or a signal is received.
464  * The @condition is checked each time the waitqueue @wq_head is woken up.
465  *
466  * wake_up() has to be called after changing any variable that could
467  * change the result of the wait condition.
468  *
469  * Returns:
470  * 0 if the @condition evaluated to %false after the @timeout elapsed,
471  * 1 if the @condition evaluated to %true after the @timeout elapsed,
472  * the remaining jiffies (at least 1) if the @condition evaluated
473  * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
474  * interrupted by a signal.
475  */
476 #define wait_event_interruptible_timeout(wq_head, condition, timeout)           \
477 ({                                                                              \
478         long __ret = timeout;                                                   \
479         might_sleep();                                                          \
480         if (!___wait_cond_timeout(condition))                                   \
481                 __ret = __wait_event_interruptible_timeout(wq_head,             \
482                                                 condition, timeout);            \
483         __ret;                                                                  \
484 })
485 
486 #define __wait_event_hrtimeout(wq_head, condition, timeout, state)              \
487 ({                                                                              \
488         int __ret = 0;                                                          \
489         struct hrtimer_sleeper __t;                                             \
490                                                                                 \
491         hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);   \
492         hrtimer_init_sleeper(&__t, current);                                    \
493         if ((timeout) != KTIME_MAX)                                             \
494                 hrtimer_start_range_ns(&__t.timer, timeout,                     \
495                                        current->timer_slack_ns,                 \
496                                        HRTIMER_MODE_REL);                       \
497                                                                                 \
498         __ret = ___wait_event(wq_head, condition, state, 0, 0,                  \
499                 if (!__t.task) {                                                \
500                         __ret = -ETIME;                                         \
501                         break;                                                  \
502                 }                                                               \
503                 schedule());                                                    \
504                                                                                 \
505         hrtimer_cancel(&__t.timer);                                             \
506         destroy_hrtimer_on_stack(&__t.timer);                                   \
507         __ret;                                                                  \
508 })
509 
510 /**
511  * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
512  * @wq_head: the waitqueue to wait on
513  * @condition: a C expression for the event to wait for
514  * @timeout: timeout, as a ktime_t
515  *
516  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
517  * @condition evaluates to true or a signal is received.
518  * The @condition is checked each time the waitqueue @wq_head is woken up.
519  *
520  * wake_up() has to be called after changing any variable that could
521  * change the result of the wait condition.
522  *
523  * The function returns 0 if @condition became true, or -ETIME if the timeout
524  * elapsed.
525  */
526 #define wait_event_hrtimeout(wq_head, condition, timeout)                       \
527 ({                                                                              \
528         int __ret = 0;                                                          \
529         might_sleep();                                                          \
530         if (!(condition))                                                       \
531                 __ret = __wait_event_hrtimeout(wq_head, condition, timeout,     \
532                                                TASK_UNINTERRUPTIBLE);           \
533         __ret;                                                                  \
534 })
535 
536 /**
537  * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
538  * @wq: the waitqueue to wait on
539  * @condition: a C expression for the event to wait for
540  * @timeout: timeout, as a ktime_t
541  *
542  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
543  * @condition evaluates to true or a signal is received.
544  * The @condition is checked each time the waitqueue @wq is woken up.
545  *
546  * wake_up() has to be called after changing any variable that could
547  * change the result of the wait condition.
548  *
549  * The function returns 0 if @condition became true, -ERESTARTSYS if it was
550  * interrupted by a signal, or -ETIME if the timeout elapsed.
551  */
552 #define wait_event_interruptible_hrtimeout(wq, condition, timeout)              \
553 ({                                                                              \
554         long __ret = 0;                                                         \
555         might_sleep();                                                          \
556         if (!(condition))                                                       \
557                 __ret = __wait_event_hrtimeout(wq, condition, timeout,          \
558                                                TASK_INTERRUPTIBLE);             \
559         __ret;                                                                  \
560 })
561 
562 #define __wait_event_interruptible_exclusive(wq, condition)                     \
563         ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,                  \
564                       schedule())
565 
566 #define wait_event_interruptible_exclusive(wq, condition)                       \
567 ({                                                                              \
568         int __ret = 0;                                                          \
569         might_sleep();                                                          \
570         if (!(condition))                                                       \
571                 __ret = __wait_event_interruptible_exclusive(wq, condition);    \
572         __ret;                                                                  \
573 })
574 
575 #define __wait_event_killable_exclusive(wq, condition)                          \
576         ___wait_event(wq, condition, TASK_KILLABLE, 1, 0,                       \
577                       schedule())
578 
579 #define wait_event_killable_exclusive(wq, condition)                            \
580 ({                                                                              \
581         int __ret = 0;                                                          \
582         might_sleep();                                                          \
583         if (!(condition))                                                       \
584                 __ret = __wait_event_killable_exclusive(wq, condition);         \
585         __ret;                                                                  \
586 })
587 
588 
589 #define __wait_event_freezable_exclusive(wq, condition)                         \
590         ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,                  \
591                         schedule(); try_to_freeze())
592 
593 #define wait_event_freezable_exclusive(wq, condition)                           \
594 ({                                                                              \
595         int __ret = 0;                                                          \
596         might_sleep();                                                          \
597         if (!(condition))                                                       \
598                 __ret = __wait_event_freezable_exclusive(wq, condition);        \
599         __ret;                                                                  \
600 })
601 
602 extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
603 extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
604 
605 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn)         \
606 ({                                                                              \
607         int __ret;                                                              \
608         DEFINE_WAIT(__wait);                                                    \
609         if (exclusive)                                                          \
610                 __wait.flags |= WQ_FLAG_EXCLUSIVE;                              \
611         do {                                                                    \
612                 __ret = fn(&(wq), &__wait);                                     \
613                 if (__ret)                                                      \
614                         break;                                                  \
615         } while (!(condition));                                                 \
616         __remove_wait_queue(&(wq), &__wait);                                    \
617         __set_current_state(TASK_RUNNING);                                      \
618         __ret;                                                                  \
619 })
620 
621 
622 /**
623  * wait_event_interruptible_locked - sleep until a condition gets true
624  * @wq: the waitqueue to wait on
625  * @condition: a C expression for the event to wait for
626  *
627  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
628  * @condition evaluates to true or a signal is received.
629  * The @condition is checked each time the waitqueue @wq is woken up.
630  *
631  * It must be called with wq.lock being held.  This spinlock is
632  * unlocked while sleeping but @condition testing is done while lock
633  * is held and when this macro exits the lock is held.
634  *
635  * The lock is locked/unlocked using spin_lock()/spin_unlock()
636  * functions which must match the way they are locked/unlocked outside
637  * of this macro.
638  *
639  * wake_up_locked() has to be called after changing any variable that could
640  * change the result of the wait condition.
641  *
642  * The function will return -ERESTARTSYS if it was interrupted by a
643  * signal and 0 if @condition evaluated to true.
644  */
645 #define wait_event_interruptible_locked(wq, condition)                          \
646         ((condition)                                                            \
647          ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
648 
649 /**
650  * wait_event_interruptible_locked_irq - sleep until a condition gets true
651  * @wq: the waitqueue to wait on
652  * @condition: a C expression for the event to wait for
653  *
654  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
655  * @condition evaluates to true or a signal is received.
656  * The @condition is checked each time the waitqueue @wq is woken up.
657  *
658  * It must be called with wq.lock being held.  This spinlock is
659  * unlocked while sleeping but @condition testing is done while lock
660  * is held and when this macro exits the lock is held.
661  *
662  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
663  * functions which must match the way they are locked/unlocked outside
664  * of this macro.
665  *
666  * wake_up_locked() has to be called after changing any variable that could
667  * change the result of the wait condition.
668  *
669  * The function will return -ERESTARTSYS if it was interrupted by a
670  * signal and 0 if @condition evaluated to true.
671  */
672 #define wait_event_interruptible_locked_irq(wq, condition)                      \
673         ((condition)                                                            \
674          ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
675 
676 /**
677  * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
678  * @wq: the waitqueue to wait on
679  * @condition: a C expression for the event to wait for
680  *
681  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
682  * @condition evaluates to true or a signal is received.
683  * The @condition is checked each time the waitqueue @wq is woken up.
684  *
685  * It must be called with wq.lock being held.  This spinlock is
686  * unlocked while sleeping but @condition testing is done while lock
687  * is held and when this macro exits the lock is held.
688  *
689  * The lock is locked/unlocked using spin_lock()/spin_unlock()
690  * functions which must match the way they are locked/unlocked outside
691  * of this macro.
692  *
693  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
694  * set thus when other process waits process on the list if this
695  * process is awaken further processes are not considered.
696  *
697  * wake_up_locked() has to be called after changing any variable that could
698  * change the result of the wait condition.
699  *
700  * The function will return -ERESTARTSYS if it was interrupted by a
701  * signal and 0 if @condition evaluated to true.
702  */
703 #define wait_event_interruptible_exclusive_locked(wq, condition)                \
704         ((condition)                                                            \
705          ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
706 
707 /**
708  * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
709  * @wq: the waitqueue to wait on
710  * @condition: a C expression for the event to wait for
711  *
712  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
713  * @condition evaluates to true or a signal is received.
714  * The @condition is checked each time the waitqueue @wq is woken up.
715  *
716  * It must be called with wq.lock being held.  This spinlock is
717  * unlocked while sleeping but @condition testing is done while lock
718  * is held and when this macro exits the lock is held.
719  *
720  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
721  * functions which must match the way they are locked/unlocked outside
722  * of this macro.
723  *
724  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
725  * set thus when other process waits process on the list if this
726  * process is awaken further processes are not considered.
727  *
728  * wake_up_locked() has to be called after changing any variable that could
729  * change the result of the wait condition.
730  *
731  * The function will return -ERESTARTSYS if it was interrupted by a
732  * signal and 0 if @condition evaluated to true.
733  */
734 #define wait_event_interruptible_exclusive_locked_irq(wq, condition)            \
735         ((condition)                                                            \
736          ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
737 
738 
739 #define __wait_event_killable(wq, condition)                                    \
740         ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
741 
742 /**
743  * wait_event_killable - sleep until a condition gets true
744  * @wq_head: the waitqueue to wait on
745  * @condition: a C expression for the event to wait for
746  *
747  * The process is put to sleep (TASK_KILLABLE) until the
748  * @condition evaluates to true or a signal is received.
749  * The @condition is checked each time the waitqueue @wq_head is woken up.
750  *
751  * wake_up() has to be called after changing any variable that could
752  * change the result of the wait condition.
753  *
754  * The function will return -ERESTARTSYS if it was interrupted by a
755  * signal and 0 if @condition evaluated to true.
756  */
757 #define wait_event_killable(wq_head, condition)                                 \
758 ({                                                                              \
759         int __ret = 0;                                                          \
760         might_sleep();                                                          \
761         if (!(condition))                                                       \
762                 __ret = __wait_event_killable(wq_head, condition);              \
763         __ret;                                                                  \
764 })
765 
766 #define __wait_event_killable_timeout(wq_head, condition, timeout)              \
767         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
768                       TASK_KILLABLE, 0, timeout,                                \
769                       __ret = schedule_timeout(__ret))
770 
771 /**
772  * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
773  * @wq_head: the waitqueue to wait on
774  * @condition: a C expression for the event to wait for
775  * @timeout: timeout, in jiffies
776  *
777  * The process is put to sleep (TASK_KILLABLE) until the
778  * @condition evaluates to true or a kill signal is received.
779  * The @condition is checked each time the waitqueue @wq_head is woken up.
780  *
781  * wake_up() has to be called after changing any variable that could
782  * change the result of the wait condition.
783  *
784  * Returns:
785  * 0 if the @condition evaluated to %false after the @timeout elapsed,
786  * 1 if the @condition evaluated to %true after the @timeout elapsed,
787  * the remaining jiffies (at least 1) if the @condition evaluated
788  * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
789  * interrupted by a kill signal.
790  *
791  * Only kill signals interrupt this process.
792  */
793 #define wait_event_killable_timeout(wq_head, condition, timeout)                \
794 ({                                                                              \
795         long __ret = timeout;                                                   \
796         might_sleep();                                                          \
797         if (!___wait_cond_timeout(condition))                                   \
798                 __ret = __wait_event_killable_timeout(wq_head,                  \
799                                                 condition, timeout);            \
800         __ret;                                                                  \
801 })
802 
803 
804 #define __wait_event_lock_irq(wq_head, condition, lock, cmd)                    \
805         (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,     \
806                             spin_unlock_irq(&lock);                             \
807                             cmd;                                                \
808                             schedule();                                         \
809                             spin_lock_irq(&lock))
810 
811 /**
812  * wait_event_lock_irq_cmd - sleep until a condition gets true. The
813  *                           condition is checked under the lock. This
814  *                           is expected to be called with the lock
815  *                           taken.
816  * @wq_head: the waitqueue to wait on
817  * @condition: a C expression for the event to wait for
818  * @lock: a locked spinlock_t, which will be released before cmd
819  *        and schedule() and reacquired afterwards.
820  * @cmd: a command which is invoked outside the critical section before
821  *       sleep
822  *
823  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
824  * @condition evaluates to true. The @condition is checked each time
825  * the waitqueue @wq_head is woken up.
826  *
827  * wake_up() has to be called after changing any variable that could
828  * change the result of the wait condition.
829  *
830  * This is supposed to be called while holding the lock. The lock is
831  * dropped before invoking the cmd and going to sleep and is reacquired
832  * afterwards.
833  */
834 #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd)                  \
835 do {                                                                            \
836         if (condition)                                                          \
837                 break;                                                          \
838         __wait_event_lock_irq(wq_head, condition, lock, cmd);                   \
839 } while (0)
840 
841 /**
842  * wait_event_lock_irq - sleep until a condition gets true. The
843  *                       condition is checked under the lock. This
844  *                       is expected to be called with the lock
845  *                       taken.
846  * @wq_head: the waitqueue to wait on
847  * @condition: a C expression for the event to wait for
848  * @lock: a locked spinlock_t, which will be released before schedule()
849  *        and reacquired afterwards.
850  *
851  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
852  * @condition evaluates to true. The @condition is checked each time
853  * the waitqueue @wq_head is woken up.
854  *
855  * wake_up() has to be called after changing any variable that could
856  * change the result of the wait condition.
857  *
858  * This is supposed to be called while holding the lock. The lock is
859  * dropped before going to sleep and is reacquired afterwards.
860  */
861 #define wait_event_lock_irq(wq_head, condition, lock)                           \
862 do {                                                                            \
863         if (condition)                                                          \
864                 break;                                                          \
865         __wait_event_lock_irq(wq_head, condition, lock, );                      \
866 } while (0)
867 
868 
869 #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd)      \
870         ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,             \
871                       spin_unlock_irq(&lock);                                   \
872                       cmd;                                                      \
873                       schedule();                                               \
874                       spin_lock_irq(&lock))
875 
876 /**
877  * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
878  *              The condition is checked under the lock. This is expected to
879  *              be called with the lock taken.
880  * @wq_head: the waitqueue to wait on
881  * @condition: a C expression for the event to wait for
882  * @lock: a locked spinlock_t, which will be released before cmd and
883  *        schedule() and reacquired afterwards.
884  * @cmd: a command which is invoked outside the critical section before
885  *       sleep
886  *
887  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
888  * @condition evaluates to true or a signal is received. The @condition is
889  * checked each time the waitqueue @wq_head is woken up.
890  *
891  * wake_up() has to be called after changing any variable that could
892  * change the result of the wait condition.
893  *
894  * This is supposed to be called while holding the lock. The lock is
895  * dropped before invoking the cmd and going to sleep and is reacquired
896  * afterwards.
897  *
898  * The macro will return -ERESTARTSYS if it was interrupted by a signal
899  * and 0 if @condition evaluated to true.
900  */
901 #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd)    \
902 ({                                                                              \
903         int __ret = 0;                                                          \
904         if (!(condition))                                                       \
905                 __ret = __wait_event_interruptible_lock_irq(wq_head,            \
906                                                 condition, lock, cmd);          \
907         __ret;                                                                  \
908 })
909 
910 /**
911  * wait_event_interruptible_lock_irq - sleep until a condition gets true.
912  *              The condition is checked under the lock. This is expected
913  *              to be called with the lock taken.
914  * @wq_head: the waitqueue to wait on
915  * @condition: a C expression for the event to wait for
916  * @lock: a locked spinlock_t, which will be released before schedule()
917  *        and reacquired afterwards.
918  *
919  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
920  * @condition evaluates to true or signal is received. The @condition is
921  * checked each time the waitqueue @wq_head is woken up.
922  *
923  * wake_up() has to be called after changing any variable that could
924  * change the result of the wait condition.
925  *
926  * This is supposed to be called while holding the lock. The lock is
927  * dropped before going to sleep and is reacquired afterwards.
928  *
929  * The macro will return -ERESTARTSYS if it was interrupted by a signal
930  * and 0 if @condition evaluated to true.
931  */
932 #define wait_event_interruptible_lock_irq(wq_head, condition, lock)             \
933 ({                                                                              \
934         int __ret = 0;                                                          \
935         if (!(condition))                                                       \
936                 __ret = __wait_event_interruptible_lock_irq(wq_head,            \
937                                                 condition, lock,);              \
938         __ret;                                                                  \
939 })
940 
941 #define __wait_event_interruptible_lock_irq_timeout(wq_head, condition,         \
942                                                     lock, timeout)              \
943         ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
944                       TASK_INTERRUPTIBLE, 0, timeout,                           \
945                       spin_unlock_irq(&lock);                                   \
946                       __ret = schedule_timeout(__ret);                          \
947                       spin_lock_irq(&lock));
948 
949 /**
950  * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
951  *              true or a timeout elapses. The condition is checked under
952  *              the lock. This is expected to be called with the lock taken.
953  * @wq_head: the waitqueue to wait on
954  * @condition: a C expression for the event to wait for
955  * @lock: a locked spinlock_t, which will be released before schedule()
956  *        and reacquired afterwards.
957  * @timeout: timeout, in jiffies
958  *
959  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
960  * @condition evaluates to true or signal is received. The @condition is
961  * checked each time the waitqueue @wq_head is woken up.
962  *
963  * wake_up() has to be called after changing any variable that could
964  * change the result of the wait condition.
965  *
966  * This is supposed to be called while holding the lock. The lock is
967  * dropped before going to sleep and is reacquired afterwards.
968  *
969  * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
970  * was interrupted by a signal, and the remaining jiffies otherwise
971  * if the condition evaluated to true before the timeout elapsed.
972  */
973 #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock,     \
974                                                   timeout)                      \
975 ({                                                                              \
976         long __ret = timeout;                                                   \
977         if (!___wait_cond_timeout(condition))                                   \
978                 __ret = __wait_event_interruptible_lock_irq_timeout(            \
979                                         wq_head, condition, lock, timeout);     \
980         __ret;                                                                  \
981 })
982 
983 /*
984  * Waitqueues which are removed from the waitqueue_head at wakeup time
985  */
986 void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
987 void prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
988 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
989 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
990 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
991 int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
992 int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
993 
994 #define DEFINE_WAIT_FUNC(name, function)                                        \
995         struct wait_queue_entry name = {                                        \
996                 .private        = current,                                      \
997                 .func           = function,                                     \
998                 .entry          = LIST_HEAD_INIT((name).entry),                 \
999         }
1000 
1001 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1002 
1003 #define init_wait(wait)                                                         \
1004         do {                                                                    \
1005                 (wait)->private = current;                                      \
1006                 (wait)->func = autoremove_wake_function;                        \
1007                 INIT_LIST_HEAD(&(wait)->entry);                                 \
1008                 (wait)->flags = 0;                                              \
1009         } while (0)
1010 
1011 #endif /* _LINUX_WAIT_H */
1012 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp