Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * <linux/swait.h> (simple wait queues ) implementation:
0004  */
0005 
0006 void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
0007                  struct lock_class_key *key)
0008 {
0009     raw_spin_lock_init(&q->lock);
0010     lockdep_set_class_and_name(&q->lock, key, name);
0011     INIT_LIST_HEAD(&q->task_list);
0012 }
0013 EXPORT_SYMBOL(__init_swait_queue_head);
0014 
0015 /*
0016  * The thing about the wake_up_state() return value; I think we can ignore it.
0017  *
0018  * If for some reason it would return 0, that means the previously waiting
0019  * task is already running, so it will observe condition true (or has already).
0020  */
0021 void swake_up_locked(struct swait_queue_head *q)
0022 {
0023     struct swait_queue *curr;
0024 
0025     if (list_empty(&q->task_list))
0026         return;
0027 
0028     curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
0029     wake_up_process(curr->task);
0030     list_del_init(&curr->task_list);
0031 }
0032 EXPORT_SYMBOL(swake_up_locked);
0033 
0034 /*
0035  * Wake up all waiters. This is an interface which is solely exposed for
0036  * completions and not for general usage.
0037  *
0038  * It is intentionally different from swake_up_all() to allow usage from
0039  * hard interrupt context and interrupt disabled regions.
0040  */
0041 void swake_up_all_locked(struct swait_queue_head *q)
0042 {
0043     while (!list_empty(&q->task_list))
0044         swake_up_locked(q);
0045 }
0046 
0047 void swake_up_one(struct swait_queue_head *q)
0048 {
0049     unsigned long flags;
0050 
0051     raw_spin_lock_irqsave(&q->lock, flags);
0052     swake_up_locked(q);
0053     raw_spin_unlock_irqrestore(&q->lock, flags);
0054 }
0055 EXPORT_SYMBOL(swake_up_one);
0056 
0057 /*
0058  * Does not allow usage from IRQ disabled, since we must be able to
0059  * release IRQs to guarantee bounded hold time.
0060  */
0061 void swake_up_all(struct swait_queue_head *q)
0062 {
0063     struct swait_queue *curr;
0064     LIST_HEAD(tmp);
0065 
0066     raw_spin_lock_irq(&q->lock);
0067     list_splice_init(&q->task_list, &tmp);
0068     while (!list_empty(&tmp)) {
0069         curr = list_first_entry(&tmp, typeof(*curr), task_list);
0070 
0071         wake_up_state(curr->task, TASK_NORMAL);
0072         list_del_init(&curr->task_list);
0073 
0074         if (list_empty(&tmp))
0075             break;
0076 
0077         raw_spin_unlock_irq(&q->lock);
0078         raw_spin_lock_irq(&q->lock);
0079     }
0080     raw_spin_unlock_irq(&q->lock);
0081 }
0082 EXPORT_SYMBOL(swake_up_all);
0083 
0084 void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
0085 {
0086     wait->task = current;
0087     if (list_empty(&wait->task_list))
0088         list_add_tail(&wait->task_list, &q->task_list);
0089 }
0090 
0091 void prepare_to_swait_exclusive(struct swait_queue_head *q, struct swait_queue *wait, int state)
0092 {
0093     unsigned long flags;
0094 
0095     raw_spin_lock_irqsave(&q->lock, flags);
0096     __prepare_to_swait(q, wait);
0097     set_current_state(state);
0098     raw_spin_unlock_irqrestore(&q->lock, flags);
0099 }
0100 EXPORT_SYMBOL(prepare_to_swait_exclusive);
0101 
0102 long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
0103 {
0104     unsigned long flags;
0105     long ret = 0;
0106 
0107     raw_spin_lock_irqsave(&q->lock, flags);
0108     if (signal_pending_state(state, current)) {
0109         /*
0110          * See prepare_to_wait_event(). TL;DR, subsequent swake_up_one()
0111          * must not see us.
0112          */
0113         list_del_init(&wait->task_list);
0114         ret = -ERESTARTSYS;
0115     } else {
0116         __prepare_to_swait(q, wait);
0117         set_current_state(state);
0118     }
0119     raw_spin_unlock_irqrestore(&q->lock, flags);
0120 
0121     return ret;
0122 }
0123 EXPORT_SYMBOL(prepare_to_swait_event);
0124 
0125 void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
0126 {
0127     __set_current_state(TASK_RUNNING);
0128     if (!list_empty(&wait->task_list))
0129         list_del_init(&wait->task_list);
0130 }
0131 
0132 void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
0133 {
0134     unsigned long flags;
0135 
0136     __set_current_state(TASK_RUNNING);
0137 
0138     if (!list_empty_careful(&wait->task_list)) {
0139         raw_spin_lock_irqsave(&q->lock, flags);
0140         list_del_init(&wait->task_list);
0141         raw_spin_unlock_irqrestore(&q->lock, flags);
0142     }
0143 }
0144 EXPORT_SYMBOL(finish_swait);