wait_bit.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #ifndef _LINUX_WAIT_BIT_H
  2. #define _LINUX_WAIT_BIT_H
  3. /*
  4. * Linux wait-bit related types and methods:
  5. */
  6. #include <linux/wait.h>
  7. struct wait_bit_key {
  8. void *flags;
  9. int bit_nr;
  10. #define WAIT_ATOMIC_T_BIT_NR -1
  11. unsigned long timeout;
  12. };
  13. struct wait_bit_queue_entry {
  14. struct wait_bit_key key;
  15. struct wait_queue_entry wq_entry;
  16. };
  17. #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
  18. { .flags = word, .bit_nr = bit, }
  19. #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
  20. { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
  21. typedef int wait_bit_action_f(struct wait_bit_key *key, int mode);
  22. void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit);
  23. int __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode);
  24. int __wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode);
  25. void wake_up_bit(void *word, int bit);
  26. void wake_up_atomic_t(atomic_t *p);
  27. int out_of_line_wait_on_bit(void *word, int, wait_bit_action_f *action, unsigned int mode);
  28. int out_of_line_wait_on_bit_timeout(void *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout);
  29. int out_of_line_wait_on_bit_lock(void *word, int, wait_bit_action_f *action, unsigned int mode);
  30. int out_of_line_wait_on_atomic_t(atomic_t *p, int (*)(atomic_t *), unsigned int mode);
  31. struct wait_queue_head *bit_waitqueue(void *word, int bit);
  32. extern void __init wait_bit_init(void);
  33. int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
  34. #define DEFINE_WAIT_BIT(name, word, bit) \
  35. struct wait_bit_queue_entry name = { \
  36. .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
  37. .wq_entry = { \
  38. .private = current, \
  39. .func = wake_bit_function, \
  40. .entry = \
  41. LIST_HEAD_INIT((name).wq_entry.entry), \
  42. }, \
  43. }
  44. extern int bit_wait(struct wait_bit_key *key, int bit);
  45. extern int bit_wait_io(struct wait_bit_key *key, int bit);
  46. extern int bit_wait_timeout(struct wait_bit_key *key, int bit);
  47. extern int bit_wait_io_timeout(struct wait_bit_key *key, int bit);
  48. /**
  49. * wait_on_bit - wait for a bit to be cleared
  50. * @word: the word being waited on, a kernel virtual address
  51. * @bit: the bit of the word being waited on
  52. * @mode: the task state to sleep in
  53. *
  54. * There is a standard hashed waitqueue table for generic use. This
  55. * is the part of the hashtable's accessor API that waits on a bit.
  56. * For instance, if one were to have waiters on a bitflag, one would
  57. * call wait_on_bit() in threads waiting for the bit to clear.
  58. * One uses wait_on_bit() where one is waiting for the bit to clear,
  59. * but has no intention of setting it.
  60. * Returned value will be zero if the bit was cleared, or non-zero
  61. * if the process received a signal and the mode permitted wakeup
  62. * on that signal.
  63. */
  64. static inline int
  65. wait_on_bit(unsigned long *word, int bit, unsigned mode)
  66. {
  67. might_sleep();
  68. if (!test_bit(bit, word))
  69. return 0;
  70. return out_of_line_wait_on_bit(word, bit,
  71. bit_wait,
  72. mode);
  73. }
  74. /**
  75. * wait_on_bit_io - wait for a bit to be cleared
  76. * @word: the word being waited on, a kernel virtual address
  77. * @bit: the bit of the word being waited on
  78. * @mode: the task state to sleep in
  79. *
  80. * Use the standard hashed waitqueue table to wait for a bit
  81. * to be cleared. This is similar to wait_on_bit(), but calls
  82. * io_schedule() instead of schedule() for the actual waiting.
  83. *
  84. * Returned value will be zero if the bit was cleared, or non-zero
  85. * if the process received a signal and the mode permitted wakeup
  86. * on that signal.
  87. */
  88. static inline int
  89. wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
  90. {
  91. might_sleep();
  92. if (!test_bit(bit, word))
  93. return 0;
  94. return out_of_line_wait_on_bit(word, bit,
  95. bit_wait_io,
  96. mode);
  97. }
  98. /**
  99. * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
  100. * @word: the word being waited on, a kernel virtual address
  101. * @bit: the bit of the word being waited on
  102. * @mode: the task state to sleep in
  103. * @timeout: timeout, in jiffies
  104. *
  105. * Use the standard hashed waitqueue table to wait for a bit
  106. * to be cleared. This is similar to wait_on_bit(), except also takes a
  107. * timeout parameter.
  108. *
  109. * Returned value will be zero if the bit was cleared before the
  110. * @timeout elapsed, or non-zero if the @timeout elapsed or process
  111. * received a signal and the mode permitted wakeup on that signal.
  112. */
  113. static inline int
  114. wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
  115. unsigned long timeout)
  116. {
  117. might_sleep();
  118. if (!test_bit(bit, word))
  119. return 0;
  120. return out_of_line_wait_on_bit_timeout(word, bit,
  121. bit_wait_timeout,
  122. mode, timeout);
  123. }
  124. /**
  125. * wait_on_bit_action - wait for a bit to be cleared
  126. * @word: the word being waited on, a kernel virtual address
  127. * @bit: the bit of the word being waited on
  128. * @action: the function used to sleep, which may take special actions
  129. * @mode: the task state to sleep in
  130. *
  131. * Use the standard hashed waitqueue table to wait for a bit
  132. * to be cleared, and allow the waiting action to be specified.
  133. * This is like wait_on_bit() but allows fine control of how the waiting
  134. * is done.
  135. *
  136. * Returned value will be zero if the bit was cleared, or non-zero
  137. * if the process received a signal and the mode permitted wakeup
  138. * on that signal.
  139. */
  140. static inline int
  141. wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
  142. unsigned mode)
  143. {
  144. might_sleep();
  145. if (!test_bit(bit, word))
  146. return 0;
  147. return out_of_line_wait_on_bit(word, bit, action, mode);
  148. }
  149. /**
  150. * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
  151. * @word: the word being waited on, a kernel virtual address
  152. * @bit: the bit of the word being waited on
  153. * @mode: the task state to sleep in
  154. *
  155. * There is a standard hashed waitqueue table for generic use. This
  156. * is the part of the hashtable's accessor API that waits on a bit
  157. * when one intends to set it, for instance, trying to lock bitflags.
  158. * For instance, if one were to have waiters trying to set bitflag
  159. * and waiting for it to clear before setting it, one would call
  160. * wait_on_bit() in threads waiting to be able to set the bit.
  161. * One uses wait_on_bit_lock() where one is waiting for the bit to
  162. * clear with the intention of setting it, and when done, clearing it.
  163. *
  164. * Returns zero if the bit was (eventually) found to be clear and was
  165. * set. Returns non-zero if a signal was delivered to the process and
  166. * the @mode allows that signal to wake the process.
  167. */
  168. static inline int
  169. wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
  170. {
  171. might_sleep();
  172. if (!test_and_set_bit(bit, word))
  173. return 0;
  174. return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
  175. }
  176. /**
  177. * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
  178. * @word: the word being waited on, a kernel virtual address
  179. * @bit: the bit of the word being waited on
  180. * @mode: the task state to sleep in
  181. *
  182. * Use the standard hashed waitqueue table to wait for a bit
  183. * to be cleared and then to atomically set it. This is similar
  184. * to wait_on_bit(), but calls io_schedule() instead of schedule()
  185. * for the actual waiting.
  186. *
  187. * Returns zero if the bit was (eventually) found to be clear and was
  188. * set. Returns non-zero if a signal was delivered to the process and
  189. * the @mode allows that signal to wake the process.
  190. */
  191. static inline int
  192. wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
  193. {
  194. might_sleep();
  195. if (!test_and_set_bit(bit, word))
  196. return 0;
  197. return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
  198. }
  199. /**
  200. * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
  201. * @word: the word being waited on, a kernel virtual address
  202. * @bit: the bit of the word being waited on
  203. * @action: the function used to sleep, which may take special actions
  204. * @mode: the task state to sleep in
  205. *
  206. * Use the standard hashed waitqueue table to wait for a bit
  207. * to be cleared and then to set it, and allow the waiting action
  208. * to be specified.
  209. * This is like wait_on_bit() but allows fine control of how the waiting
  210. * is done.
  211. *
  212. * Returns zero if the bit was (eventually) found to be clear and was
  213. * set. Returns non-zero if a signal was delivered to the process and
  214. * the @mode allows that signal to wake the process.
  215. */
  216. static inline int
  217. wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
  218. unsigned mode)
  219. {
  220. might_sleep();
  221. if (!test_and_set_bit(bit, word))
  222. return 0;
  223. return out_of_line_wait_on_bit_lock(word, bit, action, mode);
  224. }
  225. /**
  226. * wait_on_atomic_t - Wait for an atomic_t to become 0
  227. * @val: The atomic value being waited on, a kernel virtual address
  228. * @action: the function used to sleep, which may take special actions
  229. * @mode: the task state to sleep in
  230. *
  231. * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
  232. * the purpose of getting a waitqueue, but we set the key to a bit number
  233. * outside of the target 'word'.
  234. */
  235. static inline
  236. int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
  237. {
  238. might_sleep();
  239. if (atomic_read(val) == 0)
  240. return 0;
  241. return out_of_line_wait_on_atomic_t(val, action, mode);
  242. }
  243. #endif /* _LINUX_WAIT_BIT_H */