wait_bit.h 9.1 KB

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