wait.c 7.1 KB

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