blob: cd87131f2fc2aa138aa1bc818f5efab612c3085c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic waiting primitives.
3 *
4 * (C) 2004 William Irwin, Oracle
5 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#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
Ingo Molnar21d71f52006-07-10 04:45:32 -070013void init_waitqueue_head(wait_queue_head_t *q)
14{
15 spin_lock_init(&q->lock);
16 INIT_LIST_HEAD(&q->task_list);
17}
Ingo Molnareb4542b2006-07-03 00:25:07 -070018
Ingo Molnar21d71f52006-07-10 04:45:32 -070019EXPORT_SYMBOL(init_waitqueue_head);
Ingo Molnareb4542b2006-07-03 00:25:07 -070020
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -080021void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 unsigned long flags;
24
25 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
26 spin_lock_irqsave(&q->lock, flags);
27 __add_wait_queue(q, wait);
28 spin_unlock_irqrestore(&q->lock, flags);
29}
30EXPORT_SYMBOL(add_wait_queue);
31
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -080032void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 unsigned long flags;
35
36 wait->flags |= WQ_FLAG_EXCLUSIVE;
37 spin_lock_irqsave(&q->lock, flags);
38 __add_wait_queue_tail(q, wait);
39 spin_unlock_irqrestore(&q->lock, flags);
40}
41EXPORT_SYMBOL(add_wait_queue_exclusive);
42
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -080043void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 unsigned long flags;
46
47 spin_lock_irqsave(&q->lock, flags);
48 __remove_wait_queue(q, wait);
49 spin_unlock_irqrestore(&q->lock, flags);
50}
51EXPORT_SYMBOL(remove_wait_queue);
52
53
54/*
55 * Note: we use "set_current_state()" _after_ the wait-queue add,
56 * because we need a memory barrier there on SMP, so that any
57 * wake-function that tests for the wait-queue being active
58 * will be guaranteed to see waitqueue addition _or_ subsequent
59 * tests in this thread will see the wakeup having taken place.
60 *
61 * The spin_unlock() itself is semi-permeable and only protects
62 * one way (it only protects stuff inside the critical region and
63 * stops them from bleeding out - it would still allow subsequent
Michael Opdenacker59c51592007-05-09 08:57:56 +020064 * loads to move into the critical region).
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -080066void
Linus Torvalds1da177e2005-04-16 15:20:36 -070067prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
68{
69 unsigned long flags;
70
71 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
72 spin_lock_irqsave(&q->lock, flags);
73 if (list_empty(&wait->task_list))
74 __add_wait_queue(q, wait);
Tejun Heoa25d6442008-10-15 22:01:38 -070075 set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 spin_unlock_irqrestore(&q->lock, flags);
77}
78EXPORT_SYMBOL(prepare_to_wait);
79
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -080080void
Linus Torvalds1da177e2005-04-16 15:20:36 -070081prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
82{
83 unsigned long flags;
84
85 wait->flags |= WQ_FLAG_EXCLUSIVE;
86 spin_lock_irqsave(&q->lock, flags);
87 if (list_empty(&wait->task_list))
88 __add_wait_queue_tail(q, wait);
Tejun Heoa25d6442008-10-15 22:01:38 -070089 set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 spin_unlock_irqrestore(&q->lock, flags);
91}
92EXPORT_SYMBOL(prepare_to_wait_exclusive);
93
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -080094void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 unsigned long flags;
97
98 __set_current_state(TASK_RUNNING);
99 /*
100 * We can check for list emptiness outside the lock
101 * IFF:
102 * - we use the "careful" check that verifies both
103 * the next and prev pointers, so that there cannot
104 * be any half-pending updates in progress on other
105 * CPU's that we haven't seen yet (and that might
106 * still change the stack area.
107 * and
108 * - all other users take the lock (ie we can only
109 * have _one_ other CPU that looks at or modifies
110 * the list).
111 */
112 if (!list_empty_careful(&wait->task_list)) {
113 spin_lock_irqsave(&q->lock, flags);
114 list_del_init(&wait->task_list);
115 spin_unlock_irqrestore(&q->lock, flags);
116 }
117}
118EXPORT_SYMBOL(finish_wait);
119
120int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
121{
122 int ret = default_wake_function(wait, mode, sync, key);
123
124 if (ret)
125 list_del_init(&wait->task_list);
126 return ret;
127}
128EXPORT_SYMBOL(autoremove_wake_function);
129
130int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
131{
132 struct wait_bit_key *key = arg;
133 struct wait_bit_queue *wait_bit
134 = container_of(wait, struct wait_bit_queue, wait);
135
136 if (wait_bit->key.flags != key->flags ||
137 wait_bit->key.bit_nr != key->bit_nr ||
138 test_bit(key->bit_nr, key->flags))
139 return 0;
140 else
141 return autoremove_wake_function(wait, mode, sync, key);
142}
143EXPORT_SYMBOL(wake_bit_function);
144
145/*
146 * To allow interruptible waiting and asynchronous (i.e. nonblocking)
147 * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
148 * permitted return codes. Nonzero return codes halt waiting and return.
149 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800150int __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151__wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
152 int (*action)(void *), unsigned mode)
153{
154 int ret = 0;
155
156 do {
157 prepare_to_wait(wq, &q->wait, mode);
158 if (test_bit(q->key.bit_nr, q->key.flags))
159 ret = (*action)(q->key.flags);
160 } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
161 finish_wait(wq, &q->wait);
162 return ret;
163}
164EXPORT_SYMBOL(__wait_on_bit);
165
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800166int __sched out_of_line_wait_on_bit(void *word, int bit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int (*action)(void *), unsigned mode)
168{
169 wait_queue_head_t *wq = bit_waitqueue(word, bit);
170 DEFINE_WAIT_BIT(wait, word, bit);
171
172 return __wait_on_bit(wq, &wait, action, mode);
173}
174EXPORT_SYMBOL(out_of_line_wait_on_bit);
175
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800176int __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177__wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
178 int (*action)(void *), unsigned mode)
179{
180 int ret = 0;
181
182 do {
183 prepare_to_wait_exclusive(wq, &q->wait, mode);
184 if (test_bit(q->key.bit_nr, q->key.flags)) {
185 if ((ret = (*action)(q->key.flags)))
186 break;
187 }
188 } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
189 finish_wait(wq, &q->wait);
190 return ret;
191}
192EXPORT_SYMBOL(__wait_on_bit_lock);
193
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800194int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int (*action)(void *), unsigned mode)
196{
197 wait_queue_head_t *wq = bit_waitqueue(word, bit);
198 DEFINE_WAIT_BIT(wait, word, bit);
199
200 return __wait_on_bit_lock(wq, &wait, action, mode);
201}
202EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
203
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800204void __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
207 if (waitqueue_active(wq))
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500208 __wake_up(wq, TASK_NORMAL, 1, &key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210EXPORT_SYMBOL(__wake_up_bit);
211
212/**
213 * wake_up_bit - wake up a waiter on a bit
214 * @word: the word being waited on, a kernel virtual address
215 * @bit: the bit of the word being waited on
216 *
217 * There is a standard hashed waitqueue table for generic use. This
218 * is the part of the hashtable's accessor API that wakes up waiters
219 * on a bit. For instance, if one were to have waiters on a bitflag,
220 * one would call wake_up_bit() after clearing the bit.
221 *
222 * In order for this to function properly, as it uses waitqueue_active()
223 * internally, some kind of memory barrier must be done prior to calling
224 * this. Typically, this will be smp_mb__after_clear_bit(), but in some
225 * cases where bitflags are manipulated non-atomically under a lock, one
226 * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
227 * because spin_unlock() does not guarantee a memory barrier.
228 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800229void wake_up_bit(void *word, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 __wake_up_bit(bit_waitqueue(word, bit), word, bit);
232}
233EXPORT_SYMBOL(wake_up_bit);
234
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800235wait_queue_head_t *bit_waitqueue(void *word, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 const int shift = BITS_PER_LONG == 32 ? 5 : 6;
238 const struct zone *zone = page_zone(virt_to_page(word));
239 unsigned long val = (unsigned long)word << shift | bit;
240
241 return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
242}
243EXPORT_SYMBOL(bit_waitqueue);