wait.c 7.3 KB

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