wait.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. #ifndef _LINUX_WAIT_H
  2. #define _LINUX_WAIT_H
  3. #define WNOHANG 0x00000001
  4. #define WUNTRACED 0x00000002
  5. #define WSTOPPED WUNTRACED
  6. #define WEXITED 0x00000004
  7. #define WCONTINUED 0x00000008
  8. #define WNOWAIT 0x01000000 /* Don't reap, just poll status. */
  9. #define __WNOTHREAD 0x20000000 /* Don't wait on children of other threads in this group */
  10. #define __WALL 0x40000000 /* Wait on all children, regardless of type */
  11. #define __WCLONE 0x80000000 /* Wait only on non-SIGCHLD children */
  12. /* First argument to waitid: */
  13. #define P_ALL 0
  14. #define P_PID 1
  15. #define P_PGID 2
  16. #ifdef __KERNEL__
  17. #include <linux/list.h>
  18. #include <linux/stddef.h>
  19. #include <linux/spinlock.h>
  20. #include <asm/system.h>
  21. #include <asm/current.h>
  22. typedef struct __wait_queue wait_queue_t;
  23. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  24. int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
  25. struct __wait_queue {
  26. unsigned int flags;
  27. #define WQ_FLAG_EXCLUSIVE 0x01
  28. void *private;
  29. wait_queue_func_t func;
  30. struct list_head task_list;
  31. };
  32. struct wait_bit_key {
  33. void *flags;
  34. int bit_nr;
  35. };
  36. struct wait_bit_queue {
  37. struct wait_bit_key key;
  38. wait_queue_t wait;
  39. };
  40. struct __wait_queue_head {
  41. spinlock_t lock;
  42. struct list_head task_list;
  43. };
  44. typedef struct __wait_queue_head wait_queue_head_t;
  45. struct task_struct;
  46. /*
  47. * Macros for declaration and initialisaton of the datatypes
  48. */
  49. #define __WAITQUEUE_INITIALIZER(name, tsk) { \
  50. .private = tsk, \
  51. .func = default_wake_function, \
  52. .task_list = { NULL, NULL } }
  53. #define DECLARE_WAITQUEUE(name, tsk) \
  54. wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
  55. #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
  56. .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
  57. .task_list = { &(name).task_list, &(name).task_list } }
  58. #define DECLARE_WAIT_QUEUE_HEAD(name) \
  59. wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
  60. #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
  61. { .flags = word, .bit_nr = bit, }
  62. extern void __init_waitqueue_head(wait_queue_head_t *q, struct lock_class_key *);
  63. #define init_waitqueue_head(q) \
  64. do { \
  65. static struct lock_class_key __key; \
  66. \
  67. __init_waitqueue_head((q), &__key); \
  68. } while (0)
  69. #ifdef CONFIG_LOCKDEP
  70. # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
  71. ({ init_waitqueue_head(&name); name; })
  72. # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
  73. wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
  74. #else
  75. # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
  76. #endif
  77. static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
  78. {
  79. q->flags = 0;
  80. q->private = p;
  81. q->func = default_wake_function;
  82. }
  83. static inline void init_waitqueue_func_entry(wait_queue_t *q,
  84. wait_queue_func_t func)
  85. {
  86. q->flags = 0;
  87. q->private = NULL;
  88. q->func = func;
  89. }
  90. static inline int waitqueue_active(wait_queue_head_t *q)
  91. {
  92. return !list_empty(&q->task_list);
  93. }
  94. extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
  95. extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
  96. extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
  97. static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
  98. {
  99. list_add(&new->task_list, &head->task_list);
  100. }
  101. /*
  102. * Used for wake-one threads:
  103. */
  104. static inline void __add_wait_queue_tail(wait_queue_head_t *head,
  105. wait_queue_t *new)
  106. {
  107. list_add_tail(&new->task_list, &head->task_list);
  108. }
  109. static inline void __remove_wait_queue(wait_queue_head_t *head,
  110. wait_queue_t *old)
  111. {
  112. list_del(&old->task_list);
  113. }
  114. void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
  115. void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
  116. void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
  117. void *key);
  118. void __wake_up_locked(wait_queue_head_t *q, unsigned int mode);
  119. void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
  120. void __wake_up_bit(wait_queue_head_t *, void *, int);
  121. int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
  122. int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
  123. void wake_up_bit(void *, int);
  124. int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
  125. int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
  126. wait_queue_head_t *bit_waitqueue(void *, int);
  127. #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
  128. #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
  129. #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
  130. #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL)
  131. #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
  132. #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
  133. #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
  134. #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
  135. /*
  136. * Wakeup macros to be used to report events to the targets.
  137. */
  138. #define wake_up_poll(x, m) \
  139. __wake_up(x, TASK_NORMAL, 1, (void *) (m))
  140. #define wake_up_locked_poll(x, m) \
  141. __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
  142. #define wake_up_interruptible_poll(x, m) \
  143. __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
  144. #define wake_up_interruptible_sync_poll(x, m) \
  145. __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
  146. #define __wait_event(wq, condition) \
  147. do { \
  148. DEFINE_WAIT(__wait); \
  149. \
  150. for (;;) { \
  151. prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
  152. if (condition) \
  153. break; \
  154. schedule(); \
  155. } \
  156. finish_wait(&wq, &__wait); \
  157. } while (0)
  158. /**
  159. * wait_event - sleep until a condition gets true
  160. * @wq: the waitqueue to wait on
  161. * @condition: a C expression for the event to wait for
  162. *
  163. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  164. * @condition evaluates to true. The @condition is checked each time
  165. * the waitqueue @wq is woken up.
  166. *
  167. * wake_up() has to be called after changing any variable that could
  168. * change the result of the wait condition.
  169. */
  170. #define wait_event(wq, condition) \
  171. do { \
  172. if (condition) \
  173. break; \
  174. __wait_event(wq, condition); \
  175. } while (0)
  176. #define __wait_event_timeout(wq, condition, ret) \
  177. do { \
  178. DEFINE_WAIT(__wait); \
  179. \
  180. for (;;) { \
  181. prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
  182. if (condition) \
  183. break; \
  184. ret = schedule_timeout(ret); \
  185. if (!ret) \
  186. break; \
  187. } \
  188. finish_wait(&wq, &__wait); \
  189. } while (0)
  190. /**
  191. * wait_event_timeout - sleep until a condition gets true or a timeout elapses
  192. * @wq: the waitqueue to wait on
  193. * @condition: a C expression for the event to wait for
  194. * @timeout: timeout, in jiffies
  195. *
  196. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  197. * @condition evaluates to true. The @condition is checked each time
  198. * the waitqueue @wq is woken up.
  199. *
  200. * wake_up() has to be called after changing any variable that could
  201. * change the result of the wait condition.
  202. *
  203. * The function returns 0 if the @timeout elapsed, and the remaining
  204. * jiffies if the condition evaluated to true before the timeout elapsed.
  205. */
  206. #define wait_event_timeout(wq, condition, timeout) \
  207. ({ \
  208. long __ret = timeout; \
  209. if (!(condition)) \
  210. __wait_event_timeout(wq, condition, __ret); \
  211. __ret; \
  212. })
  213. #define __wait_event_interruptible(wq, condition, ret) \
  214. do { \
  215. DEFINE_WAIT(__wait); \
  216. \
  217. for (;;) { \
  218. prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
  219. if (condition) \
  220. break; \
  221. if (!signal_pending(current)) { \
  222. schedule(); \
  223. continue; \
  224. } \
  225. ret = -ERESTARTSYS; \
  226. break; \
  227. } \
  228. finish_wait(&wq, &__wait); \
  229. } while (0)
  230. /**
  231. * wait_event_interruptible - sleep until a condition gets true
  232. * @wq: the waitqueue to wait on
  233. * @condition: a C expression for the event to wait for
  234. *
  235. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  236. * @condition evaluates to true or a signal is received.
  237. * The @condition is checked each time the waitqueue @wq is woken up.
  238. *
  239. * wake_up() has to be called after changing any variable that could
  240. * change the result of the wait condition.
  241. *
  242. * The function will return -ERESTARTSYS if it was interrupted by a
  243. * signal and 0 if @condition evaluated to true.
  244. */
  245. #define wait_event_interruptible(wq, condition) \
  246. ({ \
  247. int __ret = 0; \
  248. if (!(condition)) \
  249. __wait_event_interruptible(wq, condition, __ret); \
  250. __ret; \
  251. })
  252. #define __wait_event_interruptible_timeout(wq, condition, ret) \
  253. do { \
  254. DEFINE_WAIT(__wait); \
  255. \
  256. for (;;) { \
  257. prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
  258. if (condition) \
  259. break; \
  260. if (!signal_pending(current)) { \
  261. ret = schedule_timeout(ret); \
  262. if (!ret) \
  263. break; \
  264. continue; \
  265. } \
  266. ret = -ERESTARTSYS; \
  267. break; \
  268. } \
  269. finish_wait(&wq, &__wait); \
  270. } while (0)
  271. /**
  272. * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
  273. * @wq: the waitqueue to wait on
  274. * @condition: a C expression for the event to wait for
  275. * @timeout: timeout, in jiffies
  276. *
  277. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  278. * @condition evaluates to true or a signal is received.
  279. * The @condition is checked each time the waitqueue @wq is woken up.
  280. *
  281. * wake_up() has to be called after changing any variable that could
  282. * change the result of the wait condition.
  283. *
  284. * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
  285. * was interrupted by a signal, and the remaining jiffies otherwise
  286. * if the condition evaluated to true before the timeout elapsed.
  287. */
  288. #define wait_event_interruptible_timeout(wq, condition, timeout) \
  289. ({ \
  290. long __ret = timeout; \
  291. if (!(condition)) \
  292. __wait_event_interruptible_timeout(wq, condition, __ret); \
  293. __ret; \
  294. })
  295. #define __wait_event_interruptible_exclusive(wq, condition, ret) \
  296. do { \
  297. DEFINE_WAIT(__wait); \
  298. \
  299. for (;;) { \
  300. prepare_to_wait_exclusive(&wq, &__wait, \
  301. TASK_INTERRUPTIBLE); \
  302. if (condition) { \
  303. finish_wait(&wq, &__wait); \
  304. break; \
  305. } \
  306. if (!signal_pending(current)) { \
  307. schedule(); \
  308. continue; \
  309. } \
  310. ret = -ERESTARTSYS; \
  311. abort_exclusive_wait(&wq, &__wait, \
  312. TASK_INTERRUPTIBLE, NULL); \
  313. break; \
  314. } \
  315. } while (0)
  316. #define wait_event_interruptible_exclusive(wq, condition) \
  317. ({ \
  318. int __ret = 0; \
  319. if (!(condition)) \
  320. __wait_event_interruptible_exclusive(wq, condition, __ret);\
  321. __ret; \
  322. })
  323. #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
  324. ({ \
  325. int __ret = 0; \
  326. DEFINE_WAIT(__wait); \
  327. if (exclusive) \
  328. __wait.flags |= WQ_FLAG_EXCLUSIVE; \
  329. do { \
  330. if (likely(list_empty(&__wait.task_list))) \
  331. __add_wait_queue_tail(&(wq), &__wait); \
  332. set_current_state(TASK_INTERRUPTIBLE); \
  333. if (signal_pending(current)) { \
  334. __ret = -ERESTARTSYS; \
  335. break; \
  336. } \
  337. if (irq) \
  338. spin_unlock_irq(&(wq).lock); \
  339. else \
  340. spin_unlock(&(wq).lock); \
  341. schedule(); \
  342. if (irq) \
  343. spin_lock_irq(&(wq).lock); \
  344. else \
  345. spin_lock(&(wq).lock); \
  346. } while (!(condition)); \
  347. __remove_wait_queue(&(wq), &__wait); \
  348. __set_current_state(TASK_RUNNING); \
  349. __ret; \
  350. })
  351. /**
  352. * wait_event_interruptible_locked - sleep until a condition gets true
  353. * @wq: the waitqueue to wait on
  354. * @condition: a C expression for the event to wait for
  355. *
  356. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  357. * @condition evaluates to true or a signal is received.
  358. * The @condition is checked each time the waitqueue @wq is woken up.
  359. *
  360. * It must be called with wq.lock being held. This spinlock is
  361. * unlocked while sleeping but @condition testing is done while lock
  362. * is held and when this macro exits the lock is held.
  363. *
  364. * The lock is locked/unlocked using spin_lock()/spin_unlock()
  365. * functions which must match the way they are locked/unlocked outside
  366. * of this macro.
  367. *
  368. * wake_up_locked() has to be called after changing any variable that could
  369. * change the result of the wait condition.
  370. *
  371. * The function will return -ERESTARTSYS if it was interrupted by a
  372. * signal and 0 if @condition evaluated to true.
  373. */
  374. #define wait_event_interruptible_locked(wq, condition) \
  375. ((condition) \
  376. ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
  377. /**
  378. * wait_event_interruptible_locked_irq - sleep until a condition gets true
  379. * @wq: the waitqueue to wait on
  380. * @condition: a C expression for the event to wait for
  381. *
  382. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  383. * @condition evaluates to true or a signal is received.
  384. * The @condition is checked each time the waitqueue @wq is woken up.
  385. *
  386. * It must be called with wq.lock being held. This spinlock is
  387. * unlocked while sleeping but @condition testing is done while lock
  388. * is held and when this macro exits the lock is held.
  389. *
  390. * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
  391. * functions which must match the way they are locked/unlocked outside
  392. * of this macro.
  393. *
  394. * wake_up_locked() has to be called after changing any variable that could
  395. * change the result of the wait condition.
  396. *
  397. * The function will return -ERESTARTSYS if it was interrupted by a
  398. * signal and 0 if @condition evaluated to true.
  399. */
  400. #define wait_event_interruptible_locked_irq(wq, condition) \
  401. ((condition) \
  402. ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
  403. /**
  404. * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
  405. * @wq: the waitqueue to wait on
  406. * @condition: a C expression for the event to wait for
  407. *
  408. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  409. * @condition evaluates to true or a signal is received.
  410. * The @condition is checked each time the waitqueue @wq is woken up.
  411. *
  412. * It must be called with wq.lock being held. This spinlock is
  413. * unlocked while sleeping but @condition testing is done while lock
  414. * is held and when this macro exits the lock is held.
  415. *
  416. * The lock is locked/unlocked using spin_lock()/spin_unlock()
  417. * functions which must match the way they are locked/unlocked outside
  418. * of this macro.
  419. *
  420. * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
  421. * set thus when other process waits process on the list if this
  422. * process is awaken further processes are not considered.
  423. *
  424. * wake_up_locked() has to be called after changing any variable that could
  425. * change the result of the wait condition.
  426. *
  427. * The function will return -ERESTARTSYS if it was interrupted by a
  428. * signal and 0 if @condition evaluated to true.
  429. */
  430. #define wait_event_interruptible_exclusive_locked(wq, condition) \
  431. ((condition) \
  432. ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
  433. /**
  434. * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
  435. * @wq: the waitqueue to wait on
  436. * @condition: a C expression for the event to wait for
  437. *
  438. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  439. * @condition evaluates to true or a signal is received.
  440. * The @condition is checked each time the waitqueue @wq is woken up.
  441. *
  442. * It must be called with wq.lock being held. This spinlock is
  443. * unlocked while sleeping but @condition testing is done while lock
  444. * is held and when this macro exits the lock is held.
  445. *
  446. * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
  447. * functions which must match the way they are locked/unlocked outside
  448. * of this macro.
  449. *
  450. * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
  451. * set thus when other process waits process on the list if this
  452. * process is awaken further processes are not considered.
  453. *
  454. * wake_up_locked() has to be called after changing any variable that could
  455. * change the result of the wait condition.
  456. *
  457. * The function will return -ERESTARTSYS if it was interrupted by a
  458. * signal and 0 if @condition evaluated to true.
  459. */
  460. #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
  461. ((condition) \
  462. ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
  463. #define __wait_event_killable(wq, condition, ret) \
  464. do { \
  465. DEFINE_WAIT(__wait); \
  466. \
  467. for (;;) { \
  468. prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
  469. if (condition) \
  470. break; \
  471. if (!fatal_signal_pending(current)) { \
  472. schedule(); \
  473. continue; \
  474. } \
  475. ret = -ERESTARTSYS; \
  476. break; \
  477. } \
  478. finish_wait(&wq, &__wait); \
  479. } while (0)
  480. /**
  481. * wait_event_killable - sleep until a condition gets true
  482. * @wq: the waitqueue to wait on
  483. * @condition: a C expression for the event to wait for
  484. *
  485. * The process is put to sleep (TASK_KILLABLE) until the
  486. * @condition evaluates to true or a signal is received.
  487. * The @condition is checked each time the waitqueue @wq is woken up.
  488. *
  489. * wake_up() has to be called after changing any variable that could
  490. * change the result of the wait condition.
  491. *
  492. * The function will return -ERESTARTSYS if it was interrupted by a
  493. * signal and 0 if @condition evaluated to true.
  494. */
  495. #define wait_event_killable(wq, condition) \
  496. ({ \
  497. int __ret = 0; \
  498. if (!(condition)) \
  499. __wait_event_killable(wq, condition, __ret); \
  500. __ret; \
  501. })
  502. /*
  503. * Must be called with the spinlock in the wait_queue_head_t held.
  504. */
  505. static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
  506. wait_queue_t * wait)
  507. {
  508. wait->flags |= WQ_FLAG_EXCLUSIVE;
  509. __add_wait_queue_tail(q, wait);
  510. }
  511. /*
  512. * Must be called with the spinlock in the wait_queue_head_t held.
  513. */
  514. static inline void remove_wait_queue_locked(wait_queue_head_t *q,
  515. wait_queue_t * wait)
  516. {
  517. __remove_wait_queue(q, wait);
  518. }
  519. /*
  520. * These are the old interfaces to sleep waiting for an event.
  521. * They are racy. DO NOT use them, use the wait_event* interfaces above.
  522. * We plan to remove these interfaces.
  523. */
  524. extern void sleep_on(wait_queue_head_t *q);
  525. extern long sleep_on_timeout(wait_queue_head_t *q,
  526. signed long timeout);
  527. extern void interruptible_sleep_on(wait_queue_head_t *q);
  528. extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
  529. signed long timeout);
  530. /*
  531. * Waitqueues which are removed from the waitqueue_head at wakeup time
  532. */
  533. void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
  534. void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
  535. void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
  536. void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
  537. unsigned int mode, void *key);
  538. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  539. int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  540. #define DEFINE_WAIT_FUNC(name, function) \
  541. wait_queue_t name = { \
  542. .private = current, \
  543. .func = function, \
  544. .task_list = LIST_HEAD_INIT((name).task_list), \
  545. }
  546. #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
  547. #define DEFINE_WAIT_BIT(name, word, bit) \
  548. struct wait_bit_queue name = { \
  549. .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
  550. .wait = { \
  551. .private = current, \
  552. .func = wake_bit_function, \
  553. .task_list = \
  554. LIST_HEAD_INIT((name).wait.task_list), \
  555. }, \
  556. }
  557. #define init_wait(wait) \
  558. do { \
  559. (wait)->private = current; \
  560. (wait)->func = autoremove_wake_function; \
  561. INIT_LIST_HEAD(&(wait)->task_list); \
  562. } while (0)
  563. /**
  564. * wait_on_bit - wait for a bit to be cleared
  565. * @word: the word being waited on, a kernel virtual address
  566. * @bit: the bit of the word being waited on
  567. * @action: the function used to sleep, which may take special actions
  568. * @mode: the task state to sleep in
  569. *
  570. * There is a standard hashed waitqueue table for generic use. This
  571. * is the part of the hashtable's accessor API that waits on a bit.
  572. * For instance, if one were to have waiters on a bitflag, one would
  573. * call wait_on_bit() in threads waiting for the bit to clear.
  574. * One uses wait_on_bit() where one is waiting for the bit to clear,
  575. * but has no intention of setting it.
  576. */
  577. static inline int wait_on_bit(void *word, int bit,
  578. int (*action)(void *), unsigned mode)
  579. {
  580. if (!test_bit(bit, word))
  581. return 0;
  582. return out_of_line_wait_on_bit(word, bit, action, mode);
  583. }
  584. /**
  585. * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
  586. * @word: the word being waited on, a kernel virtual address
  587. * @bit: the bit of the word being waited on
  588. * @action: the function used to sleep, which may take special actions
  589. * @mode: the task state to sleep in
  590. *
  591. * There is a standard hashed waitqueue table for generic use. This
  592. * is the part of the hashtable's accessor API that waits on a bit
  593. * when one intends to set it, for instance, trying to lock bitflags.
  594. * For instance, if one were to have waiters trying to set bitflag
  595. * and waiting for it to clear before setting it, one would call
  596. * wait_on_bit() in threads waiting to be able to set the bit.
  597. * One uses wait_on_bit_lock() where one is waiting for the bit to
  598. * clear with the intention of setting it, and when done, clearing it.
  599. */
  600. static inline int wait_on_bit_lock(void *word, int bit,
  601. int (*action)(void *), unsigned mode)
  602. {
  603. if (!test_and_set_bit(bit, word))
  604. return 0;
  605. return out_of_line_wait_on_bit_lock(word, bit, action, mode);
  606. }
  607. #endif /* __KERNEL__ */
  608. #endif