linux/include/linux/wait.h
<<
>>
Prefs
   1#ifndef _LINUX_WAIT_H
   2#define _LINUX_WAIT_H
   3
   4
   5#include <linux/list.h>
   6#include <linux/stddef.h>
   7#include <linux/spinlock.h>
   8#include <asm/current.h>
   9#include <uapi/linux/wait.h>
  10
  11typedef struct __wait_queue wait_queue_t;
  12typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  13int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
  14
  15struct __wait_queue {
  16        unsigned int flags;
  17#define WQ_FLAG_EXCLUSIVE       0x01
  18        void *private;
  19        wait_queue_func_t func;
  20        struct list_head task_list;
  21};
  22
  23struct wait_bit_key {
  24        void *flags;
  25        int bit_nr;
  26};
  27
  28struct wait_bit_queue {
  29        struct wait_bit_key key;
  30        wait_queue_t wait;
  31};
  32
  33struct __wait_queue_head {
  34        spinlock_t lock;
  35        struct list_head task_list;
  36};
  37typedef struct __wait_queue_head wait_queue_head_t;
  38
  39struct task_struct;
  40
  41/*
  42 * Macros for declaration and initialisaton of the datatypes
  43 */
  44
  45#define __WAITQUEUE_INITIALIZER(name, tsk) {                            \
  46        .private        = tsk,                                          \
  47        .func           = default_wake_function,                        \
  48        .task_list      = { NULL, NULL } }
  49
  50#define DECLARE_WAITQUEUE(name, tsk)                                    \
  51        wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
  52
  53#define __WAIT_QUEUE_HEAD_INITIALIZER(name) {                           \
  54        .lock           = __SPIN_LOCK_UNLOCKED(name.lock),              \
  55        .task_list      = { &(name).task_list, &(name).task_list } }
  56
  57#define DECLARE_WAIT_QUEUE_HEAD(name) \
  58        wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
  59
  60#define __WAIT_BIT_KEY_INITIALIZER(word, bit)                           \
  61        { .flags = word, .bit_nr = bit, }
  62
  63extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
  64
  65#define init_waitqueue_head(q)                          \
  66        do {                                            \
  67                static struct lock_class_key __key;     \
  68                                                        \
  69                __init_waitqueue_head((q), #q, &__key); \
  70        } while (0)
  71
  72#ifdef CONFIG_LOCKDEP
  73# define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
  74        ({ init_waitqueue_head(&name); name; })
  75# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
  76        wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
  77#else
  78# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
  79#endif
  80
  81static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
  82{
  83        q->flags = 0;
  84        q->private = p;
  85        q->func = default_wake_function;
  86}
  87
  88static inline void init_waitqueue_func_entry(wait_queue_t *q,
  89                                        wait_queue_func_t func)
  90{
  91        q->flags = 0;
  92        q->private = NULL;
  93        q->func = func;
  94}
  95
  96static inline int waitqueue_active(wait_queue_head_t *q)
  97{
  98        return !list_empty(&q->task_list);
  99}
 100
 101extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
 102extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
 103extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
 104
 105static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
 106{
 107        list_add(&new->task_list, &head->task_list);
 108}
 109
 110/*
 111 * Used for wake-one threads:
 112 */
 113static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
 114                                              wait_queue_t *wait)
 115{
 116        wait->flags |= WQ_FLAG_EXCLUSIVE;
 117        __add_wait_queue(q, wait);
 118}
 119
 120static inline void __add_wait_queue_tail(wait_queue_head_t *head,
 121                                         wait_queue_t *new)
 122{
 123        list_add_tail(&new->task_list, &head->task_list);
 124}
 125
 126static inline void __add_wait_queue_tail_exclusive(wait_queue_head_t *q,
 127                                              wait_queue_t *wait)
 128{
 129        wait->flags |= WQ_FLAG_EXCLUSIVE;
 130        __add_wait_queue_tail(q, wait);
 131}
 132
 133static inline void __remove_wait_queue(wait_queue_head_t *head,
 134                                                        wait_queue_t *old)
 135{
 136        list_del(&old->task_list);
 137}
 138
 139void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
 140void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
 141void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
 142                        void *key);
 143void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
 144void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
 145void __wake_up_bit(wait_queue_head_t *, void *, int);
 146int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
 147int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
 148void wake_up_bit(void *, int);
 149int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
 150int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
 151wait_queue_head_t *bit_waitqueue(void *, int);
 152
 153#define wake_up(x)                      __wake_up(x, TASK_NORMAL, 1, NULL)
 154#define wake_up_nr(x, nr)               __wake_up(x, TASK_NORMAL, nr, NULL)
 155#define wake_up_all(x)                  __wake_up(x, TASK_NORMAL, 0, NULL)
 156#define wake_up_locked(x)               __wake_up_locked((x), TASK_NORMAL, 1)
 157#define wake_up_all_locked(x)           __wake_up_locked((x), TASK_NORMAL, 0)
 158
 159#define wake_up_interruptible(x)        __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
 160#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
 161#define wake_up_interruptible_all(x)    __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
 162#define wake_up_interruptible_sync(x)   __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
 163
 164/*
 165 * Wakeup macros to be used to report events to the targets.
 166 */
 167#define wake_up_poll(x, m)                              \
 168        __wake_up(x, TASK_NORMAL, 1, (void *) (m))
 169#define wake_up_locked_poll(x, m)                               \
 170        __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
 171#define wake_up_interruptible_poll(x, m)                        \
 172        __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
 173#define wake_up_interruptible_sync_poll(x, m)                           \
 174        __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
 175
 176#define __wait_event(wq, condition)                                     \
 177do {                                                                    \
 178        DEFINE_WAIT(__wait);                                            \
 179                                                                        \
 180        for (;;) {                                                      \
 181                prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
 182                if (condition)                                          \
 183                        break;                                          \
 184                schedule();                                             \
 185        }                                                               \
 186        finish_wait(&wq, &__wait);                                      \
 187} while (0)
 188
 189/**
 190 * wait_event - sleep until a condition gets true
 191 * @wq: the waitqueue to wait on
 192 * @condition: a C expression for the event to wait for
 193 *
 194 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
 195 * @condition evaluates to true. The @condition is checked each time
 196 * the waitqueue @wq is woken up.
 197 *
 198 * wake_up() has to be called after changing any variable that could
 199 * change the result of the wait condition.
 200 */
 201#define wait_event(wq, condition)                                       \
 202do {                                                                    \
 203        if (condition)                                                  \
 204                break;                                                  \
 205        __wait_event(wq, condition);                                    \
 206} while (0)
 207
 208#define __wait_event_timeout(wq, condition, ret)                        \
 209do {                                                                    \
 210        DEFINE_WAIT(__wait);                                            \
 211                                                                        \
 212        for (;;) {                                                      \
 213                prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
 214                if (condition)                                          \
 215                        break;                                          \
 216                ret = schedule_timeout(ret);                            \
 217                if (!ret)                                               \
 218                        break;                                          \
 219        }                                                               \
 220        if (!ret && (condition))                                        \
 221                ret = 1;                                                \
 222        finish_wait(&wq, &__wait);                                      \
 223} while (0)
 224
 225/**
 226 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
 227 * @wq: the waitqueue to wait on
 228 * @condition: a C expression for the event to wait for
 229 * @timeout: timeout, in jiffies
 230 *
 231 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
 232 * @condition evaluates to true. The @condition is checked each time
 233 * the waitqueue @wq is woken up.
 234 *
 235 * wake_up() has to be called after changing any variable that could
 236 * change the result of the wait condition.
 237 *
 238 * The function returns 0 if the @timeout elapsed, or the remaining
 239 * jiffies (at least 1) if the @condition evaluated to %true before
 240 * the @timeout elapsed.
 241 */
 242#define wait_event_timeout(wq, condition, timeout)                      \
 243({                                                                      \
 244        long __ret = timeout;                                           \
 245        if (!(condition))                                               \
 246                __wait_event_timeout(wq, condition, __ret);             \
 247        __ret;                                                          \
 248})
 249
 250#define __wait_event_interruptible(wq, condition, ret)                  \
 251do {                                                                    \
 252        DEFINE_WAIT(__wait);                                            \
 253                                                                        \
 254        for (;;) {                                                      \
 255                prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
 256                if (condition)                                          \
 257                        break;                                          \
 258                if (!signal_pending(current)) {                         \
 259                        schedule();                                     \
 260                        continue;                                       \
 261                }                                                       \
 262                ret = -ERESTARTSYS;                                     \
 263                break;                                                  \
 264        }                                                               \
 265        finish_wait(&wq, &__wait);                                      \
 266} while (0)
 267
 268/**
 269 * wait_event_interruptible - sleep until a condition gets true
 270 * @wq: the waitqueue to wait on
 271 * @condition: a C expression for the event to wait for
 272 *
 273 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
 274 * @condition evaluates to true or a signal is received.
 275 * The @condition is checked each time the waitqueue @wq is woken up.
 276 *
 277 * wake_up() has to be called after changing any variable that could
 278 * change the result of the wait condition.
 279 *
 280 * The function will return -ERESTARTSYS if it was interrupted by a
 281 * signal and 0 if @condition evaluated to true.
 282 */
 283#define wait_event_interruptible(wq, condition)                         \
 284({                                                                      \
 285        int __ret = 0;                                                  \
 286        if (!(condition))                                               \
 287                __wait_event_interruptible(wq, condition, __ret);       \
 288        __ret;                                                          \
 289})
 290
 291#define __wait_event_interruptible_timeout(wq, condition, ret)          \
 292do {                                                                    \
 293        DEFINE_WAIT(__wait);                                            \
 294                                                                        \
 295        for (;;) {                                                      \
 296                prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
 297                if (condition)                                          \
 298                        break;                                          \
 299                if (!signal_pending(current)) {                         \
 300                        ret = schedule_timeout(ret);                    \
 301                        if (!ret)                                       \
 302                                break;                                  \
 303                        continue;                                       \
 304                }                                                       \
 305                ret = -ERESTARTSYS;                                     \
 306                break;                                                  \
 307        }                                                               \
 308        if (!ret && (condition))                                        \
 309                ret = 1;                                                \
 310        finish_wait(&wq, &__wait);                                      \
 311} while (0)
 312
 313/**
 314 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
 315 * @wq: the waitqueue to wait on
 316 * @condition: a C expression for the event to wait for
 317 * @timeout: timeout, in jiffies
 318 *
 319 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
 320 * @condition evaluates to true or a signal is received.
 321 * The @condition is checked each time the waitqueue @wq is woken up.
 322 *
 323 * wake_up() has to be called after changing any variable that could
 324 * change the result of the wait condition.
 325 *
 326 * Returns:
 327 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
 328 * a signal, or the remaining jiffies (at least 1) if the @condition
 329 * evaluated to %true before the @timeout elapsed.
 330 */
 331#define wait_event_interruptible_timeout(wq, condition, timeout)        \
 332({                                                                      \
 333        long __ret = timeout;                                           \
 334        if (!(condition))                                               \
 335                __wait_event_interruptible_timeout(wq, condition, __ret); \
 336        __ret;                                                          \
 337})
 338
 339#define __wait_event_interruptible_exclusive(wq, condition, ret)        \
 340do {                                                                    \
 341        DEFINE_WAIT(__wait);                                            \
 342                                                                        \
 343        for (;;) {                                                      \
 344                prepare_to_wait_exclusive(&wq, &__wait,                 \
 345                                        TASK_INTERRUPTIBLE);            \
 346                if (condition) {                                        \
 347                        finish_wait(&wq, &__wait);                      \
 348                        break;                                          \
 349                }                                                       \
 350                if (!signal_pending(current)) {                         \
 351                        schedule();                                     \
 352                        continue;                                       \
 353                }                                                       \
 354                ret = -ERESTARTSYS;                                     \
 355                abort_exclusive_wait(&wq, &__wait,                      \
 356                                TASK_INTERRUPTIBLE, NULL);              \
 357                break;                                                  \
 358        }                                                               \
 359} while (0)
 360
 361#define wait_event_interruptible_exclusive(wq, condition)               \
 362({                                                                      \
 363        int __ret = 0;                                                  \
 364        if (!(condition))                                               \
 365                __wait_event_interruptible_exclusive(wq, condition, __ret);\
 366        __ret;                                                          \
 367})
 368
 369
 370#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
 371({                                                                      \
 372        int __ret = 0;                                                  \
 373        DEFINE_WAIT(__wait);                                            \
 374        if (exclusive)                                                  \
 375                __wait.flags |= WQ_FLAG_EXCLUSIVE;                      \
 376        do {                                                            \
 377                if (likely(list_empty(&__wait.task_list)))              \
 378                        __add_wait_queue_tail(&(wq), &__wait);          \
 379                set_current_state(TASK_INTERRUPTIBLE);                  \
 380                if (signal_pending(current)) {                          \
 381                        __ret = -ERESTARTSYS;                           \
 382                        break;                                          \
 383                }                                                       \
 384                if (irq)                                                \
 385                        spin_unlock_irq(&(wq).lock);                    \
 386                else                                                    \
 387                        spin_unlock(&(wq).lock);                        \
 388                schedule();                                             \
 389                if (irq)                                                \
 390                        spin_lock_irq(&(wq).lock);                      \
 391                else                                                    \
 392                        spin_lock(&(wq).lock);                          \
 393        } while (!(condition));                                         \
 394        __remove_wait_queue(&(wq), &__wait);                            \
 395        __set_current_state(TASK_RUNNING);                              \
 396        __ret;                                                          \
 397})
 398
 399
 400/**
 401 * wait_event_interruptible_locked - sleep until a condition gets true
 402 * @wq: the waitqueue to wait on
 403 * @condition: a C expression for the event to wait for
 404 *
 405 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
 406 * @condition evaluates to true or a signal is received.
 407 * The @condition is checked each time the waitqueue @wq is woken up.
 408 *
 409 * It must be called with wq.lock being held.  This spinlock is
 410 * unlocked while sleeping but @condition testing is done while lock
 411 * is held and when this macro exits the lock is held.
 412 *
 413 * The lock is locked/unlocked using spin_lock()/spin_unlock()
 414 * functions which must match the way they are locked/unlocked outside
 415 * of this macro.
 416 *
 417 * wake_up_locked() has to be called after changing any variable that could
 418 * change the result of the wait condition.
 419 *
 420 * The function will return -ERESTARTSYS if it was interrupted by a
 421 * signal and 0 if @condition evaluated to true.
 422 */
 423#define wait_event_interruptible_locked(wq, condition)                  \
 424        ((condition)                                                    \
 425         ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
 426
 427/**
 428 * wait_event_interruptible_locked_irq - sleep until a condition gets true
 429 * @wq: 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 is woken up.
 435 *
 436 * It must be called with wq.lock being held.  This spinlock is
 437 * unlocked while sleeping but @condition testing is done while lock
 438 * is held and when this macro exits the lock is held.
 439 *
 440 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
 441 * functions which must match the way they are locked/unlocked outside
 442 * of this macro.
 443 *
 444 * wake_up_locked() has to be called after changing any variable that could
 445 * change the result of the wait condition.
 446 *
 447 * The function will return -ERESTARTSYS if it was interrupted by a
 448 * signal and 0 if @condition evaluated to true.
 449 */
 450#define wait_event_interruptible_locked_irq(wq, condition)              \
 451        ((condition)                                                    \
 452         ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
 453
 454/**
 455 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
 456 * @wq: the waitqueue to wait on
 457 * @condition: a C expression for the event to wait for
 458 *
 459 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
 460 * @condition evaluates to true or a signal is received.
 461 * The @condition is checked each time the waitqueue @wq is woken up.
 462 *
 463 * It must be called with wq.lock being held.  This spinlock is
 464 * unlocked while sleeping but @condition testing is done while lock
 465 * is held and when this macro exits the lock is held.
 466 *
 467 * The lock is locked/unlocked using spin_lock()/spin_unlock()
 468 * functions which must match the way they are locked/unlocked outside
 469 * of this macro.
 470 *
 471 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
 472 * set thus when other process waits process on the list if this
 473 * process is awaken further processes are not considered.
 474 *
 475 * wake_up_locked() has to be called after changing any variable that could
 476 * change the result of the wait condition.
 477 *
 478 * The function will return -ERESTARTSYS if it was interrupted by a
 479 * signal and 0 if @condition evaluated to true.
 480 */
 481#define wait_event_interruptible_exclusive_locked(wq, condition)        \
 482        ((condition)                                                    \
 483         ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
 484
 485/**
 486 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
 487 * @wq: the waitqueue to wait on
 488 * @condition: a C expression for the event to wait for
 489 *
 490 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
 491 * @condition evaluates to true or a signal is received.
 492 * The @condition is checked each time the waitqueue @wq is woken up.
 493 *
 494 * It must be called with wq.lock being held.  This spinlock is
 495 * unlocked while sleeping but @condition testing is done while lock
 496 * is held and when this macro exits the lock is held.
 497 *
 498 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
 499 * functions which must match the way they are locked/unlocked outside
 500 * of this macro.
 501 *
 502 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
 503 * set thus when other process waits process on the list if this
 504 * process is awaken further processes are not considered.
 505 *
 506 * wake_up_locked() has to be called after changing any variable that could
 507 * change the result of the wait condition.
 508 *
 509 * The function will return -ERESTARTSYS if it was interrupted by a
 510 * signal and 0 if @condition evaluated to true.
 511 */
 512#define wait_event_interruptible_exclusive_locked_irq(wq, condition)    \
 513        ((condition)                                                    \
 514         ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
 515
 516
 517
 518#define __wait_event_killable(wq, condition, ret)                       \
 519do {                                                                    \
 520        DEFINE_WAIT(__wait);                                            \
 521                                                                        \
 522        for (;;) {                                                      \
 523                prepare_to_wait(&wq, &__wait, TASK_KILLABLE);           \
 524                if (condition)                                          \
 525                        break;                                          \
 526                if (!fatal_signal_pending(current)) {                   \
 527                        schedule();                                     \
 528                        continue;                                       \
 529                }                                                       \
 530                ret = -ERESTARTSYS;                                     \
 531                break;                                                  \
 532        }                                                               \
 533        finish_wait(&wq, &__wait);                                      \
 534} while (0)
 535
 536/**
 537 * wait_event_killable - sleep until a condition gets true
 538 * @wq: the waitqueue to wait on
 539 * @condition: a C expression for the event to wait for
 540 *
 541 * The process is put to sleep (TASK_KILLABLE) until the
 542 * @condition evaluates to true or a signal is received.
 543 * The @condition is checked each time the waitqueue @wq is woken up.
 544 *
 545 * wake_up() has to be called after changing any variable that could
 546 * change the result of the wait condition.
 547 *
 548 * The function will return -ERESTARTSYS if it was interrupted by a
 549 * signal and 0 if @condition evaluated to true.
 550 */
 551#define wait_event_killable(wq, condition)                              \
 552({                                                                      \
 553        int __ret = 0;                                                  \
 554        if (!(condition))                                               \
 555                __wait_event_killable(wq, condition, __ret);            \
 556        __ret;                                                          \
 557})
 558
 559
 560#define __wait_event_lock_irq(wq, condition, lock, cmd)                 \
 561do {                                                                    \
 562        DEFINE_WAIT(__wait);                                            \
 563                                                                        \
 564        for (;;) {                                                      \
 565                prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
 566                if (condition)                                          \
 567                        break;                                          \
 568                spin_unlock_irq(&lock);                                 \
 569                cmd;                                                    \
 570                schedule();                                             \
 571                spin_lock_irq(&lock);                                   \
 572        }                                                               \
 573        finish_wait(&wq, &__wait);                                      \
 574} while (0)
 575
 576/**
 577 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
 578 *                           condition is checked under the lock. This
 579 *                           is expected to be called with the lock
 580 *                           taken.
 581 * @wq: the waitqueue to wait on
 582 * @condition: a C expression for the event to wait for
 583 * @lock: a locked spinlock_t, which will be released before cmd
 584 *        and schedule() and reacquired afterwards.
 585 * @cmd: a command which is invoked outside the critical section before
 586 *       sleep
 587 *
 588 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
 589 * @condition evaluates to true. The @condition is checked each time
 590 * the waitqueue @wq is woken up.
 591 *
 592 * wake_up() has to be called after changing any variable that could
 593 * change the result of the wait condition.
 594 *
 595 * This is supposed to be called while holding the lock. The lock is
 596 * dropped before invoking the cmd and going to sleep and is reacquired
 597 * afterwards.
 598 */
 599#define wait_event_lock_irq_cmd(wq, condition, lock, cmd)               \
 600do {                                                                    \
 601        if (condition)                                                  \
 602                break;                                                  \
 603        __wait_event_lock_irq(wq, condition, lock, cmd);                \
 604} while (0)
 605
 606/**
 607 * wait_event_lock_irq - sleep until a condition gets true. The
 608 *                       condition is checked under the lock. This
 609 *                       is expected to be called with the lock
 610 *                       taken.
 611 * @wq: the waitqueue to wait on
 612 * @condition: a C expression for the event to wait for
 613 * @lock: a locked spinlock_t, which will be released before schedule()
 614 *        and reacquired afterwards.
 615 *
 616 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
 617 * @condition evaluates to true. The @condition is checked each time
 618 * the waitqueue @wq is woken up.
 619 *
 620 * wake_up() has to be called after changing any variable that could
 621 * change the result of the wait condition.
 622 *
 623 * This is supposed to be called while holding the lock. The lock is
 624 * dropped before going to sleep and is reacquired afterwards.
 625 */
 626#define wait_event_lock_irq(wq, condition, lock)                        \
 627do {                                                                    \
 628        if (condition)                                                  \
 629                break;                                                  \
 630        __wait_event_lock_irq(wq, condition, lock, );                   \
 631} while (0)
 632
 633
 634#define __wait_event_interruptible_lock_irq(wq, condition,              \
 635                                            lock, ret, cmd)             \
 636do {                                                                    \
 637        DEFINE_WAIT(__wait);                                            \
 638                                                                        \
 639        for (;;) {                                                      \
 640                prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
 641                if (condition)                                          \
 642                        break;                                          \
 643                if (signal_pending(current)) {                          \
 644                        ret = -ERESTARTSYS;                             \
 645                        break;                                          \
 646                }                                                       \
 647                spin_unlock_irq(&lock);                                 \
 648                cmd;                                                    \
 649                schedule();                                             \
 650                spin_lock_irq(&lock);                                   \
 651        }                                                               \
 652        finish_wait(&wq, &__wait);                                      \
 653} while (0)
 654
 655/**
 656 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
 657 *              The condition is checked under the lock. This is expected to
 658 *              be called with the lock taken.
 659 * @wq: the waitqueue to wait on
 660 * @condition: a C expression for the event to wait for
 661 * @lock: a locked spinlock_t, which will be released before cmd and
 662 *        schedule() and reacquired afterwards.
 663 * @cmd: a command which is invoked outside the critical section before
 664 *       sleep
 665 *
 666 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
 667 * @condition evaluates to true or a signal is received. The @condition is
 668 * checked each time the waitqueue @wq is woken up.
 669 *
 670 * wake_up() has to be called after changing any variable that could
 671 * change the result of the wait condition.
 672 *
 673 * This is supposed to be called while holding the lock. The lock is
 674 * dropped before invoking the cmd and going to sleep and is reacquired
 675 * afterwards.
 676 *
 677 * The macro will return -ERESTARTSYS if it was interrupted by a signal
 678 * and 0 if @condition evaluated to true.
 679 */
 680#define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
 681({                                                                      \
 682        int __ret = 0;                                                  \
 683                                                                        \
 684        if (!(condition))                                               \
 685                __wait_event_interruptible_lock_irq(wq, condition,      \
 686                                                    lock, __ret, cmd);  \
 687        __ret;                                                          \
 688})
 689
 690/**
 691 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
 692 *              The condition is checked under the lock. This is expected
 693 *              to be called with the lock taken.
 694 * @wq: the waitqueue to wait on
 695 * @condition: a C expression for the event to wait for
 696 * @lock: a locked spinlock_t, which will be released before schedule()
 697 *        and reacquired afterwards.
 698 *
 699 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
 700 * @condition evaluates to true or signal is received. The @condition is
 701 * checked each time the waitqueue @wq is woken up.
 702 *
 703 * wake_up() has to be called after changing any variable that could
 704 * change the result of the wait condition.
 705 *
 706 * This is supposed to be called while holding the lock. The lock is
 707 * dropped before going to sleep and is reacquired afterwards.
 708 *
 709 * The macro will return -ERESTARTSYS if it was interrupted by a signal
 710 * and 0 if @condition evaluated to true.
 711 */
 712#define wait_event_interruptible_lock_irq(wq, condition, lock)          \
 713({                                                                      \
 714        int __ret = 0;                                                  \
 715                                                                        \
 716        if (!(condition))                                               \
 717                __wait_event_interruptible_lock_irq(wq, condition,      \
 718                                                    lock, __ret, );     \
 719        __ret;                                                          \
 720})
 721
 722
 723/*
 724 * These are the old interfaces to sleep waiting for an event.
 725 * They are racy.  DO NOT use them, use the wait_event* interfaces above.
 726 * We plan to remove these interfaces.
 727 */
 728extern void sleep_on(wait_queue_head_t *q);
 729extern long sleep_on_timeout(wait_queue_head_t *q,
 730                                      signed long timeout);
 731extern void interruptible_sleep_on(wait_queue_head_t *q);
 732extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
 733                                           signed long timeout);
 734
 735/*
 736 * Waitqueues which are removed from the waitqueue_head at wakeup time
 737 */
 738void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
 739void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
 740void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
 741void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
 742                        unsigned int mode, void *key);
 743int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
 744int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
 745
 746#define DEFINE_WAIT_FUNC(name, function)                                \
 747        wait_queue_t name = {                                           \
 748                .private        = current,                              \
 749                .func           = function,                             \
 750                .task_list      = LIST_HEAD_INIT((name).task_list),     \
 751        }
 752
 753#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
 754
 755#define DEFINE_WAIT_BIT(name, word, bit)                                \
 756        struct wait_bit_queue name = {                                  \
 757                .key = __WAIT_BIT_KEY_INITIALIZER(word, bit),           \
 758                .wait   = {                                             \
 759                        .private        = current,                      \
 760                        .func           = wake_bit_function,            \
 761                        .task_list      =                               \
 762                                LIST_HEAD_INIT((name).wait.task_list),  \
 763                },                                                      \
 764        }
 765
 766#define init_wait(wait)                                                 \
 767        do {                                                            \
 768                (wait)->private = current;                              \
 769                (wait)->func = autoremove_wake_function;                \
 770                INIT_LIST_HEAD(&(wait)->task_list);                     \
 771                (wait)->flags = 0;                                      \
 772        } while (0)
 773
 774/**
 775 * wait_on_bit - wait for a bit to be cleared
 776 * @word: the word being waited on, a kernel virtual address
 777 * @bit: the bit of the word being waited on
 778 * @action: the function used to sleep, which may take special actions
 779 * @mode: the task state to sleep in
 780 *
 781 * There is a standard hashed waitqueue table for generic use. This
 782 * is the part of the hashtable's accessor API that waits on a bit.
 783 * For instance, if one were to have waiters on a bitflag, one would
 784 * call wait_on_bit() in threads waiting for the bit to clear.
 785 * One uses wait_on_bit() where one is waiting for the bit to clear,
 786 * but has no intention of setting it.
 787 */
 788static inline int wait_on_bit(void *word, int bit,
 789                                int (*action)(void *), unsigned mode)
 790{
 791        if (!test_bit(bit, word))
 792                return 0;
 793        return out_of_line_wait_on_bit(word, bit, action, mode);
 794}
 795
 796/**
 797 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
 798 * @word: the word being waited on, a kernel virtual address
 799 * @bit: the bit of the word being waited on
 800 * @action: the function used to sleep, which may take special actions
 801 * @mode: the task state to sleep in
 802 *
 803 * There is a standard hashed waitqueue table for generic use. This
 804 * is the part of the hashtable's accessor API that waits on a bit
 805 * when one intends to set it, for instance, trying to lock bitflags.
 806 * For instance, if one were to have waiters trying to set bitflag
 807 * and waiting for it to clear before setting it, one would call
 808 * wait_on_bit() in threads waiting to be able to set the bit.
 809 * One uses wait_on_bit_lock() where one is waiting for the bit to
 810 * clear with the intention of setting it, and when done, clearing it.
 811 */
 812static inline int wait_on_bit_lock(void *word, int bit,
 813                                int (*action)(void *), unsigned mode)
 814{
 815        if (!test_and_set_bit(bit, word))
 816                return 0;
 817        return out_of_line_wait_on_bit_lock(word, bit, action, mode);
 818}
 819        
 820#endif
 821
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.