wait_bit.h 9.3 KB

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