igb: cleanup function header comments
[linux-2.6/mini2440.git] / kernel / wait.c
blobc275c56cf2d313e509a2aac411d3bcebf4278ca1
1 /*
2 * Generic waiting primitives.
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>
13 void init_waitqueue_head(wait_queue_head_t *q)
15 spin_lock_init(&q->lock);
16 INIT_LIST_HEAD(&q->task_list);
19 EXPORT_SYMBOL(init_waitqueue_head);
21 void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
23 unsigned long flags;
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);
30 EXPORT_SYMBOL(add_wait_queue);
32 void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
34 unsigned long flags;
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);
41 EXPORT_SYMBOL(add_wait_queue_exclusive);
43 void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
45 unsigned long flags;
47 spin_lock_irqsave(&q->lock, flags);
48 __remove_wait_queue(q, wait);
49 spin_unlock_irqrestore(&q->lock, flags);
51 EXPORT_SYMBOL(remove_wait_queue);
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.
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
64 * loads to move into the critical region).
66 void
67 prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
69 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(q, wait);
76 * don't alter the task state if this is just going to
77 * queue an async wait queue callback
79 if (is_sync_wait(wait))
80 set_current_state(state);
81 spin_unlock_irqrestore(&q->lock, flags);
83 EXPORT_SYMBOL(prepare_to_wait);
85 void
86 prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
88 unsigned long flags;
90 wait->flags |= WQ_FLAG_EXCLUSIVE;
91 spin_lock_irqsave(&q->lock, flags);
92 if (list_empty(&wait->task_list))
93 __add_wait_queue_tail(q, wait);
95 * don't alter the task state if this is just going to
96 * queue an async wait queue callback
98 if (is_sync_wait(wait))
99 set_current_state(state);
100 spin_unlock_irqrestore(&q->lock, flags);
102 EXPORT_SYMBOL(prepare_to_wait_exclusive);
104 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
106 unsigned long flags;
108 __set_current_state(TASK_RUNNING);
110 * We can check for list emptiness outside the lock
111 * IFF:
112 * - we use the "careful" check that verifies both
113 * the next and prev pointers, so that there cannot
114 * be any half-pending updates in progress on other
115 * CPU's that we haven't seen yet (and that might
116 * still change the stack area.
117 * and
118 * - all other users take the lock (ie we can only
119 * have _one_ other CPU that looks at or modifies
120 * the list).
122 if (!list_empty_careful(&wait->task_list)) {
123 spin_lock_irqsave(&q->lock, flags);
124 list_del_init(&wait->task_list);
125 spin_unlock_irqrestore(&q->lock, flags);
128 EXPORT_SYMBOL(finish_wait);
130 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
132 int ret = default_wake_function(wait, mode, sync, key);
134 if (ret)
135 list_del_init(&wait->task_list);
136 return ret;
138 EXPORT_SYMBOL(autoremove_wake_function);
140 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
142 struct wait_bit_key *key = arg;
143 struct wait_bit_queue *wait_bit
144 = container_of(wait, struct wait_bit_queue, wait);
146 if (wait_bit->key.flags != key->flags ||
147 wait_bit->key.bit_nr != key->bit_nr ||
148 test_bit(key->bit_nr, key->flags))
149 return 0;
150 else
151 return autoremove_wake_function(wait, mode, sync, key);
153 EXPORT_SYMBOL(wake_bit_function);
156 * To allow interruptible waiting and asynchronous (i.e. nonblocking)
157 * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
158 * permitted return codes. Nonzero return codes halt waiting and return.
160 int __sched
161 __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
162 int (*action)(void *), unsigned mode)
164 int ret = 0;
166 do {
167 prepare_to_wait(wq, &q->wait, mode);
168 if (test_bit(q->key.bit_nr, q->key.flags))
169 ret = (*action)(q->key.flags);
170 } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
171 finish_wait(wq, &q->wait);
172 return ret;
174 EXPORT_SYMBOL(__wait_on_bit);
176 int __sched out_of_line_wait_on_bit(void *word, int bit,
177 int (*action)(void *), unsigned mode)
179 wait_queue_head_t *wq = bit_waitqueue(word, bit);
180 DEFINE_WAIT_BIT(wait, word, bit);
182 return __wait_on_bit(wq, &wait, action, mode);
184 EXPORT_SYMBOL(out_of_line_wait_on_bit);
186 int __sched
187 __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
188 int (*action)(void *), unsigned mode)
190 int ret = 0;
192 do {
193 prepare_to_wait_exclusive(wq, &q->wait, mode);
194 if (test_bit(q->key.bit_nr, q->key.flags)) {
195 if ((ret = (*action)(q->key.flags)))
196 break;
198 } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
199 finish_wait(wq, &q->wait);
200 return ret;
202 EXPORT_SYMBOL(__wait_on_bit_lock);
204 int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
205 int (*action)(void *), unsigned mode)
207 wait_queue_head_t *wq = bit_waitqueue(word, bit);
208 DEFINE_WAIT_BIT(wait, word, bit);
210 return __wait_on_bit_lock(wq, &wait, action, mode);
212 EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
214 void __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
216 struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
217 if (waitqueue_active(wq))
218 __wake_up(wq, TASK_NORMAL, 1, &key);
220 EXPORT_SYMBOL(__wake_up_bit);
223 * wake_up_bit - wake up a waiter on a bit
224 * @word: the word being waited on, a kernel virtual address
225 * @bit: the bit of the word being waited on
227 * There is a standard hashed waitqueue table for generic use. This
228 * is the part of the hashtable's accessor API that wakes up waiters
229 * on a bit. For instance, if one were to have waiters on a bitflag,
230 * one would call wake_up_bit() after clearing the bit.
232 * In order for this to function properly, as it uses waitqueue_active()
233 * internally, some kind of memory barrier must be done prior to calling
234 * this. Typically, this will be smp_mb__after_clear_bit(), but in some
235 * cases where bitflags are manipulated non-atomically under a lock, one
236 * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
237 * because spin_unlock() does not guarantee a memory barrier.
239 void wake_up_bit(void *word, int bit)
241 __wake_up_bit(bit_waitqueue(word, bit), word, bit);
243 EXPORT_SYMBOL(wake_up_bit);
245 wait_queue_head_t *bit_waitqueue(void *word, int bit)
247 const int shift = BITS_PER_LONG == 32 ? 5 : 6;
248 const struct zone *zone = page_zone(virt_to_page(word));
249 unsigned long val = (unsigned long)word << shift | bit;
251 return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
253 EXPORT_SYMBOL(bit_waitqueue);