wait.h 14 KB

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