wait.h 32 KB

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