1
2
3
4
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
13void __init_waitqueue_head(wait_queue_head_t *q, struct lock_class_key *key)
14{
15 spin_lock_init(&q->lock);
16 lockdep_set_class(&q->lock, key);
17 INIT_LIST_HEAD(&q->task_list);
18}
19
20EXPORT_SYMBOL(__init_waitqueue_head);
21
22void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
23{
24 unsigned long flags;
25
26 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
27 spin_lock_irqsave(&q->lock, flags);
28 __add_wait_queue(q, wait);
29 spin_unlock_irqrestore(&q->lock, flags);
30}
31EXPORT_SYMBOL(add_wait_queue);
32
33void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
34{
35 unsigned long flags;
36
37 wait->flags |= WQ_FLAG_EXCLUSIVE;
38 spin_lock_irqsave(&q->lock, flags);
39 __add_wait_queue_tail(q, wait);
40 spin_unlock_irqrestore(&q->lock, flags);
41}
42EXPORT_SYMBOL(add_wait_queue_exclusive);
43
44void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
45{
46 unsigned long flags;
47
48 spin_lock_irqsave(&q->lock, flags);
49 __remove_wait_queue(q, wait);
50 spin_unlock_irqrestore(&q->lock, flags);
51}
52EXPORT_SYMBOL(remove_wait_queue);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67void
68prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
69{
70 unsigned long flags;
71
72 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
73 spin_lock_irqsave(&q->lock, flags);
74 if (list_empty(&wait->task_list))
75 __add_wait_queue(q, wait);
76 set_current_state(state);
77 spin_unlock_irqrestore(&q->lock, flags);
78}
79EXPORT_SYMBOL(prepare_to_wait);
80
81void
82prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
83{
84 unsigned long flags;
85
86 wait->flags |= WQ_FLAG_EXCLUSIVE;
87 spin_lock_irqsave(&q->lock, flags);
88 if (list_empty(&wait->task_list))
89 __add_wait_queue_tail(q, wait);
90 set_current_state(state);
91 spin_unlock_irqrestore(&q->lock, flags);
92}
93EXPORT_SYMBOL(prepare_to_wait_exclusive);
94
95
96
97
98
99
100
101
102
103
104void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
105{
106 unsigned long flags;
107
108 __set_current_state(TASK_RUNNING);
109
110
111
112
113
114
115
116
117
118
119
120
121
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);
126 }
127}
128EXPORT_SYMBOL(finish_wait);
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
149 unsigned int mode, void *key)
150{
151 unsigned long flags;
152
153 __set_current_state(TASK_RUNNING);
154 spin_lock_irqsave(&q->lock, flags);
155 if (!list_empty(&wait->task_list))
156 list_del_init(&wait->task_list);
157 else if (waitqueue_active(q))
158 __wake_up_locked_key(q, mode, key);
159 spin_unlock_irqrestore(&q->lock, flags);
160}
161EXPORT_SYMBOL(abort_exclusive_wait);
162
163int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
164{
165 int ret = default_wake_function(wait, mode, sync, key);
166
167 if (ret)
168 list_del_init(&wait->task_list);
169 return ret;
170}
171EXPORT_SYMBOL(autoremove_wake_function);
172
173int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
174{
175 struct wait_bit_key *key = arg;
176 struct wait_bit_queue *wait_bit
177 = container_of(wait, struct wait_bit_queue, wait);
178
179 if (wait_bit->key.flags != key->flags ||
180 wait_bit->key.bit_nr != key->bit_nr ||
181 test_bit(key->bit_nr, key->flags))
182 return 0;
183 else
184 return autoremove_wake_function(wait, mode, sync, key);
185}
186EXPORT_SYMBOL(wake_bit_function);
187
188
189
190
191
192
193int __sched
194__wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
195 int (*action)(void *), unsigned mode)
196{
197 int ret = 0;
198
199 do {
200 prepare_to_wait(wq, &q->wait, mode);
201 if (test_bit(q->key.bit_nr, q->key.flags))
202 ret = (*action)(q->key.flags);
203 } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
204 finish_wait(wq, &q->wait);
205 return ret;
206}
207EXPORT_SYMBOL(__wait_on_bit);
208
209int __sched out_of_line_wait_on_bit(void *word, int bit,
210 int (*action)(void *), unsigned mode)
211{
212 wait_queue_head_t *wq = bit_waitqueue(word, bit);
213 DEFINE_WAIT_BIT(wait, word, bit);
214
215 return __wait_on_bit(wq, &wait, action, mode);
216}
217EXPORT_SYMBOL(out_of_line_wait_on_bit);
218
219int __sched
220__wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
221 int (*action)(void *), unsigned mode)
222{
223 do {
224 int ret;
225
226 prepare_to_wait_exclusive(wq, &q->wait, mode);
227 if (!test_bit(q->key.bit_nr, q->key.flags))
228 continue;
229 ret = action(q->key.flags);
230 if (!ret)
231 continue;
232 abort_exclusive_wait(wq, &q->wait, mode, &q->key);
233 return ret;
234 } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
235 finish_wait(wq, &q->wait);
236 return 0;
237}
238EXPORT_SYMBOL(__wait_on_bit_lock);
239
240int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
241 int (*action)(void *), unsigned mode)
242{
243 wait_queue_head_t *wq = bit_waitqueue(word, bit);
244 DEFINE_WAIT_BIT(wait, word, bit);
245
246 return __wait_on_bit_lock(wq, &wait, action, mode);
247}
248EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
249
250void __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
251{
252 struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
253 if (waitqueue_active(wq))
254 __wake_up(wq, TASK_NORMAL, 1, &key);
255}
256EXPORT_SYMBOL(__wake_up_bit);
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275void wake_up_bit(void *word, int bit)
276{
277 __wake_up_bit(bit_waitqueue(word, bit), word, bit);
278}
279EXPORT_SYMBOL(wake_up_bit);
280
281wait_queue_head_t *bit_waitqueue(void *word, int bit)
282{
283 const int shift = BITS_PER_LONG == 32 ? 5 : 6;
284 const struct zone *zone = page_zone(virt_to_page(word));
285 unsigned long val = (unsigned long)word << shift | bit;
286
287 return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
288}
289EXPORT_SYMBOL(bit_waitqueue);
290