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