wait.h 16 KB

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