]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/sched/swait.c
sched: Clean up and harmonize the coding style of the scheduler code base
[mirror_ubuntu-jammy-kernel.git] / kernel / sched / swait.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
97fb7a0a
IM
2/*
3 * <linux/swait.h> (simple wait queues ) implementation:
4 */
174cd4b1 5#include <linux/sched/signal.h>
13b35686
PZI
6#include <linux/swait.h>
7
8void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
9 struct lock_class_key *key)
10{
11 raw_spin_lock_init(&q->lock);
12 lockdep_set_class_and_name(&q->lock, key, name);
13 INIT_LIST_HEAD(&q->task_list);
14}
15EXPORT_SYMBOL(__init_swait_queue_head);
16
17/*
18 * The thing about the wake_up_state() return value; I think we can ignore it.
19 *
20 * If for some reason it would return 0, that means the previously waiting
21 * task is already running, so it will observe condition true (or has already).
22 */
23void swake_up_locked(struct swait_queue_head *q)
24{
25 struct swait_queue *curr;
26
27 if (list_empty(&q->task_list))
28 return;
29
30 curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
31 wake_up_process(curr->task);
32 list_del_init(&curr->task_list);
33}
34EXPORT_SYMBOL(swake_up_locked);
35
36void swake_up(struct swait_queue_head *q)
37{
38 unsigned long flags;
39
13b35686
PZI
40 raw_spin_lock_irqsave(&q->lock, flags);
41 swake_up_locked(q);
42 raw_spin_unlock_irqrestore(&q->lock, flags);
43}
44EXPORT_SYMBOL(swake_up);
45
46/*
47 * Does not allow usage from IRQ disabled, since we must be able to
48 * release IRQs to guarantee bounded hold time.
49 */
50void swake_up_all(struct swait_queue_head *q)
51{
52 struct swait_queue *curr;
53 LIST_HEAD(tmp);
54
13b35686
PZI
55 raw_spin_lock_irq(&q->lock);
56 list_splice_init(&q->task_list, &tmp);
57 while (!list_empty(&tmp)) {
58 curr = list_first_entry(&tmp, typeof(*curr), task_list);
59
60 wake_up_state(curr->task, TASK_NORMAL);
61 list_del_init(&curr->task_list);
62
63 if (list_empty(&tmp))
64 break;
65
66 raw_spin_unlock_irq(&q->lock);
67 raw_spin_lock_irq(&q->lock);
68 }
69 raw_spin_unlock_irq(&q->lock);
70}
71EXPORT_SYMBOL(swake_up_all);
72
73void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
74{
75 wait->task = current;
76 if (list_empty(&wait->task_list))
77 list_add(&wait->task_list, &q->task_list);
78}
79
80void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state)
81{
82 unsigned long flags;
83
84 raw_spin_lock_irqsave(&q->lock, flags);
85 __prepare_to_swait(q, wait);
86 set_current_state(state);
87 raw_spin_unlock_irqrestore(&q->lock, flags);
88}
89EXPORT_SYMBOL(prepare_to_swait);
90
91long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
92{
93 if (signal_pending_state(state, current))
94 return -ERESTARTSYS;
95
96 prepare_to_swait(q, wait, state);
97
98 return 0;
99}
100EXPORT_SYMBOL(prepare_to_swait_event);
101
102void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
103{
104 __set_current_state(TASK_RUNNING);
105 if (!list_empty(&wait->task_list))
106 list_del_init(&wait->task_list);
107}
108
109void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
110{
111 unsigned long flags;
112
113 __set_current_state(TASK_RUNNING);
114
115 if (!list_empty_careful(&wait->task_list)) {
116 raw_spin_lock_irqsave(&q->lock, flags);
117 list_del_init(&wait->task_list);
118 raw_spin_unlock_irqrestore(&q->lock, flags);
119 }
120}
121EXPORT_SYMBOL(finish_swait);