Linux 4.15.6
[linux/fpc-iii.git] / kernel / sched / swait.c
blob9ff1555341ed5d47ba6302da0a2e74acfa6bb16c
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))
25 return;
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)
35 unsigned long flags;
37 raw_spin_lock_irqsave(&q->lock, flags);
38 swake_up_locked(q);
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;
50 LIST_HEAD(tmp);
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);
60 if (list_empty(&tmp))
61 break;
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)
72 wait->task = current;
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)
79 unsigned long flags;
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))
91 return -ERESTARTSYS;
93 prepare_to_swait(q, wait, state);
95 return 0;
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)
108 unsigned long flags;
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);