wait.h 29 KB

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