2 * Generic waiting primitives.
4 * (C) 2004 William Irwin, Oracle
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/sched.h>
10 #include <linux/wait.h>
11 #include <linux/hash.h>
13 void fastcall
add_wait_queue(wait_queue_head_t
*q
, wait_queue_t
*wait
)
17 wait
->flags
&= ~WQ_FLAG_EXCLUSIVE
;
18 spin_lock_irqsave(&q
->lock
, flags
);
19 __add_wait_queue(q
, wait
);
20 spin_unlock_irqrestore(&q
->lock
, flags
);
22 EXPORT_SYMBOL(add_wait_queue
);
24 void fastcall
add_wait_queue_exclusive(wait_queue_head_t
*q
, wait_queue_t
*wait
)
28 wait
->flags
|= WQ_FLAG_EXCLUSIVE
;
29 spin_lock_irqsave(&q
->lock
, flags
);
30 __add_wait_queue_tail(q
, wait
);
31 spin_unlock_irqrestore(&q
->lock
, flags
);
33 EXPORT_SYMBOL(add_wait_queue_exclusive
);
35 void fastcall
remove_wait_queue(wait_queue_head_t
*q
, wait_queue_t
*wait
)
39 spin_lock_irqsave(&q
->lock
, flags
);
40 __remove_wait_queue(q
, wait
);
41 spin_unlock_irqrestore(&q
->lock
, flags
);
43 EXPORT_SYMBOL(remove_wait_queue
);
47 * Note: we use "set_current_state()" _after_ the wait-queue add,
48 * because we need a memory barrier there on SMP, so that any
49 * wake-function that tests for the wait-queue being active
50 * will be guaranteed to see waitqueue addition _or_ subsequent
51 * tests in this thread will see the wakeup having taken place.
53 * The spin_unlock() itself is semi-permeable and only protects
54 * one way (it only protects stuff inside the critical region and
55 * stops them from bleeding out - it would still allow subsequent
56 * loads to move into the the critical region).
59 prepare_to_wait(wait_queue_head_t
*q
, wait_queue_t
*wait
, int state
)
63 wait
->flags
&= ~WQ_FLAG_EXCLUSIVE
;
64 spin_lock_irqsave(&q
->lock
, flags
);
65 if (list_empty(&wait
->task_list
))
66 __add_wait_queue(q
, wait
);
68 * don't alter the task state if this is just going to
69 * queue an async wait queue callback
71 if (is_sync_wait(wait
))
72 set_current_state(state
);
73 spin_unlock_irqrestore(&q
->lock
, flags
);
75 EXPORT_SYMBOL(prepare_to_wait
);
78 prepare_to_wait_exclusive(wait_queue_head_t
*q
, wait_queue_t
*wait
, int state
)
82 wait
->flags
|= WQ_FLAG_EXCLUSIVE
;
83 spin_lock_irqsave(&q
->lock
, flags
);
84 if (list_empty(&wait
->task_list
))
85 __add_wait_queue_tail(q
, wait
);
87 * don't alter the task state if this is just going to
88 * queue an async wait queue callback
90 if (is_sync_wait(wait
))
91 set_current_state(state
);
92 spin_unlock_irqrestore(&q
->lock
, flags
);
94 EXPORT_SYMBOL(prepare_to_wait_exclusive
);
96 void fastcall
finish_wait(wait_queue_head_t
*q
, wait_queue_t
*wait
)
100 __set_current_state(TASK_RUNNING
);
102 * We can check for list emptiness outside the lock
104 * - we use the "careful" check that verifies both
105 * the next and prev pointers, so that there cannot
106 * be any half-pending updates in progress on other
107 * CPU's that we haven't seen yet (and that might
108 * still change the stack area.
110 * - all other users take the lock (ie we can only
111 * have _one_ other CPU that looks at or modifies
114 if (!list_empty_careful(&wait
->task_list
)) {
115 spin_lock_irqsave(&q
->lock
, flags
);
116 list_del_init(&wait
->task_list
);
117 spin_unlock_irqrestore(&q
->lock
, flags
);
120 EXPORT_SYMBOL(finish_wait
);
122 int autoremove_wake_function(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
)
124 int ret
= default_wake_function(wait
, mode
, sync
, key
);
127 list_del_init(&wait
->task_list
);
130 EXPORT_SYMBOL(autoremove_wake_function
);
132 int wake_bit_function(wait_queue_t
*wait
, unsigned mode
, int sync
, void *arg
)
134 struct wait_bit_key
*key
= arg
;
135 struct wait_bit_queue
*wait_bit
136 = container_of(wait
, struct wait_bit_queue
, wait
);
138 if (wait_bit
->key
.flags
!= key
->flags
||
139 wait_bit
->key
.bit_nr
!= key
->bit_nr
||
140 test_bit(key
->bit_nr
, key
->flags
))
143 return autoremove_wake_function(wait
, mode
, sync
, key
);
145 EXPORT_SYMBOL(wake_bit_function
);
148 * To allow interruptible waiting and asynchronous (i.e. nonblocking)
149 * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
150 * permitted return codes. Nonzero return codes halt waiting and return.
153 __wait_on_bit(wait_queue_head_t
*wq
, struct wait_bit_queue
*q
,
154 int (*action
)(void *), unsigned mode
)
159 prepare_to_wait(wq
, &q
->wait
, mode
);
160 if (test_bit(q
->key
.bit_nr
, q
->key
.flags
))
161 ret
= (*action
)(q
->key
.flags
);
162 } while (test_bit(q
->key
.bit_nr
, q
->key
.flags
) && !ret
);
163 finish_wait(wq
, &q
->wait
);
166 EXPORT_SYMBOL(__wait_on_bit
);
168 int __sched fastcall
out_of_line_wait_on_bit(void *word
, int bit
,
169 int (*action
)(void *), unsigned mode
)
171 wait_queue_head_t
*wq
= bit_waitqueue(word
, bit
);
172 DEFINE_WAIT_BIT(wait
, word
, bit
);
174 return __wait_on_bit(wq
, &wait
, action
, mode
);
176 EXPORT_SYMBOL(out_of_line_wait_on_bit
);
179 __wait_on_bit_lock(wait_queue_head_t
*wq
, struct wait_bit_queue
*q
,
180 int (*action
)(void *), unsigned mode
)
185 prepare_to_wait_exclusive(wq
, &q
->wait
, mode
);
186 if (test_bit(q
->key
.bit_nr
, q
->key
.flags
)) {
187 if ((ret
= (*action
)(q
->key
.flags
)))
190 } while (test_and_set_bit(q
->key
.bit_nr
, q
->key
.flags
));
191 finish_wait(wq
, &q
->wait
);
194 EXPORT_SYMBOL(__wait_on_bit_lock
);
196 int __sched fastcall
out_of_line_wait_on_bit_lock(void *word
, int bit
,
197 int (*action
)(void *), unsigned mode
)
199 wait_queue_head_t
*wq
= bit_waitqueue(word
, bit
);
200 DEFINE_WAIT_BIT(wait
, word
, bit
);
202 return __wait_on_bit_lock(wq
, &wait
, action
, mode
);
204 EXPORT_SYMBOL(out_of_line_wait_on_bit_lock
);
206 void fastcall
__wake_up_bit(wait_queue_head_t
*wq
, void *word
, int bit
)
208 struct wait_bit_key key
= __WAIT_BIT_KEY_INITIALIZER(word
, bit
);
209 if (waitqueue_active(wq
))
210 __wake_up(wq
, TASK_INTERRUPTIBLE
|TASK_UNINTERRUPTIBLE
, 1, &key
);
212 EXPORT_SYMBOL(__wake_up_bit
);
215 * wake_up_bit - wake up a waiter on a bit
216 * @word: the word being waited on, a kernel virtual address
217 * @bit: the bit of the word being waited on
219 * There is a standard hashed waitqueue table for generic use. This
220 * is the part of the hashtable's accessor API that wakes up waiters
221 * on a bit. For instance, if one were to have waiters on a bitflag,
222 * one would call wake_up_bit() after clearing the bit.
224 * In order for this to function properly, as it uses waitqueue_active()
225 * internally, some kind of memory barrier must be done prior to calling
226 * this. Typically, this will be smp_mb__after_clear_bit(), but in some
227 * cases where bitflags are manipulated non-atomically under a lock, one
228 * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
229 * because spin_unlock() does not guarantee a memory barrier.
231 void fastcall
wake_up_bit(void *word
, int bit
)
233 __wake_up_bit(bit_waitqueue(word
, bit
), word
, bit
);
235 EXPORT_SYMBOL(wake_up_bit
);
237 fastcall wait_queue_head_t
*bit_waitqueue(void *word
, int bit
)
239 const int shift
= BITS_PER_LONG
== 32 ? 5 : 6;
240 const struct zone
*zone
= page_zone(virt_to_page(word
));
241 unsigned long val
= (unsigned long)word
<< shift
| bit
;
243 return &zone
->wait_table
[hash_long(val
, zone
->wait_table_bits
)];
245 EXPORT_SYMBOL(bit_waitqueue
);