wait.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Generic waiting primitives.
  3. *
  4. * (C) 2004 William Irwin, Oracle
  5. */
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/mm.h>
  10. #include <linux/wait.h>
  11. #include <linux/hash.h>
  12. void init_waitqueue_head(wait_queue_head_t *q)
  13. {
  14. spin_lock_init(&q->lock);
  15. INIT_LIST_HEAD(&q->task_list);
  16. }
  17. EXPORT_SYMBOL(init_waitqueue_head);
  18. void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
  19. {
  20. unsigned long flags;
  21. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  22. spin_lock_irqsave(&q->lock, flags);
  23. __add_wait_queue(q, wait);
  24. spin_unlock_irqrestore(&q->lock, flags);
  25. }
  26. EXPORT_SYMBOL(add_wait_queue);
  27. void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
  28. {
  29. unsigned long flags;
  30. wait->flags |= WQ_FLAG_EXCLUSIVE;
  31. spin_lock_irqsave(&q->lock, flags);
  32. __add_wait_queue_tail(q, wait);
  33. spin_unlock_irqrestore(&q->lock, flags);
  34. }
  35. EXPORT_SYMBOL(add_wait_queue_exclusive);
  36. void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
  37. {
  38. unsigned long flags;
  39. spin_lock_irqsave(&q->lock, flags);
  40. __remove_wait_queue(q, wait);
  41. spin_unlock_irqrestore(&q->lock, flags);
  42. }
  43. EXPORT_SYMBOL(remove_wait_queue);
  44. /*
  45. * Note: we use "set_current_state()" _after_ the wait-queue add,
  46. * because we need a memory barrier there on SMP, so that any
  47. * wake-function that tests for the wait-queue being active
  48. * will be guaranteed to see waitqueue addition _or_ subsequent
  49. * tests in this thread will see the wakeup having taken place.
  50. *
  51. * The spin_unlock() itself is semi-permeable and only protects
  52. * one way (it only protects stuff inside the critical region and
  53. * stops them from bleeding out - it would still allow subsequent
  54. * loads to move into the critical region).
  55. */
  56. void
  57. prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
  58. {
  59. unsigned long flags;
  60. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  61. spin_lock_irqsave(&q->lock, flags);
  62. if (list_empty(&wait->task_list))
  63. __add_wait_queue(q, wait);
  64. set_current_state(state);
  65. spin_unlock_irqrestore(&q->lock, flags);
  66. }
  67. EXPORT_SYMBOL(prepare_to_wait);
  68. void
  69. prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
  70. {
  71. unsigned long flags;
  72. wait->flags |= WQ_FLAG_EXCLUSIVE;
  73. spin_lock_irqsave(&q->lock, flags);
  74. if (list_empty(&wait->task_list))
  75. __add_wait_queue_tail(q, wait);
  76. set_current_state(state);
  77. spin_unlock_irqrestore(&q->lock, flags);
  78. }
  79. EXPORT_SYMBOL(prepare_to_wait_exclusive);
  80. void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
  81. {
  82. unsigned long flags;
  83. __set_current_state(TASK_RUNNING);
  84. /*
  85. * We can check for list emptiness outside the lock
  86. * IFF:
  87. * - we use the "careful" check that verifies both
  88. * the next and prev pointers, so that there cannot
  89. * be any half-pending updates in progress on other
  90. * CPU's that we haven't seen yet (and that might
  91. * still change the stack area.
  92. * and
  93. * - all other users take the lock (ie we can only
  94. * have _one_ other CPU that looks at or modifies
  95. * the list).
  96. */
  97. if (!list_empty_careful(&wait->task_list)) {
  98. spin_lock_irqsave(&q->lock, flags);
  99. list_del_init(&wait->task_list);
  100. spin_unlock_irqrestore(&q->lock, flags);
  101. }
  102. }
  103. EXPORT_SYMBOL(finish_wait);
  104. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  105. {
  106. int ret = default_wake_function(wait, mode, sync, key);
  107. if (ret)
  108. list_del_init(&wait->task_list);
  109. return ret;
  110. }
  111. EXPORT_SYMBOL(autoremove_wake_function);
  112. int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
  113. {
  114. struct wait_bit_key *key = arg;
  115. struct wait_bit_queue *wait_bit
  116. = container_of(wait, struct wait_bit_queue, wait);
  117. if (wait_bit->key.flags != key->flags ||
  118. wait_bit->key.bit_nr != key->bit_nr ||
  119. test_bit(key->bit_nr, key->flags))
  120. return 0;
  121. else
  122. return autoremove_wake_function(wait, mode, sync, key);
  123. }
  124. EXPORT_SYMBOL(wake_bit_function);
  125. /*
  126. * To allow interruptible waiting and asynchronous (i.e. nonblocking)
  127. * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
  128. * permitted return codes. Nonzero return codes halt waiting and return.
  129. */
  130. int __sched
  131. __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
  132. int (*action)(void *), unsigned mode)
  133. {
  134. int ret = 0;
  135. do {
  136. prepare_to_wait(wq, &q->wait, mode);
  137. if (test_bit(q->key.bit_nr, q->key.flags))
  138. ret = (*action)(q->key.flags);
  139. } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
  140. finish_wait(wq, &q->wait);
  141. return ret;
  142. }
  143. EXPORT_SYMBOL(__wait_on_bit);
  144. int __sched out_of_line_wait_on_bit(void *word, int bit,
  145. int (*action)(void *), unsigned mode)
  146. {
  147. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  148. DEFINE_WAIT_BIT(wait, word, bit);
  149. return __wait_on_bit(wq, &wait, action, mode);
  150. }
  151. EXPORT_SYMBOL(out_of_line_wait_on_bit);
  152. int __sched
  153. __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
  154. int (*action)(void *), unsigned mode)
  155. {
  156. int ret = 0;
  157. do {
  158. prepare_to_wait_exclusive(wq, &q->wait, mode);
  159. if (test_bit(q->key.bit_nr, q->key.flags)) {
  160. if ((ret = (*action)(q->key.flags)))
  161. break;
  162. }
  163. } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
  164. finish_wait(wq, &q->wait);
  165. return ret;
  166. }
  167. EXPORT_SYMBOL(__wait_on_bit_lock);
  168. int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
  169. int (*action)(void *), unsigned mode)
  170. {
  171. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  172. DEFINE_WAIT_BIT(wait, word, bit);
  173. return __wait_on_bit_lock(wq, &wait, action, mode);
  174. }
  175. EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
  176. void __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
  177. {
  178. struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
  179. if (waitqueue_active(wq))
  180. __wake_up(wq, TASK_NORMAL, 1, &key);
  181. }
  182. EXPORT_SYMBOL(__wake_up_bit);
  183. /**
  184. * wake_up_bit - wake up a waiter on a bit
  185. * @word: the word being waited on, a kernel virtual address
  186. * @bit: the bit of the word being waited on
  187. *
  188. * There is a standard hashed waitqueue table for generic use. This
  189. * is the part of the hashtable's accessor API that wakes up waiters
  190. * on a bit. For instance, if one were to have waiters on a bitflag,
  191. * one would call wake_up_bit() after clearing the bit.
  192. *
  193. * In order for this to function properly, as it uses waitqueue_active()
  194. * internally, some kind of memory barrier must be done prior to calling
  195. * this. Typically, this will be smp_mb__after_clear_bit(), but in some
  196. * cases where bitflags are manipulated non-atomically under a lock, one
  197. * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
  198. * because spin_unlock() does not guarantee a memory barrier.
  199. */
  200. void wake_up_bit(void *word, int bit)
  201. {
  202. __wake_up_bit(bit_waitqueue(word, bit), word, bit);
  203. }
  204. EXPORT_SYMBOL(wake_up_bit);
  205. wait_queue_head_t *bit_waitqueue(void *word, int bit)
  206. {
  207. const int shift = BITS_PER_LONG == 32 ? 5 : 6;
  208. const struct zone *zone = page_zone(virt_to_page(word));
  209. unsigned long val = (unsigned long)word << shift | bit;
  210. return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
  211. }
  212. EXPORT_SYMBOL(bit_waitqueue);