wait.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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_cond_timeout(condition) \
  151. ({ \
  152. bool __cond = (condition); \
  153. if (__cond && !__ret) \
  154. __ret = 1; \
  155. __cond || !__ret; \
  156. })
  157. #define ___wait_signal_pending(state) \
  158. ((state == TASK_INTERRUPTIBLE && signal_pending(current)) || \
  159. (state == TASK_KILLABLE && fatal_signal_pending(current)))
  160. #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
  161. ({ \
  162. __label__ __out; \
  163. DEFINE_WAIT(__wait); \
  164. long __ret = ret; \
  165. \
  166. for (;;) { \
  167. if (exclusive) \
  168. prepare_to_wait_exclusive(&wq, &__wait, state); \
  169. else \
  170. prepare_to_wait(&wq, &__wait, state); \
  171. \
  172. if (condition) \
  173. break; \
  174. \
  175. if (___wait_signal_pending(state)) { \
  176. __ret = -ERESTARTSYS; \
  177. if (exclusive) { \
  178. abort_exclusive_wait(&wq, &__wait, \
  179. state, NULL); \
  180. goto __out; \
  181. } \
  182. break; \
  183. } \
  184. \
  185. cmd; \
  186. } \
  187. finish_wait(&wq, &__wait); \
  188. __out: __ret; \
  189. })
  190. #define __wait_event(wq, condition) \
  191. (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
  192. schedule())
  193. /**
  194. * wait_event - sleep until a condition gets true
  195. * @wq: the waitqueue to wait on
  196. * @condition: a C expression for the event to wait for
  197. *
  198. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  199. * @condition evaluates to true. The @condition is checked each time
  200. * the waitqueue @wq is woken up.
  201. *
  202. * wake_up() has to be called after changing any variable that could
  203. * change the result of the wait condition.
  204. */
  205. #define wait_event(wq, condition) \
  206. do { \
  207. if (condition) \
  208. break; \
  209. __wait_event(wq, condition); \
  210. } while (0)
  211. #define __wait_event_timeout(wq, condition, timeout) \
  212. ___wait_event(wq, ___wait_cond_timeout(condition), \
  213. TASK_UNINTERRUPTIBLE, 0, timeout, \
  214. __ret = schedule_timeout(__ret))
  215. /**
  216. * wait_event_timeout - sleep until a condition gets true or a timeout elapses
  217. * @wq: the waitqueue to wait on
  218. * @condition: a C expression for the event to wait for
  219. * @timeout: timeout, in jiffies
  220. *
  221. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  222. * @condition evaluates to true. The @condition is checked each time
  223. * the waitqueue @wq is woken up.
  224. *
  225. * wake_up() has to be called after changing any variable that could
  226. * change the result of the wait condition.
  227. *
  228. * The function returns 0 if the @timeout elapsed, or the remaining
  229. * jiffies (at least 1) if the @condition evaluated to %true before
  230. * the @timeout elapsed.
  231. */
  232. #define wait_event_timeout(wq, condition, timeout) \
  233. ({ \
  234. long __ret = timeout; \
  235. if (!(condition)) \
  236. __ret = __wait_event_timeout(wq, condition, timeout); \
  237. __ret; \
  238. })
  239. #define __wait_event_interruptible(wq, condition) \
  240. ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
  241. schedule())
  242. /**
  243. * wait_event_interruptible - sleep until a condition gets true
  244. * @wq: the waitqueue to wait on
  245. * @condition: a C expression for the event to wait for
  246. *
  247. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  248. * @condition evaluates to true or a signal is received.
  249. * The @condition is checked each time the waitqueue @wq is woken up.
  250. *
  251. * wake_up() has to be called after changing any variable that could
  252. * change the result of the wait condition.
  253. *
  254. * The function will return -ERESTARTSYS if it was interrupted by a
  255. * signal and 0 if @condition evaluated to true.
  256. */
  257. #define wait_event_interruptible(wq, condition) \
  258. ({ \
  259. int __ret = 0; \
  260. if (!(condition)) \
  261. __ret = __wait_event_interruptible(wq, condition); \
  262. __ret; \
  263. })
  264. #define __wait_event_interruptible_timeout(wq, condition, timeout) \
  265. ___wait_event(wq, ___wait_cond_timeout(condition), \
  266. TASK_INTERRUPTIBLE, 0, timeout, \
  267. __ret = schedule_timeout(__ret))
  268. /**
  269. * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
  270. * @wq: the waitqueue to wait on
  271. * @condition: a C expression for the event to wait for
  272. * @timeout: timeout, in jiffies
  273. *
  274. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  275. * @condition evaluates to true or a signal is received.
  276. * The @condition is checked each time the waitqueue @wq is woken up.
  277. *
  278. * wake_up() has to be called after changing any variable that could
  279. * change the result of the wait condition.
  280. *
  281. * Returns:
  282. * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
  283. * a signal, or the remaining jiffies (at least 1) if the @condition
  284. * evaluated to %true before the @timeout elapsed.
  285. */
  286. #define wait_event_interruptible_timeout(wq, condition, timeout) \
  287. ({ \
  288. long __ret = timeout; \
  289. if (!(condition)) \
  290. __ret = __wait_event_interruptible_timeout(wq, \
  291. condition, timeout); \
  292. __ret; \
  293. })
  294. #define __wait_event_hrtimeout(wq, condition, timeout, state) \
  295. ({ \
  296. int __ret = 0; \
  297. struct hrtimer_sleeper __t; \
  298. \
  299. hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
  300. HRTIMER_MODE_REL); \
  301. hrtimer_init_sleeper(&__t, current); \
  302. if ((timeout).tv64 != KTIME_MAX) \
  303. hrtimer_start_range_ns(&__t.timer, timeout, \
  304. current->timer_slack_ns, \
  305. HRTIMER_MODE_REL); \
  306. \
  307. __ret = ___wait_event(wq, condition, state, 0, 0, \
  308. if (!__t.task) { \
  309. __ret = -ETIME; \
  310. break; \
  311. } \
  312. schedule()); \
  313. \
  314. hrtimer_cancel(&__t.timer); \
  315. destroy_hrtimer_on_stack(&__t.timer); \
  316. __ret; \
  317. })
  318. /**
  319. * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
  320. * @wq: the waitqueue to wait on
  321. * @condition: a C expression for the event to wait for
  322. * @timeout: timeout, as a ktime_t
  323. *
  324. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  325. * @condition evaluates to true or a signal is received.
  326. * The @condition is checked each time the waitqueue @wq is woken up.
  327. *
  328. * wake_up() has to be called after changing any variable that could
  329. * change the result of the wait condition.
  330. *
  331. * The function returns 0 if @condition became true, or -ETIME if the timeout
  332. * elapsed.
  333. */
  334. #define wait_event_hrtimeout(wq, condition, timeout) \
  335. ({ \
  336. int __ret = 0; \
  337. if (!(condition)) \
  338. __ret = __wait_event_hrtimeout(wq, condition, timeout, \
  339. TASK_UNINTERRUPTIBLE); \
  340. __ret; \
  341. })
  342. /**
  343. * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
  344. * @wq: the waitqueue to wait on
  345. * @condition: a C expression for the event to wait for
  346. * @timeout: timeout, as a ktime_t
  347. *
  348. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  349. * @condition evaluates to true or a signal is received.
  350. * The @condition is checked each time the waitqueue @wq is woken up.
  351. *
  352. * wake_up() has to be called after changing any variable that could
  353. * change the result of the wait condition.
  354. *
  355. * The function returns 0 if @condition became true, -ERESTARTSYS if it was
  356. * interrupted by a signal, or -ETIME if the timeout elapsed.
  357. */
  358. #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
  359. ({ \
  360. long __ret = 0; \
  361. if (!(condition)) \
  362. __ret = __wait_event_hrtimeout(wq, condition, timeout, \
  363. TASK_INTERRUPTIBLE); \
  364. __ret; \
  365. })
  366. #define __wait_event_interruptible_exclusive(wq, condition) \
  367. ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
  368. schedule())
  369. #define wait_event_interruptible_exclusive(wq, condition) \
  370. ({ \
  371. int __ret = 0; \
  372. if (!(condition)) \
  373. __ret = __wait_event_interruptible_exclusive(wq, condition);\
  374. __ret; \
  375. })
  376. #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
  377. ({ \
  378. int __ret = 0; \
  379. DEFINE_WAIT(__wait); \
  380. if (exclusive) \
  381. __wait.flags |= WQ_FLAG_EXCLUSIVE; \
  382. do { \
  383. if (likely(list_empty(&__wait.task_list))) \
  384. __add_wait_queue_tail(&(wq), &__wait); \
  385. set_current_state(TASK_INTERRUPTIBLE); \
  386. if (signal_pending(current)) { \
  387. __ret = -ERESTARTSYS; \
  388. break; \
  389. } \
  390. if (irq) \
  391. spin_unlock_irq(&(wq).lock); \
  392. else \
  393. spin_unlock(&(wq).lock); \
  394. schedule(); \
  395. if (irq) \
  396. spin_lock_irq(&(wq).lock); \
  397. else \
  398. spin_lock(&(wq).lock); \
  399. } while (!(condition)); \
  400. __remove_wait_queue(&(wq), &__wait); \
  401. __set_current_state(TASK_RUNNING); \
  402. __ret; \
  403. })
  404. /**
  405. * wait_event_interruptible_locked - sleep until a condition gets true
  406. * @wq: the waitqueue to wait on
  407. * @condition: a C expression for the event to wait for
  408. *
  409. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  410. * @condition evaluates to true or a signal is received.
  411. * The @condition is checked each time the waitqueue @wq is woken up.
  412. *
  413. * It must be called with wq.lock being held. This spinlock is
  414. * unlocked while sleeping but @condition testing is done while lock
  415. * is held and when this macro exits the lock is held.
  416. *
  417. * The lock is locked/unlocked using spin_lock()/spin_unlock()
  418. * functions which must match the way they are locked/unlocked outside
  419. * of this macro.
  420. *
  421. * wake_up_locked() has to be called after changing any variable that could
  422. * change the result of the wait condition.
  423. *
  424. * The function will return -ERESTARTSYS if it was interrupted by a
  425. * signal and 0 if @condition evaluated to true.
  426. */
  427. #define wait_event_interruptible_locked(wq, condition) \
  428. ((condition) \
  429. ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
  430. /**
  431. * wait_event_interruptible_locked_irq - sleep until a condition gets true
  432. * @wq: the waitqueue to wait on
  433. * @condition: a C expression for the event to wait for
  434. *
  435. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  436. * @condition evaluates to true or a signal is received.
  437. * The @condition is checked each time the waitqueue @wq is woken up.
  438. *
  439. * It must be called with wq.lock being held. This spinlock is
  440. * unlocked while sleeping but @condition testing is done while lock
  441. * is held and when this macro exits the lock is held.
  442. *
  443. * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
  444. * functions which must match the way they are locked/unlocked outside
  445. * of this macro.
  446. *
  447. * wake_up_locked() has to be called after changing any variable that could
  448. * change the result of the wait condition.
  449. *
  450. * The function will return -ERESTARTSYS if it was interrupted by a
  451. * signal and 0 if @condition evaluated to true.
  452. */
  453. #define wait_event_interruptible_locked_irq(wq, condition) \
  454. ((condition) \
  455. ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
  456. /**
  457. * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
  458. * @wq: the waitqueue to wait on
  459. * @condition: a C expression for the event to wait for
  460. *
  461. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  462. * @condition evaluates to true or a signal is received.
  463. * The @condition is checked each time the waitqueue @wq is woken up.
  464. *
  465. * It must be called with wq.lock being held. This spinlock is
  466. * unlocked while sleeping but @condition testing is done while lock
  467. * is held and when this macro exits the lock is held.
  468. *
  469. * The lock is locked/unlocked using spin_lock()/spin_unlock()
  470. * functions which must match the way they are locked/unlocked outside
  471. * of this macro.
  472. *
  473. * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
  474. * set thus when other process waits process on the list if this
  475. * process is awaken further processes are not considered.
  476. *
  477. * wake_up_locked() has to be called after changing any variable that could
  478. * change the result of the wait condition.
  479. *
  480. * The function will return -ERESTARTSYS if it was interrupted by a
  481. * signal and 0 if @condition evaluated to true.
  482. */
  483. #define wait_event_interruptible_exclusive_locked(wq, condition) \
  484. ((condition) \
  485. ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
  486. /**
  487. * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
  488. * @wq: the waitqueue to wait on
  489. * @condition: a C expression for the event to wait for
  490. *
  491. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  492. * @condition evaluates to true or a signal is received.
  493. * The @condition is checked each time the waitqueue @wq is woken up.
  494. *
  495. * It must be called with wq.lock being held. This spinlock is
  496. * unlocked while sleeping but @condition testing is done while lock
  497. * is held and when this macro exits the lock is held.
  498. *
  499. * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
  500. * functions which must match the way they are locked/unlocked outside
  501. * of this macro.
  502. *
  503. * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
  504. * set thus when other process waits process on the list if this
  505. * process is awaken further processes are not considered.
  506. *
  507. * wake_up_locked() has to be called after changing any variable that could
  508. * change the result of the wait condition.
  509. *
  510. * The function will return -ERESTARTSYS if it was interrupted by a
  511. * signal and 0 if @condition evaluated to true.
  512. */
  513. #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
  514. ((condition) \
  515. ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
  516. #define __wait_event_killable(wq, condition) \
  517. ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
  518. /**
  519. * wait_event_killable - sleep until a condition gets true
  520. * @wq: the waitqueue to wait on
  521. * @condition: a C expression for the event to wait for
  522. *
  523. * The process is put to sleep (TASK_KILLABLE) until the
  524. * @condition evaluates to true or a signal is received.
  525. * The @condition is checked each time the waitqueue @wq is woken up.
  526. *
  527. * wake_up() has to be called after changing any variable that could
  528. * change the result of the wait condition.
  529. *
  530. * The function will return -ERESTARTSYS if it was interrupted by a
  531. * signal and 0 if @condition evaluated to true.
  532. */
  533. #define wait_event_killable(wq, condition) \
  534. ({ \
  535. int __ret = 0; \
  536. if (!(condition)) \
  537. __ret = __wait_event_killable(wq, condition); \
  538. __ret; \
  539. })
  540. #define __wait_event_lock_irq(wq, condition, lock, cmd) \
  541. (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
  542. spin_unlock_irq(&lock); \
  543. cmd; \
  544. schedule(); \
  545. spin_lock_irq(&lock))
  546. /**
  547. * wait_event_lock_irq_cmd - sleep until a condition gets true. The
  548. * condition is checked under the lock. This
  549. * is expected to be called with the lock
  550. * taken.
  551. * @wq: the waitqueue to wait on
  552. * @condition: a C expression for the event to wait for
  553. * @lock: a locked spinlock_t, which will be released before cmd
  554. * and schedule() and reacquired afterwards.
  555. * @cmd: a command which is invoked outside the critical section before
  556. * sleep
  557. *
  558. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  559. * @condition evaluates to true. The @condition is checked each time
  560. * the waitqueue @wq is woken up.
  561. *
  562. * wake_up() has to be called after changing any variable that could
  563. * change the result of the wait condition.
  564. *
  565. * This is supposed to be called while holding the lock. The lock is
  566. * dropped before invoking the cmd and going to sleep and is reacquired
  567. * afterwards.
  568. */
  569. #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
  570. do { \
  571. if (condition) \
  572. break; \
  573. __wait_event_lock_irq(wq, condition, lock, cmd); \
  574. } while (0)
  575. /**
  576. * wait_event_lock_irq - sleep until a condition gets true. The
  577. * condition is checked under the lock. This
  578. * is expected to be called with the lock
  579. * taken.
  580. * @wq: the waitqueue to wait on
  581. * @condition: a C expression for the event to wait for
  582. * @lock: a locked spinlock_t, which will be released before schedule()
  583. * and reacquired afterwards.
  584. *
  585. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  586. * @condition evaluates to true. The @condition is checked each time
  587. * the waitqueue @wq is woken up.
  588. *
  589. * wake_up() has to be called after changing any variable that could
  590. * change the result of the wait condition.
  591. *
  592. * This is supposed to be called while holding the lock. The lock is
  593. * dropped before going to sleep and is reacquired afterwards.
  594. */
  595. #define wait_event_lock_irq(wq, condition, lock) \
  596. do { \
  597. if (condition) \
  598. break; \
  599. __wait_event_lock_irq(wq, condition, lock, ); \
  600. } while (0)
  601. #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
  602. ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
  603. spin_unlock_irq(&lock); \
  604. cmd; \
  605. schedule(); \
  606. spin_lock_irq(&lock))
  607. /**
  608. * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
  609. * The condition is checked under the lock. This is expected to
  610. * be called with the lock taken.
  611. * @wq: the waitqueue to wait on
  612. * @condition: a C expression for the event to wait for
  613. * @lock: a locked spinlock_t, which will be released before cmd and
  614. * schedule() and reacquired afterwards.
  615. * @cmd: a command which is invoked outside the critical section before
  616. * sleep
  617. *
  618. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  619. * @condition evaluates to true or a signal is received. The @condition is
  620. * checked each time the waitqueue @wq is woken up.
  621. *
  622. * wake_up() has to be called after changing any variable that could
  623. * change the result of the wait condition.
  624. *
  625. * This is supposed to be called while holding the lock. The lock is
  626. * dropped before invoking the cmd and going to sleep and is reacquired
  627. * afterwards.
  628. *
  629. * The macro will return -ERESTARTSYS if it was interrupted by a signal
  630. * and 0 if @condition evaluated to true.
  631. */
  632. #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
  633. ({ \
  634. int __ret = 0; \
  635. if (!(condition)) \
  636. __ret = __wait_event_interruptible_lock_irq(wq, \
  637. condition, lock, cmd); \
  638. __ret; \
  639. })
  640. /**
  641. * wait_event_interruptible_lock_irq - sleep until a condition gets true.
  642. * The condition is checked under the lock. This is expected
  643. * to be called with the lock taken.
  644. * @wq: the waitqueue to wait on
  645. * @condition: a C expression for the event to wait for
  646. * @lock: a locked spinlock_t, which will be released before schedule()
  647. * and reacquired afterwards.
  648. *
  649. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  650. * @condition evaluates to true or signal is received. The @condition is
  651. * checked each time the waitqueue @wq is woken up.
  652. *
  653. * wake_up() has to be called after changing any variable that could
  654. * change the result of the wait condition.
  655. *
  656. * This is supposed to be called while holding the lock. The lock is
  657. * dropped before going to sleep and is reacquired afterwards.
  658. *
  659. * The macro will return -ERESTARTSYS if it was interrupted by a signal
  660. * and 0 if @condition evaluated to true.
  661. */
  662. #define wait_event_interruptible_lock_irq(wq, condition, lock) \
  663. ({ \
  664. int __ret = 0; \
  665. if (!(condition)) \
  666. __ret = __wait_event_interruptible_lock_irq(wq, \
  667. condition, lock,) \
  668. __ret; \
  669. })
  670. #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
  671. lock, timeout) \
  672. ___wait_event(wq, ___wait_cond_timeout(condition), \
  673. TASK_INTERRUPTIBLE, 0, ret, \
  674. spin_unlock_irq(&lock); \
  675. __ret = schedule_timeout(__ret); \
  676. spin_lock_irq(&lock));
  677. /**
  678. * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses.
  679. * The condition is checked under the lock. This is expected
  680. * to be called with the lock taken.
  681. * @wq: the waitqueue to wait on
  682. * @condition: a C expression for the event to wait for
  683. * @lock: a locked spinlock_t, which will be released before schedule()
  684. * and reacquired afterwards.
  685. * @timeout: timeout, in jiffies
  686. *
  687. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  688. * @condition evaluates to true or signal is received. The @condition is
  689. * checked each time the waitqueue @wq is woken up.
  690. *
  691. * wake_up() has to be called after changing any variable that could
  692. * change the result of the wait condition.
  693. *
  694. * This is supposed to be called while holding the lock. The lock is
  695. * dropped before going to sleep and is reacquired afterwards.
  696. *
  697. * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
  698. * was interrupted by a signal, and the remaining jiffies otherwise
  699. * if the condition evaluated to true before the timeout elapsed.
  700. */
  701. #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
  702. timeout) \
  703. ({ \
  704. long __ret = timeout; \
  705. if (!(condition)) \
  706. __ret = __wait_event_interruptible_lock_irq_timeout( \
  707. wq, condition, lock, timeout); \
  708. __ret; \
  709. })
  710. /*
  711. * These are the old interfaces to sleep waiting for an event.
  712. * They are racy. DO NOT use them, use the wait_event* interfaces above.
  713. * We plan to remove these interfaces.
  714. */
  715. extern void sleep_on(wait_queue_head_t *q);
  716. extern long sleep_on_timeout(wait_queue_head_t *q,
  717. signed long timeout);
  718. extern void interruptible_sleep_on(wait_queue_head_t *q);
  719. extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
  720. 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,
  728. unsigned int mode, void *key);
  729. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  730. int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  731. #define DEFINE_WAIT_FUNC(name, function) \
  732. wait_queue_t name = { \
  733. .private = current, \
  734. .func = function, \
  735. .task_list = LIST_HEAD_INIT((name).task_list), \
  736. }
  737. #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
  738. #define DEFINE_WAIT_BIT(name, word, bit) \
  739. struct wait_bit_queue name = { \
  740. .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
  741. .wait = { \
  742. .private = current, \
  743. .func = wake_bit_function, \
  744. .task_list = \
  745. LIST_HEAD_INIT((name).wait.task_list), \
  746. }, \
  747. }
  748. #define init_wait(wait) \
  749. do { \
  750. (wait)->private = current; \
  751. (wait)->func = autoremove_wake_function; \
  752. INIT_LIST_HEAD(&(wait)->task_list); \
  753. (wait)->flags = 0; \
  754. } while (0)
  755. /**
  756. * wait_on_bit - wait for a bit to be cleared
  757. * @word: the word being waited on, a kernel virtual address
  758. * @bit: the bit of the word being waited on
  759. * @action: the function used to sleep, which may take special actions
  760. * @mode: the task state to sleep in
  761. *
  762. * There is a standard hashed waitqueue table for generic use. This
  763. * is the part of the hashtable's accessor API that waits on a bit.
  764. * For instance, if one were to have waiters on a bitflag, one would
  765. * call wait_on_bit() in threads waiting for the bit to clear.
  766. * One uses wait_on_bit() where one is waiting for the bit to clear,
  767. * but has no intention of setting it.
  768. */
  769. static inline int wait_on_bit(void *word, int bit,
  770. int (*action)(void *), unsigned mode)
  771. {
  772. if (!test_bit(bit, word))
  773. return 0;
  774. return out_of_line_wait_on_bit(word, bit, action, mode);
  775. }
  776. /**
  777. * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
  778. * @word: the word being waited on, a kernel virtual address
  779. * @bit: the bit of the word being waited on
  780. * @action: the function used to sleep, which may take special actions
  781. * @mode: the task state to sleep in
  782. *
  783. * There is a standard hashed waitqueue table for generic use. This
  784. * is the part of the hashtable's accessor API that waits on a bit
  785. * when one intends to set it, for instance, trying to lock bitflags.
  786. * For instance, if one were to have waiters trying to set bitflag
  787. * and waiting for it to clear before setting it, one would call
  788. * wait_on_bit() in threads waiting to be able to set the bit.
  789. * One uses wait_on_bit_lock() where one is waiting for the bit to
  790. * clear with the intention of setting it, and when done, clearing it.
  791. */
  792. static inline int wait_on_bit_lock(void *word, int bit,
  793. int (*action)(void *), unsigned mode)
  794. {
  795. if (!test_and_set_bit(bit, word))
  796. return 0;
  797. return out_of_line_wait_on_bit_lock(word, bit, action, mode);
  798. }
  799. /**
  800. * wait_on_atomic_t - Wait for an atomic_t to become 0
  801. * @val: The atomic value being waited on, a kernel virtual address
  802. * @action: the function used to sleep, which may take special actions
  803. * @mode: the task state to sleep in
  804. *
  805. * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
  806. * the purpose of getting a waitqueue, but we set the key to a bit number
  807. * outside of the target 'word'.
  808. */
  809. static inline
  810. int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
  811. {
  812. if (atomic_read(val) == 0)
  813. return 0;
  814. return out_of_line_wait_on_atomic_t(val, action, mode);
  815. }
  816. #endif