wait.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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/config.h>
  18. #include <linux/list.h>
  19. #include <linux/stddef.h>
  20. #include <linux/spinlock.h>
  21. #include <asm/system.h>
  22. #include <asm/current.h>
  23. typedef struct __wait_queue wait_queue_t;
  24. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int sync, void *key);
  25. int default_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  26. struct __wait_queue {
  27. unsigned int flags;
  28. #define WQ_FLAG_EXCLUSIVE 0x01
  29. void *private;
  30. wait_queue_func_t func;
  31. struct list_head task_list;
  32. };
  33. struct wait_bit_key {
  34. void *flags;
  35. int bit_nr;
  36. };
  37. struct wait_bit_queue {
  38. struct wait_bit_key key;
  39. wait_queue_t wait;
  40. };
  41. struct __wait_queue_head {
  42. spinlock_t lock;
  43. struct list_head task_list;
  44. };
  45. typedef struct __wait_queue_head wait_queue_head_t;
  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, \
  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. static inline void init_waitqueue_head(wait_queue_head_t *q)
  63. {
  64. spin_lock_init(&q->lock);
  65. INIT_LIST_HEAD(&q->task_list);
  66. }
  67. static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
  68. {
  69. q->flags = 0;
  70. q->private = p;
  71. q->func = default_wake_function;
  72. }
  73. static inline void init_waitqueue_func_entry(wait_queue_t *q,
  74. wait_queue_func_t func)
  75. {
  76. q->flags = 0;
  77. q->private = NULL;
  78. q->func = func;
  79. }
  80. static inline int waitqueue_active(wait_queue_head_t *q)
  81. {
  82. return !list_empty(&q->task_list);
  83. }
  84. /*
  85. * Used to distinguish between sync and async io wait context:
  86. * sync i/o typically specifies a NULL wait queue entry or a wait
  87. * queue entry bound to a task (current task) to wake up.
  88. * aio specifies a wait queue entry with an async notification
  89. * callback routine, not associated with any task.
  90. */
  91. #define is_sync_wait(wait) (!(wait) || ((wait)->private))
  92. extern void FASTCALL(add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));
  93. extern void FASTCALL(add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait));
  94. extern void FASTCALL(remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));
  95. static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
  96. {
  97. list_add(&new->task_list, &head->task_list);
  98. }
  99. /*
  100. * Used for wake-one threads:
  101. */
  102. static inline void __add_wait_queue_tail(wait_queue_head_t *head,
  103. wait_queue_t *new)
  104. {
  105. list_add_tail(&new->task_list, &head->task_list);
  106. }
  107. static inline void __remove_wait_queue(wait_queue_head_t *head,
  108. wait_queue_t *old)
  109. {
  110. list_del(&old->task_list);
  111. }
  112. void FASTCALL(__wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key));
  113. extern void FASTCALL(__wake_up_locked(wait_queue_head_t *q, unsigned int mode));
  114. extern void FASTCALL(__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr));
  115. void FASTCALL(__wake_up_bit(wait_queue_head_t *, void *, int));
  116. int FASTCALL(__wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned));
  117. int FASTCALL(__wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned));
  118. void FASTCALL(wake_up_bit(void *, int));
  119. int FASTCALL(out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned));
  120. int FASTCALL(out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned));
  121. wait_queue_head_t *FASTCALL(bit_waitqueue(void *, int));
  122. #define wake_up(x) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1, NULL)
  123. #define wake_up_nr(x, nr) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, nr, NULL)
  124. #define wake_up_all(x) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 0, NULL)
  125. #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
  126. #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
  127. #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
  128. #define wake_up_locked(x) __wake_up_locked((x), TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE)
  129. #define wake_up_interruptible_sync(x) __wake_up_sync((x),TASK_INTERRUPTIBLE, 1)
  130. #define __wait_event(wq, condition) \
  131. do { \
  132. DEFINE_WAIT(__wait); \
  133. \
  134. for (;;) { \
  135. prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
  136. if (condition) \
  137. break; \
  138. schedule(); \
  139. } \
  140. finish_wait(&wq, &__wait); \
  141. } while (0)
  142. /**
  143. * wait_event - sleep until a condition gets true
  144. * @wq: the waitqueue to wait on
  145. * @condition: a C expression for the event to wait for
  146. *
  147. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  148. * @condition evaluates to true. The @condition is checked each time
  149. * the waitqueue @wq is woken up.
  150. *
  151. * wake_up() has to be called after changing any variable that could
  152. * change the result of the wait condition.
  153. */
  154. #define wait_event(wq, condition) \
  155. do { \
  156. if (condition) \
  157. break; \
  158. __wait_event(wq, condition); \
  159. } while (0)
  160. #define __wait_event_timeout(wq, condition, ret) \
  161. do { \
  162. DEFINE_WAIT(__wait); \
  163. \
  164. for (;;) { \
  165. prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
  166. if (condition) \
  167. break; \
  168. ret = schedule_timeout(ret); \
  169. if (!ret) \
  170. break; \
  171. } \
  172. finish_wait(&wq, &__wait); \
  173. } while (0)
  174. /**
  175. * wait_event_timeout - sleep until a condition gets true or a timeout elapses
  176. * @wq: the waitqueue to wait on
  177. * @condition: a C expression for the event to wait for
  178. * @timeout: timeout, in jiffies
  179. *
  180. * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
  181. * @condition evaluates to true. The @condition is checked each time
  182. * the waitqueue @wq is woken up.
  183. *
  184. * wake_up() has to be called after changing any variable that could
  185. * change the result of the wait condition.
  186. *
  187. * The function returns 0 if the @timeout elapsed, and the remaining
  188. * jiffies if the condition evaluated to true before the timeout elapsed.
  189. */
  190. #define wait_event_timeout(wq, condition, timeout) \
  191. ({ \
  192. long __ret = timeout; \
  193. if (!(condition)) \
  194. __wait_event_timeout(wq, condition, __ret); \
  195. __ret; \
  196. })
  197. #define __wait_event_interruptible(wq, condition, ret) \
  198. do { \
  199. DEFINE_WAIT(__wait); \
  200. \
  201. for (;;) { \
  202. prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
  203. if (condition) \
  204. break; \
  205. if (!signal_pending(current)) { \
  206. schedule(); \
  207. continue; \
  208. } \
  209. ret = -ERESTARTSYS; \
  210. break; \
  211. } \
  212. finish_wait(&wq, &__wait); \
  213. } while (0)
  214. /**
  215. * wait_event_interruptible - sleep until a condition gets true
  216. * @wq: the waitqueue to wait on
  217. * @condition: a C expression for the event to wait for
  218. *
  219. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  220. * @condition evaluates to true or a signal is received.
  221. * The @condition is checked each time the waitqueue @wq is woken up.
  222. *
  223. * wake_up() has to be called after changing any variable that could
  224. * change the result of the wait condition.
  225. *
  226. * The function will return -ERESTARTSYS if it was interrupted by a
  227. * signal and 0 if @condition evaluated to true.
  228. */
  229. #define wait_event_interruptible(wq, condition) \
  230. ({ \
  231. int __ret = 0; \
  232. if (!(condition)) \
  233. __wait_event_interruptible(wq, condition, __ret); \
  234. __ret; \
  235. })
  236. #define __wait_event_interruptible_timeout(wq, condition, ret) \
  237. do { \
  238. DEFINE_WAIT(__wait); \
  239. \
  240. for (;;) { \
  241. prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
  242. if (condition) \
  243. break; \
  244. if (!signal_pending(current)) { \
  245. ret = schedule_timeout(ret); \
  246. if (!ret) \
  247. break; \
  248. continue; \
  249. } \
  250. ret = -ERESTARTSYS; \
  251. break; \
  252. } \
  253. finish_wait(&wq, &__wait); \
  254. } while (0)
  255. /**
  256. * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
  257. * @wq: the waitqueue to wait on
  258. * @condition: a C expression for the event to wait for
  259. * @timeout: timeout, in jiffies
  260. *
  261. * The process is put to sleep (TASK_INTERRUPTIBLE) until the
  262. * @condition evaluates to true or a signal is received.
  263. * The @condition is checked each time the waitqueue @wq is woken up.
  264. *
  265. * wake_up() has to be called after changing any variable that could
  266. * change the result of the wait condition.
  267. *
  268. * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
  269. * was interrupted by a signal, and the remaining jiffies otherwise
  270. * if the condition evaluated to true before the timeout elapsed.
  271. */
  272. #define wait_event_interruptible_timeout(wq, condition, timeout) \
  273. ({ \
  274. long __ret = timeout; \
  275. if (!(condition)) \
  276. __wait_event_interruptible_timeout(wq, condition, __ret); \
  277. __ret; \
  278. })
  279. #define __wait_event_interruptible_exclusive(wq, condition, ret) \
  280. do { \
  281. DEFINE_WAIT(__wait); \
  282. \
  283. for (;;) { \
  284. prepare_to_wait_exclusive(&wq, &__wait, \
  285. TASK_INTERRUPTIBLE); \
  286. if (condition) \
  287. break; \
  288. if (!signal_pending(current)) { \
  289. schedule(); \
  290. continue; \
  291. } \
  292. ret = -ERESTARTSYS; \
  293. break; \
  294. } \
  295. finish_wait(&wq, &__wait); \
  296. } while (0)
  297. #define wait_event_interruptible_exclusive(wq, condition) \
  298. ({ \
  299. int __ret = 0; \
  300. if (!(condition)) \
  301. __wait_event_interruptible_exclusive(wq, condition, __ret);\
  302. __ret; \
  303. })
  304. /*
  305. * Must be called with the spinlock in the wait_queue_head_t held.
  306. */
  307. static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
  308. wait_queue_t * wait)
  309. {
  310. wait->flags |= WQ_FLAG_EXCLUSIVE;
  311. __add_wait_queue_tail(q, wait);
  312. }
  313. /*
  314. * Must be called with the spinlock in the wait_queue_head_t held.
  315. */
  316. static inline void remove_wait_queue_locked(wait_queue_head_t *q,
  317. wait_queue_t * wait)
  318. {
  319. __remove_wait_queue(q, wait);
  320. }
  321. /*
  322. * These are the old interfaces to sleep waiting for an event.
  323. * They are racy. DO NOT use them, use the wait_event* interfaces above.
  324. * We plan to remove these interfaces during 2.7.
  325. */
  326. extern void FASTCALL(sleep_on(wait_queue_head_t *q));
  327. extern long FASTCALL(sleep_on_timeout(wait_queue_head_t *q,
  328. signed long timeout));
  329. extern void FASTCALL(interruptible_sleep_on(wait_queue_head_t *q));
  330. extern long FASTCALL(interruptible_sleep_on_timeout(wait_queue_head_t *q,
  331. signed long timeout));
  332. /*
  333. * Waitqueues which are removed from the waitqueue_head at wakeup time
  334. */
  335. void FASTCALL(prepare_to_wait(wait_queue_head_t *q,
  336. wait_queue_t *wait, int state));
  337. void FASTCALL(prepare_to_wait_exclusive(wait_queue_head_t *q,
  338. wait_queue_t *wait, int state));
  339. void FASTCALL(finish_wait(wait_queue_head_t *q, wait_queue_t *wait));
  340. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  341. int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
  342. #define DEFINE_WAIT(name) \
  343. wait_queue_t name = { \
  344. .private = current, \
  345. .func = autoremove_wake_function, \
  346. .task_list = LIST_HEAD_INIT((name).task_list), \
  347. }
  348. #define DEFINE_WAIT_BIT(name, word, bit) \
  349. struct wait_bit_queue name = { \
  350. .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
  351. .wait = { \
  352. .private = current, \
  353. .func = wake_bit_function, \
  354. .task_list = \
  355. LIST_HEAD_INIT((name).wait.task_list), \
  356. }, \
  357. }
  358. #define init_wait(wait) \
  359. do { \
  360. (wait)->private = current; \
  361. (wait)->func = autoremove_wake_function; \
  362. INIT_LIST_HEAD(&(wait)->task_list); \
  363. } while (0)
  364. /**
  365. * wait_on_bit - wait for a bit to be cleared
  366. * @word: the word being waited on, a kernel virtual address
  367. * @bit: the bit of the word being waited on
  368. * @action: the function used to sleep, which may take special actions
  369. * @mode: the task state to sleep in
  370. *
  371. * There is a standard hashed waitqueue table for generic use. This
  372. * is the part of the hashtable's accessor API that waits on a bit.
  373. * For instance, if one were to have waiters on a bitflag, one would
  374. * call wait_on_bit() in threads waiting for the bit to clear.
  375. * One uses wait_on_bit() where one is waiting for the bit to clear,
  376. * but has no intention of setting it.
  377. */
  378. static inline int wait_on_bit(void *word, int bit,
  379. int (*action)(void *), unsigned mode)
  380. {
  381. if (!test_bit(bit, word))
  382. return 0;
  383. return out_of_line_wait_on_bit(word, bit, action, mode);
  384. }
  385. /**
  386. * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
  387. * @word: the word being waited on, a kernel virtual address
  388. * @bit: the bit of the word being waited on
  389. * @action: the function used to sleep, which may take special actions
  390. * @mode: the task state to sleep in
  391. *
  392. * There is a standard hashed waitqueue table for generic use. This
  393. * is the part of the hashtable's accessor API that waits on a bit
  394. * when one intends to set it, for instance, trying to lock bitflags.
  395. * For instance, if one were to have waiters trying to set bitflag
  396. * and waiting for it to clear before setting it, one would call
  397. * wait_on_bit() in threads waiting to be able to set the bit.
  398. * One uses wait_on_bit_lock() where one is waiting for the bit to
  399. * clear with the intention of setting it, and when done, clearing it.
  400. */
  401. static inline int wait_on_bit_lock(void *word, int bit,
  402. int (*action)(void *), unsigned mode)
  403. {
  404. if (!test_and_set_bit(bit, word))
  405. return 0;
  406. return out_of_line_wait_on_bit_lock(word, bit, action, mode);
  407. }
  408. #endif /* __KERNEL__ */
  409. #endif