wait.h 14 KB

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