wait.c 7.1 KB

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