1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/sched/signal.h>
3 #include <linux/swait.h>
5 void __init_swait_queue_head(struct swait_queue_head
*q
, const char *name
,
6 struct lock_class_key
*key
)
8 raw_spin_lock_init(&q
->lock
);
9 lockdep_set_class_and_name(&q
->lock
, key
, name
);
10 INIT_LIST_HEAD(&q
->task_list
);
12 EXPORT_SYMBOL(__init_swait_queue_head
);
15 * The thing about the wake_up_state() return value; I think we can ignore it.
17 * If for some reason it would return 0, that means the previously waiting
18 * task is already running, so it will observe condition true (or has already).
20 void swake_up_locked(struct swait_queue_head
*q
)
22 struct swait_queue
*curr
;
24 if (list_empty(&q
->task_list
))
27 curr
= list_first_entry(&q
->task_list
, typeof(*curr
), task_list
);
28 wake_up_process(curr
->task
);
29 list_del_init(&curr
->task_list
);
31 EXPORT_SYMBOL(swake_up_locked
);
33 void swake_up(struct swait_queue_head
*q
)
37 raw_spin_lock_irqsave(&q
->lock
, flags
);
39 raw_spin_unlock_irqrestore(&q
->lock
, flags
);
41 EXPORT_SYMBOL(swake_up
);
44 * Does not allow usage from IRQ disabled, since we must be able to
45 * release IRQs to guarantee bounded hold time.
47 void swake_up_all(struct swait_queue_head
*q
)
49 struct swait_queue
*curr
;
52 raw_spin_lock_irq(&q
->lock
);
53 list_splice_init(&q
->task_list
, &tmp
);
54 while (!list_empty(&tmp
)) {
55 curr
= list_first_entry(&tmp
, typeof(*curr
), task_list
);
57 wake_up_state(curr
->task
, TASK_NORMAL
);
58 list_del_init(&curr
->task_list
);
63 raw_spin_unlock_irq(&q
->lock
);
64 raw_spin_lock_irq(&q
->lock
);
66 raw_spin_unlock_irq(&q
->lock
);
68 EXPORT_SYMBOL(swake_up_all
);
70 void __prepare_to_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
)
73 if (list_empty(&wait
->task_list
))
74 list_add(&wait
->task_list
, &q
->task_list
);
77 void prepare_to_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
, int state
)
81 raw_spin_lock_irqsave(&q
->lock
, flags
);
82 __prepare_to_swait(q
, wait
);
83 set_current_state(state
);
84 raw_spin_unlock_irqrestore(&q
->lock
, flags
);
86 EXPORT_SYMBOL(prepare_to_swait
);
88 long prepare_to_swait_event(struct swait_queue_head
*q
, struct swait_queue
*wait
, int state
)
90 if (signal_pending_state(state
, current
))
93 prepare_to_swait(q
, wait
, state
);
97 EXPORT_SYMBOL(prepare_to_swait_event
);
99 void __finish_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
)
101 __set_current_state(TASK_RUNNING
);
102 if (!list_empty(&wait
->task_list
))
103 list_del_init(&wait
->task_list
);
106 void finish_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
)
110 __set_current_state(TASK_RUNNING
);
112 if (!list_empty_careful(&wait
->task_list
)) {
113 raw_spin_lock_irqsave(&q
->lock
, flags
);
114 list_del_init(&wait
->task_list
);
115 raw_spin_unlock_irqrestore(&q
->lock
, flags
);
118 EXPORT_SYMBOL(finish_swait
);