2 * Simple waitqueues without fancy flags and callbacks
4 * (C) 2011 Thomas Gleixner <tglx@linutronix.de>
6 * Based on kernel/wait.c
8 * For licencing details see kernel-base/COPYING
10 #include <linux/init.h>
11 #include <linux/export.h>
12 #include <linux/sched.h>
13 #include <linux/wait-simple.h>
15 /* Adds w to head->list. Must be called with head->lock locked. */
16 static inline void __swait_enqueue(struct swait_head *head, struct swaiter *w)
18 list_add(&w->node, &head->list);
19 /* We can't let the condition leak before the setting of head */
23 /* Removes w from head->list. Must be called with head->lock locked. */
24 static inline void __swait_dequeue(struct swaiter *w)
26 list_del_init(&w->node);
29 void __init_swait_head(struct swait_head *head, struct lock_class_key *key)
31 raw_spin_lock_init(&head->lock);
32 lockdep_set_class(&head->lock, key);
33 INIT_LIST_HEAD(&head->list);
35 EXPORT_SYMBOL(__init_swait_head);
37 void swait_prepare_locked(struct swait_head *head, struct swaiter *w)
40 if (list_empty(&w->node))
41 __swait_enqueue(head, w);
44 void swait_prepare(struct swait_head *head, struct swaiter *w, int state)
48 raw_spin_lock_irqsave(&head->lock, flags);
49 swait_prepare_locked(head, w);
50 __set_current_state(state);
51 raw_spin_unlock_irqrestore(&head->lock, flags);
53 EXPORT_SYMBOL(swait_prepare);
55 void swait_finish_locked(struct swait_head *head, struct swaiter *w)
57 __set_current_state(TASK_RUNNING);
62 void swait_finish(struct swait_head *head, struct swaiter *w)
66 __set_current_state(TASK_RUNNING);
68 raw_spin_lock_irqsave(&head->lock, flags);
70 raw_spin_unlock_irqrestore(&head->lock, flags);
73 EXPORT_SYMBOL(swait_finish);
76 __swait_wake_locked(struct swait_head *head, unsigned int state, unsigned int num)
78 struct swaiter *curr, *next;
81 list_for_each_entry_safe(curr, next, &head->list, node) {
82 if (wake_up_state(curr->task, state)) {
83 __swait_dequeue(curr);
85 * The waiting task can free the waiter as
86 * soon as curr->task = NULL is written,
87 * without taking any locks. A memory barrier
88 * is required here to prevent the following
89 * store to curr->task from getting ahead of
90 * the dequeue operation.
102 __swait_wake(struct swait_head *head, unsigned int state, unsigned int num)
107 if (!swaitqueue_active(head))
110 raw_spin_lock_irqsave(&head->lock, flags);
111 woken = __swait_wake_locked(head, state, num);
112 raw_spin_unlock_irqrestore(&head->lock, flags);
115 EXPORT_SYMBOL(__swait_wake);