Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * <linux/swait.h> (simple wait queues ) implementation:
4 */
5
6void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
7 struct lock_class_key *key)
8{
9 raw_spin_lock_init(&q->lock);
10 lockdep_set_class_and_name(&q->lock, key, name);
11 INIT_LIST_HEAD(&q->task_list);
12}
13EXPORT_SYMBOL(__init_swait_queue_head);
14
15/*
16 * The thing about the wake_up_state() return value; I think we can ignore it.
17 *
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).
20 */
21void swake_up_locked(struct swait_queue_head *q)
22{
23 struct swait_queue *curr;
24
25 if (list_empty(&q->task_list))
26 return;
27
28 curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
29 wake_up_process(curr->task);
30 list_del_init(&curr->task_list);
31}
32EXPORT_SYMBOL(swake_up_locked);
33
34/*
35 * Wake up all waiters. This is an interface which is solely exposed for
36 * completions and not for general usage.
37 *
38 * It is intentionally different from swake_up_all() to allow usage from
39 * hard interrupt context and interrupt disabled regions.
40 */
41void swake_up_all_locked(struct swait_queue_head *q)
42{
43 while (!list_empty(&q->task_list))
44 swake_up_locked(q);
45}
46
47void swake_up_one(struct swait_queue_head *q)
48{
49 unsigned long flags;
50
51 raw_spin_lock_irqsave(&q->lock, flags);
52 swake_up_locked(q);
53 raw_spin_unlock_irqrestore(&q->lock, flags);
54}
55EXPORT_SYMBOL(swake_up_one);
56
57/*
58 * Does not allow usage from IRQ disabled, since we must be able to
59 * release IRQs to guarantee bounded hold time.
60 */
61void swake_up_all(struct swait_queue_head *q)
62{
63 struct swait_queue *curr;
64 LIST_HEAD(tmp);
65
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);
70
71 wake_up_state(curr->task, TASK_NORMAL);
72 list_del_init(&curr->task_list);
73
74 if (list_empty(&tmp))
75 break;
76
77 raw_spin_unlock_irq(&q->lock);
78 raw_spin_lock_irq(&q->lock);
79 }
80 raw_spin_unlock_irq(&q->lock);
81}
82EXPORT_SYMBOL(swake_up_all);
83
84void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
85{
86 wait->task = current;
87 if (list_empty(&wait->task_list))
88 list_add_tail(&wait->task_list, &q->task_list);
89}
90
91void prepare_to_swait_exclusive(struct swait_queue_head *q, struct swait_queue *wait, int state)
92{
93 unsigned long flags;
94
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);
99}
100EXPORT_SYMBOL(prepare_to_swait_exclusive);
101
102long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
103{
104 unsigned long flags;
105 long ret = 0;
106
107 raw_spin_lock_irqsave(&q->lock, flags);
108 if (signal_pending_state(state, current)) {
109 /*
110 * See prepare_to_wait_event(). TL;DR, subsequent swake_up_one()
111 * must not see us.
112 */
113 list_del_init(&wait->task_list);
114 ret = -ERESTARTSYS;
115 } else {
116 __prepare_to_swait(q, wait);
117 set_current_state(state);
118 }
119 raw_spin_unlock_irqrestore(&q->lock, flags);
120
121 return ret;
122}
123EXPORT_SYMBOL(prepare_to_swait_event);
124
125void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
126{
127 __set_current_state(TASK_RUNNING);
128 if (!list_empty(&wait->task_list))
129 list_del_init(&wait->task_list);
130}
131
132void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
133{
134 unsigned long flags;
135
136 __set_current_state(TASK_RUNNING);
137
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);
142 }
143}
144EXPORT_SYMBOL(finish_swait);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * <linux/swait.h> (simple wait queues ) implementation:
4 */
5#include "sched.h"
6
7void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
8 struct lock_class_key *key)
9{
10 raw_spin_lock_init(&q->lock);
11 lockdep_set_class_and_name(&q->lock, key, name);
12 INIT_LIST_HEAD(&q->task_list);
13}
14EXPORT_SYMBOL(__init_swait_queue_head);
15
16/*
17 * The thing about the wake_up_state() return value; I think we can ignore it.
18 *
19 * If for some reason it would return 0, that means the previously waiting
20 * task is already running, so it will observe condition true (or has already).
21 */
22void swake_up_locked(struct swait_queue_head *q)
23{
24 struct swait_queue *curr;
25
26 if (list_empty(&q->task_list))
27 return;
28
29 curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
30 wake_up_process(curr->task);
31 list_del_init(&curr->task_list);
32}
33EXPORT_SYMBOL(swake_up_locked);
34
35/*
36 * Wake up all waiters. This is an interface which is solely exposed for
37 * completions and not for general usage.
38 *
39 * It is intentionally different from swake_up_all() to allow usage from
40 * hard interrupt context and interrupt disabled regions.
41 */
42void swake_up_all_locked(struct swait_queue_head *q)
43{
44 while (!list_empty(&q->task_list))
45 swake_up_locked(q);
46}
47
48void swake_up_one(struct swait_queue_head *q)
49{
50 unsigned long flags;
51
52 raw_spin_lock_irqsave(&q->lock, flags);
53 swake_up_locked(q);
54 raw_spin_unlock_irqrestore(&q->lock, flags);
55}
56EXPORT_SYMBOL(swake_up_one);
57
58/*
59 * Does not allow usage from IRQ disabled, since we must be able to
60 * release IRQs to guarantee bounded hold time.
61 */
62void swake_up_all(struct swait_queue_head *q)
63{
64 struct swait_queue *curr;
65 LIST_HEAD(tmp);
66
67 raw_spin_lock_irq(&q->lock);
68 list_splice_init(&q->task_list, &tmp);
69 while (!list_empty(&tmp)) {
70 curr = list_first_entry(&tmp, typeof(*curr), task_list);
71
72 wake_up_state(curr->task, TASK_NORMAL);
73 list_del_init(&curr->task_list);
74
75 if (list_empty(&tmp))
76 break;
77
78 raw_spin_unlock_irq(&q->lock);
79 raw_spin_lock_irq(&q->lock);
80 }
81 raw_spin_unlock_irq(&q->lock);
82}
83EXPORT_SYMBOL(swake_up_all);
84
85void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
86{
87 wait->task = current;
88 if (list_empty(&wait->task_list))
89 list_add_tail(&wait->task_list, &q->task_list);
90}
91
92void prepare_to_swait_exclusive(struct swait_queue_head *q, struct swait_queue *wait, int state)
93{
94 unsigned long flags;
95
96 raw_spin_lock_irqsave(&q->lock, flags);
97 __prepare_to_swait(q, wait);
98 set_current_state(state);
99 raw_spin_unlock_irqrestore(&q->lock, flags);
100}
101EXPORT_SYMBOL(prepare_to_swait_exclusive);
102
103long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
104{
105 unsigned long flags;
106 long ret = 0;
107
108 raw_spin_lock_irqsave(&q->lock, flags);
109 if (signal_pending_state(state, current)) {
110 /*
111 * See prepare_to_wait_event(). TL;DR, subsequent swake_up_one()
112 * must not see us.
113 */
114 list_del_init(&wait->task_list);
115 ret = -ERESTARTSYS;
116 } else {
117 __prepare_to_swait(q, wait);
118 set_current_state(state);
119 }
120 raw_spin_unlock_irqrestore(&q->lock, flags);
121
122 return ret;
123}
124EXPORT_SYMBOL(prepare_to_swait_event);
125
126void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
127{
128 __set_current_state(TASK_RUNNING);
129 if (!list_empty(&wait->task_list))
130 list_del_init(&wait->task_list);
131}
132
133void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
134{
135 unsigned long flags;
136
137 __set_current_state(TASK_RUNNING);
138
139 if (!list_empty_careful(&wait->task_list)) {
140 raw_spin_lock_irqsave(&q->lock, flags);
141 list_del_init(&wait->task_list);
142 raw_spin_unlock_irqrestore(&q->lock, flags);
143 }
144}
145EXPORT_SYMBOL(finish_swait);