4 #include <linux/list.h>
5 #include <linux/stddef.h>
6 #include <linux/spinlock.h>
7 #include <asm/current.h>
12 * While these are very similar to the other/complex wait queues (wait.h) the
13 * most important difference is that the simple waitqueue allows for
14 * deterministic behaviour -- IOW it has strictly bounded IRQ and lock hold
17 * In order to make this so, we had to drop a fair number of features of the
18 * other waitqueue code; notably:
20 * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue;
21 * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right
24 * - the exclusive mode; because this requires preserving the list order
27 * - custom wake functions; because you cannot give any guarantees about
30 * As a side effect of this; the data structures are slimmer.
32 * One would recommend using this wait queue where possible.
37 struct swait_queue_head {
39 struct list_head task_list;
43 struct task_struct *task;
44 struct list_head task_list;
47 #define __SWAITQUEUE_INITIALIZER(name) { \
49 .task_list = LIST_HEAD_INIT((name).task_list), \
52 #define DECLARE_SWAITQUEUE(name) \
53 struct swait_queue name = __SWAITQUEUE_INITIALIZER(name)
55 #define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \
56 .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
57 .task_list = LIST_HEAD_INIT((name).task_list), \
60 #define DECLARE_SWAIT_QUEUE_HEAD(name) \
61 struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name)
63 extern void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
64 struct lock_class_key *key);
66 #define init_swait_queue_head(q) \
68 static struct lock_class_key __key; \
69 __init_swait_queue_head((q), #q, &__key); \
73 # define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
74 ({ init_swait_queue_head(&name); name; })
75 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
76 struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name)
78 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
79 DECLARE_SWAIT_QUEUE_HEAD(name)
83 * swait_active -- locklessly test for waiters on the queue
84 * @wq: the waitqueue to test for waiters
86 * returns true if the wait list is not empty
88 * NOTE: this function is lockless and requires care, incorrect usage _will_
89 * lead to sporadic and non-obvious failure.
91 * NOTE2: this function has the same above implications as regular waitqueues.
93 * Use either while holding swait_queue_head::lock or when used for wakeups
94 * with an extra smp_mb() like:
96 * CPU0 - waker CPU1 - waiter
99 * @cond = true; prepare_to_swait(&wq_head, &wait, state);
100 * smp_mb(); // smp_mb() from set_current_state()
101 * if (swait_active(wq_head)) if (@cond)
102 * wake_up(wq_head); break;
105 * finish_swait(&wq_head, &wait);
107 * Because without the explicit smp_mb() it's possible for the
108 * swait_active() load to get hoisted over the @cond store such that we'll
109 * observe an empty wait list while the waiter might not observe @cond.
110 * This, in turn, can trigger missing wakeups.
112 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
113 * which (when the lock is uncontended) are of roughly equal cost.
115 static inline int swait_active(struct swait_queue_head *wq)
117 return !list_empty(&wq->task_list);
121 * swq_has_sleeper - check if there are any waiting processes
122 * @wq: the waitqueue to test for waiters
124 * Returns true if @wq has waiting processes
126 * Please refer to the comment for swait_active.
128 static inline bool swq_has_sleeper(struct swait_queue_head *wq)
131 * We need to be sure we are in sync with the list_add()
132 * modifications to the wait queue (task_list).
134 * This memory barrier should be paired with one on the
138 return swait_active(wq);
141 extern void swake_up(struct swait_queue_head *q);
142 extern void swake_up_all(struct swait_queue_head *q);
143 extern void swake_up_locked(struct swait_queue_head *q);
145 extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
146 extern void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state);
147 extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state);
149 extern void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
150 extern void finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
152 /* as per ___wait_event() but for swait, therefore "exclusive == 0" */
153 #define ___swait_event(wq, condition, state, ret, cmd) \
155 struct swait_queue __wait; \
158 INIT_LIST_HEAD(&__wait.task_list); \
160 long __int = prepare_to_swait_event(&wq, &__wait, state);\
165 if (___wait_is_interruptible(state) && __int) { \
172 finish_swait(&wq, &__wait); \
176 #define __swait_event(wq, condition) \
177 (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
180 #define swait_event(wq, condition) \
184 __swait_event(wq, condition); \
187 #define __swait_event_timeout(wq, condition, timeout) \
188 ___swait_event(wq, ___wait_cond_timeout(condition), \
189 TASK_UNINTERRUPTIBLE, timeout, \
190 __ret = schedule_timeout(__ret))
192 #define swait_event_timeout(wq, condition, timeout) \
194 long __ret = timeout; \
195 if (!___wait_cond_timeout(condition)) \
196 __ret = __swait_event_timeout(wq, condition, timeout); \
200 #define __swait_event_interruptible(wq, condition) \
201 ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \
204 #define swait_event_interruptible(wq, condition) \
208 __ret = __swait_event_interruptible(wq, condition); \
212 #define __swait_event_interruptible_timeout(wq, condition, timeout) \
213 ___swait_event(wq, ___wait_cond_timeout(condition), \
214 TASK_INTERRUPTIBLE, timeout, \
215 __ret = schedule_timeout(__ret))
217 #define swait_event_interruptible_timeout(wq, condition, timeout) \
219 long __ret = timeout; \
220 if (!___wait_cond_timeout(condition)) \
221 __ret = __swait_event_interruptible_timeout(wq, \
222 condition, timeout); \
226 #define __swait_event_idle(wq, condition) \
227 (void)___swait_event(wq, condition, TASK_IDLE, 0, schedule())
230 * swait_event_idle - wait without system load contribution
231 * @wq: the waitqueue to wait on
232 * @condition: a C expression for the event to wait for
234 * The process is put to sleep (TASK_IDLE) until the @condition evaluates to
235 * true. The @condition is checked each time the waitqueue @wq is woken up.
237 * This function is mostly used when a kthread or workqueue waits for some
238 * condition and doesn't want to contribute to system load. Signals are
241 #define swait_event_idle(wq, condition) \
245 __swait_event_idle(wq, condition); \
248 #define __swait_event_idle_timeout(wq, condition, timeout) \
249 ___swait_event(wq, ___wait_cond_timeout(condition), \
250 TASK_IDLE, timeout, \
251 __ret = schedule_timeout(__ret))
254 * swait_event_idle_timeout - wait up to timeout without load contribution
255 * @wq: the waitqueue to wait on
256 * @condition: a C expression for the event to wait for
257 * @timeout: timeout at which we'll give up in jiffies
259 * The process is put to sleep (TASK_IDLE) until the @condition evaluates to
260 * true. The @condition is checked each time the waitqueue @wq is woken up.
262 * This function is mostly used when a kthread or workqueue waits for some
263 * condition and doesn't want to contribute to system load. Signals are
267 * 0 if the @condition evaluated to %false after the @timeout elapsed,
268 * 1 if the @condition evaluated to %true after the @timeout elapsed,
269 * or the remaining jiffies (at least 1) if the @condition evaluated
270 * to %true before the @timeout elapsed.
272 #define swait_event_idle_timeout(wq, condition, timeout) \
274 long __ret = timeout; \
275 if (!___wait_cond_timeout(condition)) \
276 __ret = __swait_event_idle_timeout(wq, \
277 condition, timeout); \
281 #endif /* _LINUX_SWAIT_H */