wait.h 30 KB

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