]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/linux/swait.h
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 regular wait queues (wait.h) the most
13 * important difference is that the simple waitqueue allows for deterministic
14 * behaviour -- IOW it has strictly bounded IRQ and lock hold times.
16 * Mainly, this is accomplished by two things. Firstly not allowing swake_up_all
17 * from IRQ disabled, and dropping the lock upon every wakeup, giving a higher
18 * priority task a chance to run.
20 * Secondly, we had to drop a fair number of features of the other waitqueue
23 * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue;
24 * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right
27 * - the exclusive mode; because this requires preserving the list order
30 * - custom wake callback functions; because you cannot give any guarantees
31 * about random code. This also allows swait to be used in RT, such that
32 * raw spinlock can be used for the swait queue head.
34 * As a side effect of these; the data structures are slimmer albeit more ad-hoc.
35 * For all the above, note that simple wait queues should _only_ be used under
36 * very specific realtime constraints -- it is best to stick with the regular
37 * wait queues in most cases.
42 struct swait_queue_head
{
44 struct list_head task_list
;
48 struct task_struct
*task
;
49 struct list_head task_list
;
52 #define __SWAITQUEUE_INITIALIZER(name) { \
54 .task_list = LIST_HEAD_INIT((name).task_list), \
57 #define DECLARE_SWAITQUEUE(name) \
58 struct swait_queue name = __SWAITQUEUE_INITIALIZER(name)
60 #define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \
61 .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
62 .task_list = LIST_HEAD_INIT((name).task_list), \
65 #define DECLARE_SWAIT_QUEUE_HEAD(name) \
66 struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name)
68 extern void __init_swait_queue_head(struct swait_queue_head
*q
, const char *name
,
69 struct lock_class_key
*key
);
71 #define init_swait_queue_head(q) \
73 static struct lock_class_key __key; \
74 __init_swait_queue_head((q), #q, &__key); \
78 # define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
79 ({ init_swait_queue_head(&name); name; })
80 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
81 struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name)
83 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
84 DECLARE_SWAIT_QUEUE_HEAD(name)
88 * swait_active -- locklessly test for waiters on the queue
89 * @wq: the waitqueue to test for waiters
91 * returns true if the wait list is not empty
93 * NOTE: this function is lockless and requires care, incorrect usage _will_
94 * lead to sporadic and non-obvious failure.
96 * NOTE2: this function has the same above implications as regular waitqueues.
98 * Use either while holding swait_queue_head::lock or when used for wakeups
99 * with an extra smp_mb() like:
101 * CPU0 - waker CPU1 - waiter
104 * @cond = true; prepare_to_swait(&wq_head, &wait, state);
105 * smp_mb(); // smp_mb() from set_current_state()
106 * if (swait_active(wq_head)) if (@cond)
107 * wake_up(wq_head); break;
110 * finish_swait(&wq_head, &wait);
112 * Because without the explicit smp_mb() it's possible for the
113 * swait_active() load to get hoisted over the @cond store such that we'll
114 * observe an empty wait list while the waiter might not observe @cond.
115 * This, in turn, can trigger missing wakeups.
117 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
118 * which (when the lock is uncontended) are of roughly equal cost.
120 static inline int swait_active(struct swait_queue_head
*wq
)
122 return !list_empty(&wq
->task_list
);
126 * swq_has_sleeper - check if there are any waiting processes
127 * @wq: the waitqueue to test for waiters
129 * Returns true if @wq has waiting processes
131 * Please refer to the comment for swait_active.
133 static inline bool swq_has_sleeper(struct swait_queue_head
*wq
)
136 * We need to be sure we are in sync with the list_add()
137 * modifications to the wait queue (task_list).
139 * This memory barrier should be paired with one on the
143 return swait_active(wq
);
146 extern void swake_up(struct swait_queue_head
*q
);
147 extern void swake_up_all(struct swait_queue_head
*q
);
148 extern void swake_up_locked(struct swait_queue_head
*q
);
150 extern void __prepare_to_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
);
151 extern void prepare_to_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
, int state
);
152 extern long prepare_to_swait_event(struct swait_queue_head
*q
, struct swait_queue
*wait
, int state
);
154 extern void __finish_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
);
155 extern void finish_swait(struct swait_queue_head
*q
, struct swait_queue
*wait
);
157 /* as per ___wait_event() but for swait, therefore "exclusive == 0" */
158 #define ___swait_event(wq, condition, state, ret, cmd) \
160 struct swait_queue __wait; \
163 INIT_LIST_HEAD(&__wait.task_list); \
165 long __int = prepare_to_swait_event(&wq, &__wait, state);\
170 if (___wait_is_interruptible(state) && __int) { \
177 finish_swait(&wq, &__wait); \
181 #define __swait_event(wq, condition) \
182 (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
185 #define swait_event(wq, condition) \
189 __swait_event(wq, condition); \
192 #define __swait_event_timeout(wq, condition, timeout) \
193 ___swait_event(wq, ___wait_cond_timeout(condition), \
194 TASK_UNINTERRUPTIBLE, timeout, \
195 __ret = schedule_timeout(__ret))
197 #define swait_event_timeout(wq, condition, timeout) \
199 long __ret = timeout; \
200 if (!___wait_cond_timeout(condition)) \
201 __ret = __swait_event_timeout(wq, condition, timeout); \
205 #define __swait_event_interruptible(wq, condition) \
206 ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \
209 #define swait_event_interruptible(wq, condition) \
213 __ret = __swait_event_interruptible(wq, condition); \
217 #define __swait_event_interruptible_timeout(wq, condition, timeout) \
218 ___swait_event(wq, ___wait_cond_timeout(condition), \
219 TASK_INTERRUPTIBLE, timeout, \
220 __ret = schedule_timeout(__ret))
222 #define swait_event_interruptible_timeout(wq, condition, timeout) \
224 long __ret = timeout; \
225 if (!___wait_cond_timeout(condition)) \
226 __ret = __swait_event_interruptible_timeout(wq, \
227 condition, timeout); \
231 #define __swait_event_idle(wq, condition) \
232 (void)___swait_event(wq, condition, TASK_IDLE, 0, schedule())
235 * swait_event_idle - wait without system load contribution
236 * @wq: the waitqueue to wait on
237 * @condition: a C expression for the event to wait for
239 * The process is put to sleep (TASK_IDLE) until the @condition evaluates to
240 * true. The @condition is checked each time the waitqueue @wq is woken up.
242 * This function is mostly used when a kthread or workqueue waits for some
243 * condition and doesn't want to contribute to system load. Signals are
246 #define swait_event_idle(wq, condition) \
250 __swait_event_idle(wq, condition); \
253 #define __swait_event_idle_timeout(wq, condition, timeout) \
254 ___swait_event(wq, ___wait_cond_timeout(condition), \
255 TASK_IDLE, timeout, \
256 __ret = schedule_timeout(__ret))
259 * swait_event_idle_timeout - wait up to timeout without load contribution
260 * @wq: the waitqueue to wait on
261 * @condition: a C expression for the event to wait for
262 * @timeout: timeout at which we'll give up in jiffies
264 * The process is put to sleep (TASK_IDLE) until the @condition evaluates to
265 * true. The @condition is checked each time the waitqueue @wq is woken up.
267 * This function is mostly used when a kthread or workqueue waits for some
268 * condition and doesn't want to contribute to system load. Signals are
272 * 0 if the @condition evaluated to %false after the @timeout elapsed,
273 * 1 if the @condition evaluated to %true after the @timeout elapsed,
274 * or the remaining jiffies (at least 1) if the @condition evaluated
275 * to %true before the @timeout elapsed.
277 #define swait_event_idle_timeout(wq, condition, timeout) \
279 long __ret = timeout; \
280 if (!___wait_cond_timeout(condition)) \
281 __ret = __swait_event_idle_timeout(wq, \
282 condition, timeout); \
286 #endif /* _LINUX_SWAIT_H */