wait.h 30 KB

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