1 // SPDX-License-Identifier: GPL-2.0
3 * <linux/swait.h> (simple wait queues ) implementation:
6 void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
7 struct lock_class_key *key)
9 raw_spin_lock_init(&q->lock);
10 lockdep_set_class_and_name(&q->lock, key, name);
11 INIT_LIST_HEAD(&q->task_list);
13 EXPORT_SYMBOL(__init_swait_queue_head);
16 * The thing about the wake_up_state() return value; I think we can ignore it.
18 * If for some reason it would return 0, that means the previously waiting
19 * task is already running, so it will observe condition true (or has already).
21 void swake_up_locked(struct swait_queue_head *q, int wake_flags)
23 struct swait_queue *curr;
25 if (list_empty(&q->task_list))
28 curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
29 try_to_wake_up(curr->task, TASK_NORMAL, wake_flags);
30 list_del_init(&curr->task_list);
32 EXPORT_SYMBOL(swake_up_locked);
35 * Wake up all waiters. This is an interface which is solely exposed for
36 * completions and not for general usage.
38 * It is intentionally different from swake_up_all() to allow usage from
39 * hard interrupt context and interrupt disabled regions.
41 void swake_up_all_locked(struct swait_queue_head *q)
43 while (!list_empty(&q->task_list))
44 swake_up_locked(q, 0);
47 void swake_up_one(struct swait_queue_head *q)
51 raw_spin_lock_irqsave(&q->lock, flags);
52 swake_up_locked(q, 0);
53 raw_spin_unlock_irqrestore(&q->lock, flags);
55 EXPORT_SYMBOL(swake_up_one);
58 * Does not allow usage from IRQ disabled, since we must be able to
59 * release IRQs to guarantee bounded hold time.
61 void swake_up_all(struct swait_queue_head *q)
63 struct swait_queue *curr;
66 raw_spin_lock_irq(&q->lock);
67 list_splice_init(&q->task_list, &tmp);
68 while (!list_empty(&tmp)) {
69 curr = list_first_entry(&tmp, typeof(*curr), task_list);
71 wake_up_state(curr->task, TASK_NORMAL);
72 list_del_init(&curr->task_list);
77 raw_spin_unlock_irq(&q->lock);
78 raw_spin_lock_irq(&q->lock);
80 raw_spin_unlock_irq(&q->lock);
82 EXPORT_SYMBOL(swake_up_all);
84 void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
87 if (list_empty(&wait->task_list))
88 list_add_tail(&wait->task_list, &q->task_list);
91 void prepare_to_swait_exclusive(struct swait_queue_head *q, struct swait_queue *wait, int state)
95 raw_spin_lock_irqsave(&q->lock, flags);
96 __prepare_to_swait(q, wait);
97 set_current_state(state);
98 raw_spin_unlock_irqrestore(&q->lock, flags);
100 EXPORT_SYMBOL(prepare_to_swait_exclusive);
102 long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
107 raw_spin_lock_irqsave(&q->lock, flags);
108 if (signal_pending_state(state, current)) {
110 * See prepare_to_wait_event(). TL;DR, subsequent swake_up_one()
113 list_del_init(&wait->task_list);
116 __prepare_to_swait(q, wait);
117 set_current_state(state);
119 raw_spin_unlock_irqrestore(&q->lock, flags);
123 EXPORT_SYMBOL(prepare_to_swait_event);
125 void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
127 __set_current_state(TASK_RUNNING);
128 if (!list_empty(&wait->task_list))
129 list_del_init(&wait->task_list);
132 void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
136 __set_current_state(TASK_RUNNING);
138 if (!list_empty_careful(&wait->task_list)) {
139 raw_spin_lock_irqsave(&q->lock, flags);
140 list_del_init(&wait->task_list);
141 raw_spin_unlock_irqrestore(&q->lock, flags);
144 EXPORT_SYMBOL(finish_swait);