This source file includes following definitions.
- __init_waitqueue_head
- add_wait_queue
- add_wait_queue_exclusive
- remove_wait_queue
- __wake_up_common
- __wake_up_common_lock
- __wake_up
- __wake_up_locked
- __wake_up_locked_key
- __wake_up_locked_key_bookmark
- __wake_up_sync_key
- __wake_up_sync
- prepare_to_wait
- prepare_to_wait_exclusive
- init_wait_entry
- prepare_to_wait_event
- do_wait_intr
- do_wait_intr_irq
- finish_wait
- autoremove_wake_function
- is_kthread_should_stop
- wait_woken
- woken_wake_function
1
2
3
4
5
6
7 #include "sched.h"
8
9 void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *key)
10 {
11 spin_lock_init(&wq_head->lock);
12 lockdep_set_class_and_name(&wq_head->lock, key, name);
13 INIT_LIST_HEAD(&wq_head->head);
14 }
15
16 EXPORT_SYMBOL(__init_waitqueue_head);
17
18 void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
19 {
20 unsigned long flags;
21
22 wq_entry->flags &= ~WQ_FLAG_EXCLUSIVE;
23 spin_lock_irqsave(&wq_head->lock, flags);
24 __add_wait_queue(wq_head, wq_entry);
25 spin_unlock_irqrestore(&wq_head->lock, flags);
26 }
27 EXPORT_SYMBOL(add_wait_queue);
28
29 void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
30 {
31 unsigned long flags;
32
33 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
34 spin_lock_irqsave(&wq_head->lock, flags);
35 __add_wait_queue_entry_tail(wq_head, wq_entry);
36 spin_unlock_irqrestore(&wq_head->lock, flags);
37 }
38 EXPORT_SYMBOL(add_wait_queue_exclusive);
39
40 void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
41 {
42 unsigned long flags;
43
44 spin_lock_irqsave(&wq_head->lock, flags);
45 __remove_wait_queue(wq_head, wq_entry);
46 spin_unlock_irqrestore(&wq_head->lock, flags);
47 }
48 EXPORT_SYMBOL(remove_wait_queue);
49
50
51
52
53
54
55 #define WAITQUEUE_WALK_BREAK_CNT 64
56
57
58
59
60
61
62
63
64
65
66 static int __wake_up_common(struct wait_queue_head *wq_head, unsigned int mode,
67 int nr_exclusive, int wake_flags, void *key,
68 wait_queue_entry_t *bookmark)
69 {
70 wait_queue_entry_t *curr, *next;
71 int cnt = 0;
72
73 lockdep_assert_held(&wq_head->lock);
74
75 if (bookmark && (bookmark->flags & WQ_FLAG_BOOKMARK)) {
76 curr = list_next_entry(bookmark, entry);
77
78 list_del(&bookmark->entry);
79 bookmark->flags = 0;
80 } else
81 curr = list_first_entry(&wq_head->head, wait_queue_entry_t, entry);
82
83 if (&curr->entry == &wq_head->head)
84 return nr_exclusive;
85
86 list_for_each_entry_safe_from(curr, next, &wq_head->head, entry) {
87 unsigned flags = curr->flags;
88 int ret;
89
90 if (flags & WQ_FLAG_BOOKMARK)
91 continue;
92
93 ret = curr->func(curr, mode, wake_flags, key);
94 if (ret < 0)
95 break;
96 if (ret && (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
97 break;
98
99 if (bookmark && (++cnt > WAITQUEUE_WALK_BREAK_CNT) &&
100 (&next->entry != &wq_head->head)) {
101 bookmark->flags = WQ_FLAG_BOOKMARK;
102 list_add_tail(&bookmark->entry, &next->entry);
103 break;
104 }
105 }
106
107 return nr_exclusive;
108 }
109
110 static void __wake_up_common_lock(struct wait_queue_head *wq_head, unsigned int mode,
111 int nr_exclusive, int wake_flags, void *key)
112 {
113 unsigned long flags;
114 wait_queue_entry_t bookmark;
115
116 bookmark.flags = 0;
117 bookmark.private = NULL;
118 bookmark.func = NULL;
119 INIT_LIST_HEAD(&bookmark.entry);
120
121 do {
122 spin_lock_irqsave(&wq_head->lock, flags);
123 nr_exclusive = __wake_up_common(wq_head, mode, nr_exclusive,
124 wake_flags, key, &bookmark);
125 spin_unlock_irqrestore(&wq_head->lock, flags);
126 } while (bookmark.flags & WQ_FLAG_BOOKMARK);
127 }
128
129
130
131
132
133
134
135
136
137
138
139 void __wake_up(struct wait_queue_head *wq_head, unsigned int mode,
140 int nr_exclusive, void *key)
141 {
142 __wake_up_common_lock(wq_head, mode, nr_exclusive, 0, key);
143 }
144 EXPORT_SYMBOL(__wake_up);
145
146
147
148
149 void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr)
150 {
151 __wake_up_common(wq_head, mode, nr, 0, NULL, NULL);
152 }
153 EXPORT_SYMBOL_GPL(__wake_up_locked);
154
155 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key)
156 {
157 __wake_up_common(wq_head, mode, 1, 0, key, NULL);
158 }
159 EXPORT_SYMBOL_GPL(__wake_up_locked_key);
160
161 void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
162 unsigned int mode, void *key, wait_queue_entry_t *bookmark)
163 {
164 __wake_up_common(wq_head, mode, 1, 0, key, bookmark);
165 }
166 EXPORT_SYMBOL_GPL(__wake_up_locked_key_bookmark);
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode,
186 int nr_exclusive, void *key)
187 {
188 int wake_flags = 1;
189
190 if (unlikely(!wq_head))
191 return;
192
193 if (unlikely(nr_exclusive != 1))
194 wake_flags = 0;
195
196 __wake_up_common_lock(wq_head, mode, nr_exclusive, wake_flags, key);
197 }
198 EXPORT_SYMBOL_GPL(__wake_up_sync_key);
199
200
201
202
203 void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode, int nr_exclusive)
204 {
205 __wake_up_sync_key(wq_head, mode, nr_exclusive, NULL);
206 }
207 EXPORT_SYMBOL_GPL(__wake_up_sync);
208
209
210
211
212
213
214
215
216
217
218
219
220
221 void
222 prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
223 {
224 unsigned long flags;
225
226 wq_entry->flags &= ~WQ_FLAG_EXCLUSIVE;
227 spin_lock_irqsave(&wq_head->lock, flags);
228 if (list_empty(&wq_entry->entry))
229 __add_wait_queue(wq_head, wq_entry);
230 set_current_state(state);
231 spin_unlock_irqrestore(&wq_head->lock, flags);
232 }
233 EXPORT_SYMBOL(prepare_to_wait);
234
235 void
236 prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
237 {
238 unsigned long flags;
239
240 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
241 spin_lock_irqsave(&wq_head->lock, flags);
242 if (list_empty(&wq_entry->entry))
243 __add_wait_queue_entry_tail(wq_head, wq_entry);
244 set_current_state(state);
245 spin_unlock_irqrestore(&wq_head->lock, flags);
246 }
247 EXPORT_SYMBOL(prepare_to_wait_exclusive);
248
249 void init_wait_entry(struct wait_queue_entry *wq_entry, int flags)
250 {
251 wq_entry->flags = flags;
252 wq_entry->private = current;
253 wq_entry->func = autoremove_wake_function;
254 INIT_LIST_HEAD(&wq_entry->entry);
255 }
256 EXPORT_SYMBOL(init_wait_entry);
257
258 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
259 {
260 unsigned long flags;
261 long ret = 0;
262
263 spin_lock_irqsave(&wq_head->lock, flags);
264 if (signal_pending_state(state, current)) {
265
266
267
268
269
270
271
272
273
274
275
276
277 list_del_init(&wq_entry->entry);
278 ret = -ERESTARTSYS;
279 } else {
280 if (list_empty(&wq_entry->entry)) {
281 if (wq_entry->flags & WQ_FLAG_EXCLUSIVE)
282 __add_wait_queue_entry_tail(wq_head, wq_entry);
283 else
284 __add_wait_queue(wq_head, wq_entry);
285 }
286 set_current_state(state);
287 }
288 spin_unlock_irqrestore(&wq_head->lock, flags);
289
290 return ret;
291 }
292 EXPORT_SYMBOL(prepare_to_wait_event);
293
294
295
296
297
298
299
300
301 int do_wait_intr(wait_queue_head_t *wq, wait_queue_entry_t *wait)
302 {
303 if (likely(list_empty(&wait->entry)))
304 __add_wait_queue_entry_tail(wq, wait);
305
306 set_current_state(TASK_INTERRUPTIBLE);
307 if (signal_pending(current))
308 return -ERESTARTSYS;
309
310 spin_unlock(&wq->lock);
311 schedule();
312 spin_lock(&wq->lock);
313
314 return 0;
315 }
316 EXPORT_SYMBOL(do_wait_intr);
317
318 int do_wait_intr_irq(wait_queue_head_t *wq, wait_queue_entry_t *wait)
319 {
320 if (likely(list_empty(&wait->entry)))
321 __add_wait_queue_entry_tail(wq, wait);
322
323 set_current_state(TASK_INTERRUPTIBLE);
324 if (signal_pending(current))
325 return -ERESTARTSYS;
326
327 spin_unlock_irq(&wq->lock);
328 schedule();
329 spin_lock_irq(&wq->lock);
330
331 return 0;
332 }
333 EXPORT_SYMBOL(do_wait_intr_irq);
334
335
336
337
338
339
340
341
342
343
344 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
345 {
346 unsigned long flags;
347
348 __set_current_state(TASK_RUNNING);
349
350
351
352
353
354
355
356
357
358
359
360
361
362 if (!list_empty_careful(&wq_entry->entry)) {
363 spin_lock_irqsave(&wq_head->lock, flags);
364 list_del_init(&wq_entry->entry);
365 spin_unlock_irqrestore(&wq_head->lock, flags);
366 }
367 }
368 EXPORT_SYMBOL(finish_wait);
369
370 int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key)
371 {
372 int ret = default_wake_function(wq_entry, mode, sync, key);
373
374 if (ret)
375 list_del_init(&wq_entry->entry);
376
377 return ret;
378 }
379 EXPORT_SYMBOL(autoremove_wake_function);
380
381 static inline bool is_kthread_should_stop(void)
382 {
383 return (current->flags & PF_KTHREAD) && kthread_should_stop();
384 }
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout)
407 {
408
409
410
411
412
413
414 set_current_state(mode);
415 if (!(wq_entry->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop())
416 timeout = schedule_timeout(timeout);
417 __set_current_state(TASK_RUNNING);
418
419
420
421
422
423
424
425 smp_store_mb(wq_entry->flags, wq_entry->flags & ~WQ_FLAG_WOKEN);
426
427 return timeout;
428 }
429 EXPORT_SYMBOL(wait_woken);
430
431 int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key)
432 {
433
434 smp_mb();
435 wq_entry->flags |= WQ_FLAG_WOKEN;
436
437 return default_wake_function(wq_entry, mode, sync, key);
438 }
439 EXPORT_SYMBOL(woken_wake_function);