xref: /linux-6.15/include/linux/wait.h (revision 25cd480e)
1 #ifndef _LINUX_WAIT_H
2 #define _LINUX_WAIT_H
3 /*
4  * Linux wait queue related types and methods
5  */
6 #include <linux/list.h>
7 #include <linux/stddef.h>
8 #include <linux/spinlock.h>
9 #include <asm/current.h>
10 #include <uapi/linux/wait.h>
11 
12 typedef struct __wait_queue wait_queue_t;
13 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
14 int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
15 
16 /* __wait_queue::flags */
17 #define WQ_FLAG_EXCLUSIVE	0x01
18 #define WQ_FLAG_WOKEN		0x02
19 
20 struct __wait_queue {
21 	unsigned int		flags;
22 	void			*private;
23 	wait_queue_func_t	func;
24 	struct list_head	task_list;
25 };
26 
27 struct wait_bit_key {
28 	void			*flags;
29 	int			bit_nr;
30 #define WAIT_ATOMIC_T_BIT_NR	-1
31 	unsigned long		timeout;
32 };
33 
34 struct wait_bit_queue {
35 	struct wait_bit_key	key;
36 	wait_queue_t		wait;
37 };
38 
39 struct __wait_queue_head {
40 	spinlock_t		lock;
41 	struct list_head	task_list;
42 };
43 typedef struct __wait_queue_head wait_queue_head_t;
44 
45 struct task_struct;
46 
47 /*
48  * Macros for declaration and initialisaton of the datatypes
49  */
50 
51 #define __WAITQUEUE_INITIALIZER(name, tsk) {				\
52 	.private	= tsk,						\
53 	.func		= default_wake_function,			\
54 	.task_list	= { NULL, NULL } }
55 
56 #define DECLARE_WAITQUEUE(name, tsk)					\
57 	wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
58 
59 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) {				\
60 	.lock		= __SPIN_LOCK_UNLOCKED(name.lock),		\
61 	.task_list	= { &(name).task_list, &(name).task_list } }
62 
63 #define DECLARE_WAIT_QUEUE_HEAD(name) \
64 	wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
65 
66 #define __WAIT_BIT_KEY_INITIALIZER(word, bit)				\
67 	{ .flags = word, .bit_nr = bit, }
68 
69 #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p)				\
70 	{ .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
71 
72 extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
73 
74 #define init_waitqueue_head(q)				\
75 	do {						\
76 		static struct lock_class_key __key;	\
77 							\
78 		__init_waitqueue_head((q), #q, &__key);	\
79 	} while (0)
80 
81 #ifdef CONFIG_LOCKDEP
82 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
83 	({ init_waitqueue_head(&name); name; })
84 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
85 	wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
86 #else
87 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
88 #endif
89 
90 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
91 {
92 	q->flags	= 0;
93 	q->private	= p;
94 	q->func		= default_wake_function;
95 }
96 
97 static inline void
98 init_waitqueue_func_entry(wait_queue_t *q, wait_queue_func_t func)
99 {
100 	q->flags	= 0;
101 	q->private	= NULL;
102 	q->func		= func;
103 }
104 
105 static inline int waitqueue_active(wait_queue_head_t *q)
106 {
107 	return !list_empty(&q->task_list);
108 }
109 
110 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
111 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
112 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
113 
114 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
115 {
116 	list_add(&new->task_list, &head->task_list);
117 }
118 
119 /*
120  * Used for wake-one threads:
121  */
122 static inline void
123 __add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
124 {
125 	wait->flags |= WQ_FLAG_EXCLUSIVE;
126 	__add_wait_queue(q, wait);
127 }
128 
129 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
130 					 wait_queue_t *new)
131 {
132 	list_add_tail(&new->task_list, &head->task_list);
133 }
134 
135 static inline void
136 __add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
137 {
138 	wait->flags |= WQ_FLAG_EXCLUSIVE;
139 	__add_wait_queue_tail(q, wait);
140 }
141 
142 static inline void
143 __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
144 {
145 	list_del(&old->task_list);
146 }
147 
148 typedef int wait_bit_action_f(struct wait_bit_key *);
149 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
150 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
151 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
152 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
153 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
154 void __wake_up_bit(wait_queue_head_t *, void *, int);
155 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
156 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
157 void wake_up_bit(void *, int);
158 void wake_up_atomic_t(atomic_t *);
159 int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
160 int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
161 int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
162 int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
163 wait_queue_head_t *bit_waitqueue(void *, int);
164 
165 #define wake_up(x)			__wake_up(x, TASK_NORMAL, 1, NULL)
166 #define wake_up_nr(x, nr)		__wake_up(x, TASK_NORMAL, nr, NULL)
167 #define wake_up_all(x)			__wake_up(x, TASK_NORMAL, 0, NULL)
168 #define wake_up_locked(x)		__wake_up_locked((x), TASK_NORMAL, 1)
169 #define wake_up_all_locked(x)		__wake_up_locked((x), TASK_NORMAL, 0)
170 
171 #define wake_up_interruptible(x)	__wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
172 #define wake_up_interruptible_nr(x, nr)	__wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
173 #define wake_up_interruptible_all(x)	__wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
174 #define wake_up_interruptible_sync(x)	__wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
175 
176 /*
177  * Wakeup macros to be used to report events to the targets.
178  */
179 #define wake_up_poll(x, m)						\
180 	__wake_up(x, TASK_NORMAL, 1, (void *) (m))
181 #define wake_up_locked_poll(x, m)					\
182 	__wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
183 #define wake_up_interruptible_poll(x, m)				\
184 	__wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
185 #define wake_up_interruptible_sync_poll(x, m)				\
186 	__wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
187 
188 #define ___wait_cond_timeout(condition)					\
189 ({									\
190 	bool __cond = (condition);					\
191 	if (__cond && !__ret)						\
192 		__ret = 1;						\
193 	__cond || !__ret;						\
194 })
195 
196 #define ___wait_is_interruptible(state)					\
197 	(!__builtin_constant_p(state) ||				\
198 		state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE)	\
199 
200 /*
201  * The below macro ___wait_event() has an explicit shadow of the __ret
202  * variable when used from the wait_event_*() macros.
203  *
204  * This is so that both can use the ___wait_cond_timeout() construct
205  * to wrap the condition.
206  *
207  * The type inconsistency of the wait_event_*() __ret variable is also
208  * on purpose; we use long where we can return timeout values and int
209  * otherwise.
210  */
211 
212 #define ___wait_event(wq, condition, state, exclusive, ret, cmd)	\
213 ({									\
214 	__label__ __out;						\
215 	wait_queue_t __wait;						\
216 	long __ret = ret;	/* explicit shadow */			\
217 									\
218 	INIT_LIST_HEAD(&__wait.task_list);				\
219 	if (exclusive)							\
220 		__wait.flags = WQ_FLAG_EXCLUSIVE;			\
221 	else								\
222 		__wait.flags = 0;					\
223 									\
224 	for (;;) {							\
225 		long __int = prepare_to_wait_event(&wq, &__wait, state);\
226 									\
227 		if (condition)						\
228 			break;						\
229 									\
230 		if (___wait_is_interruptible(state) && __int) {		\
231 			__ret = __int;					\
232 			if (exclusive) {				\
233 				abort_exclusive_wait(&wq, &__wait,	\
234 						     state, NULL);	\
235 				goto __out;				\
236 			}						\
237 			break;						\
238 		}							\
239 									\
240 		cmd;							\
241 	}								\
242 	finish_wait(&wq, &__wait);					\
243 __out:	__ret;								\
244 })
245 
246 #define __wait_event(wq, condition)					\
247 	(void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
248 			    schedule())
249 
250 /**
251  * wait_event - sleep until a condition gets true
252  * @wq: the waitqueue to wait on
253  * @condition: a C expression for the event to wait for
254  *
255  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
256  * @condition evaluates to true. The @condition is checked each time
257  * the waitqueue @wq is woken up.
258  *
259  * wake_up() has to be called after changing any variable that could
260  * change the result of the wait condition.
261  */
262 #define wait_event(wq, condition)					\
263 do {									\
264 	might_sleep();							\
265 	if (condition)							\
266 		break;							\
267 	__wait_event(wq, condition);					\
268 } while (0)
269 
270 #define __wait_event_freezable(wq, condition)				\
271 	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0,		\
272 			    schedule(); try_to_freeze())
273 
274 /**
275  * wait_event - sleep (or freeze) until a condition gets true
276  * @wq: the waitqueue to wait on
277  * @condition: a C expression for the event to wait for
278  *
279  * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
280  * to system load) until the @condition evaluates to true. The
281  * @condition is checked each time the waitqueue @wq is woken up.
282  *
283  * wake_up() has to be called after changing any variable that could
284  * change the result of the wait condition.
285  */
286 #define wait_event_freezable(wq, condition)				\
287 ({									\
288 	int __ret = 0;							\
289 	might_sleep();							\
290 	if (!(condition))						\
291 		__ret = __wait_event_freezable(wq, condition);		\
292 	__ret;								\
293 })
294 
295 #define __wait_event_timeout(wq, condition, timeout)			\
296 	___wait_event(wq, ___wait_cond_timeout(condition),		\
297 		      TASK_UNINTERRUPTIBLE, 0, timeout,			\
298 		      __ret = schedule_timeout(__ret))
299 
300 /**
301  * wait_event_timeout - sleep until a condition gets true or a timeout elapses
302  * @wq: the waitqueue to wait on
303  * @condition: a C expression for the event to wait for
304  * @timeout: timeout, in jiffies
305  *
306  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
307  * @condition evaluates to true. The @condition is checked each time
308  * the waitqueue @wq is woken up.
309  *
310  * wake_up() has to be called after changing any variable that could
311  * change the result of the wait condition.
312  *
313  * Returns:
314  * 0 if the @condition evaluated to %false after the @timeout elapsed,
315  * 1 if the @condition evaluated to %true after the @timeout elapsed,
316  * or the remaining jiffies (at least 1) if the @condition evaluated
317  * to %true before the @timeout elapsed.
318  */
319 #define wait_event_timeout(wq, condition, timeout)			\
320 ({									\
321 	long __ret = timeout;						\
322 	might_sleep();							\
323 	if (!___wait_cond_timeout(condition))				\
324 		__ret = __wait_event_timeout(wq, condition, timeout);	\
325 	__ret;								\
326 })
327 
328 #define __wait_event_freezable_timeout(wq, condition, timeout)		\
329 	___wait_event(wq, ___wait_cond_timeout(condition),		\
330 		      TASK_INTERRUPTIBLE, 0, timeout,			\
331 		      __ret = schedule_timeout(__ret); try_to_freeze())
332 
333 /*
334  * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
335  * increasing load and is freezable.
336  */
337 #define wait_event_freezable_timeout(wq, condition, timeout)		\
338 ({									\
339 	long __ret = timeout;						\
340 	might_sleep();							\
341 	if (!___wait_cond_timeout(condition))				\
342 		__ret = __wait_event_freezable_timeout(wq, condition, timeout);	\
343 	__ret;								\
344 })
345 
346 #define __wait_event_cmd(wq, condition, cmd1, cmd2)			\
347 	(void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
348 			    cmd1; schedule(); cmd2)
349 
350 /**
351  * wait_event_cmd - sleep until a condition gets true
352  * @wq: the waitqueue to wait on
353  * @condition: a C expression for the event to wait for
354  * @cmd1: the command will be executed before sleep
355  * @cmd2: the command will be executed after sleep
356  *
357  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
358  * @condition evaluates to true. The @condition is checked each time
359  * the waitqueue @wq is woken up.
360  *
361  * wake_up() has to be called after changing any variable that could
362  * change the result of the wait condition.
363  */
364 #define wait_event_cmd(wq, condition, cmd1, cmd2)			\
365 do {									\
366 	might_sleep();							\
367 	if (condition)							\
368 		break;							\
369 	__wait_event_cmd(wq, condition, cmd1, cmd2);			\
370 } while (0)
371 
372 #define __wait_event_interruptible(wq, condition)			\
373 	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0,		\
374 		      schedule())
375 
376 /**
377  * wait_event_interruptible - sleep until a condition gets true
378  * @wq: the waitqueue to wait on
379  * @condition: a C expression for the event to wait for
380  *
381  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
382  * @condition evaluates to true or a signal is received.
383  * The @condition is checked each time the waitqueue @wq is woken up.
384  *
385  * wake_up() has to be called after changing any variable that could
386  * change the result of the wait condition.
387  *
388  * The function will return -ERESTARTSYS if it was interrupted by a
389  * signal and 0 if @condition evaluated to true.
390  */
391 #define wait_event_interruptible(wq, condition)				\
392 ({									\
393 	int __ret = 0;							\
394 	might_sleep();							\
395 	if (!(condition))						\
396 		__ret = __wait_event_interruptible(wq, condition);	\
397 	__ret;								\
398 })
399 
400 #define __wait_event_interruptible_timeout(wq, condition, timeout)	\
401 	___wait_event(wq, ___wait_cond_timeout(condition),		\
402 		      TASK_INTERRUPTIBLE, 0, timeout,			\
403 		      __ret = schedule_timeout(__ret))
404 
405 /**
406  * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
407  * @wq: the waitqueue to wait on
408  * @condition: a C expression for the event to wait for
409  * @timeout: timeout, in jiffies
410  *
411  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
412  * @condition evaluates to true or a signal is received.
413  * The @condition is checked each time the waitqueue @wq is woken up.
414  *
415  * wake_up() has to be called after changing any variable that could
416  * change the result of the wait condition.
417  *
418  * Returns:
419  * 0 if the @condition evaluated to %false after the @timeout elapsed,
420  * 1 if the @condition evaluated to %true after the @timeout elapsed,
421  * the remaining jiffies (at least 1) if the @condition evaluated
422  * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
423  * interrupted by a signal.
424  */
425 #define wait_event_interruptible_timeout(wq, condition, timeout)	\
426 ({									\
427 	long __ret = timeout;						\
428 	might_sleep();							\
429 	if (!___wait_cond_timeout(condition))				\
430 		__ret = __wait_event_interruptible_timeout(wq,		\
431 						condition, timeout);	\
432 	__ret;								\
433 })
434 
435 #define __wait_event_hrtimeout(wq, condition, timeout, state)		\
436 ({									\
437 	int __ret = 0;							\
438 	struct hrtimer_sleeper __t;					\
439 									\
440 	hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC,		\
441 			      HRTIMER_MODE_REL);			\
442 	hrtimer_init_sleeper(&__t, current);				\
443 	if ((timeout).tv64 != KTIME_MAX)				\
444 		hrtimer_start_range_ns(&__t.timer, timeout,		\
445 				       current->timer_slack_ns,		\
446 				       HRTIMER_MODE_REL);		\
447 									\
448 	__ret = ___wait_event(wq, condition, state, 0, 0,		\
449 		if (!__t.task) {					\
450 			__ret = -ETIME;					\
451 			break;						\
452 		}							\
453 		schedule());						\
454 									\
455 	hrtimer_cancel(&__t.timer);					\
456 	destroy_hrtimer_on_stack(&__t.timer);				\
457 	__ret;								\
458 })
459 
460 /**
461  * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
462  * @wq: the waitqueue to wait on
463  * @condition: a C expression for the event to wait for
464  * @timeout: timeout, as a ktime_t
465  *
466  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
467  * @condition evaluates to true or a signal is received.
468  * The @condition is checked each time the waitqueue @wq is woken up.
469  *
470  * wake_up() has to be called after changing any variable that could
471  * change the result of the wait condition.
472  *
473  * The function returns 0 if @condition became true, or -ETIME if the timeout
474  * elapsed.
475  */
476 #define wait_event_hrtimeout(wq, condition, timeout)			\
477 ({									\
478 	int __ret = 0;							\
479 	might_sleep();							\
480 	if (!(condition))						\
481 		__ret = __wait_event_hrtimeout(wq, condition, timeout,	\
482 					       TASK_UNINTERRUPTIBLE);	\
483 	__ret;								\
484 })
485 
486 /**
487  * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
488  * @wq: the waitqueue to wait on
489  * @condition: a C expression for the event to wait for
490  * @timeout: timeout, as a ktime_t
491  *
492  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
493  * @condition evaluates to true or a signal is received.
494  * The @condition is checked each time the waitqueue @wq is woken up.
495  *
496  * wake_up() has to be called after changing any variable that could
497  * change the result of the wait condition.
498  *
499  * The function returns 0 if @condition became true, -ERESTARTSYS if it was
500  * interrupted by a signal, or -ETIME if the timeout elapsed.
501  */
502 #define wait_event_interruptible_hrtimeout(wq, condition, timeout)	\
503 ({									\
504 	long __ret = 0;							\
505 	might_sleep();							\
506 	if (!(condition))						\
507 		__ret = __wait_event_hrtimeout(wq, condition, timeout,	\
508 					       TASK_INTERRUPTIBLE);	\
509 	__ret;								\
510 })
511 
512 #define __wait_event_interruptible_exclusive(wq, condition)		\
513 	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,		\
514 		      schedule())
515 
516 #define wait_event_interruptible_exclusive(wq, condition)		\
517 ({									\
518 	int __ret = 0;							\
519 	might_sleep();							\
520 	if (!(condition))						\
521 		__ret = __wait_event_interruptible_exclusive(wq, condition);\
522 	__ret;								\
523 })
524 
525 
526 #define __wait_event_freezable_exclusive(wq, condition)			\
527 	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,		\
528 			schedule(); try_to_freeze())
529 
530 #define wait_event_freezable_exclusive(wq, condition)			\
531 ({									\
532 	int __ret = 0;							\
533 	might_sleep();							\
534 	if (!(condition))						\
535 		__ret = __wait_event_freezable_exclusive(wq, condition);\
536 	__ret;								\
537 })
538 
539 
540 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
541 ({									\
542 	int __ret = 0;							\
543 	DEFINE_WAIT(__wait);						\
544 	if (exclusive)							\
545 		__wait.flags |= WQ_FLAG_EXCLUSIVE;			\
546 	do {								\
547 		if (likely(list_empty(&__wait.task_list)))		\
548 			__add_wait_queue_tail(&(wq), &__wait);		\
549 		set_current_state(TASK_INTERRUPTIBLE);			\
550 		if (signal_pending(current)) {				\
551 			__ret = -ERESTARTSYS;				\
552 			break;						\
553 		}							\
554 		if (irq)						\
555 			spin_unlock_irq(&(wq).lock);			\
556 		else							\
557 			spin_unlock(&(wq).lock);			\
558 		schedule();						\
559 		if (irq)						\
560 			spin_lock_irq(&(wq).lock);			\
561 		else							\
562 			spin_lock(&(wq).lock);				\
563 	} while (!(condition));						\
564 	__remove_wait_queue(&(wq), &__wait);				\
565 	__set_current_state(TASK_RUNNING);				\
566 	__ret;								\
567 })
568 
569 
570 /**
571  * wait_event_interruptible_locked - sleep until a condition gets true
572  * @wq: the waitqueue to wait on
573  * @condition: a C expression for the event to wait for
574  *
575  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
576  * @condition evaluates to true or a signal is received.
577  * The @condition is checked each time the waitqueue @wq is woken up.
578  *
579  * It must be called with wq.lock being held.  This spinlock is
580  * unlocked while sleeping but @condition testing is done while lock
581  * is held and when this macro exits the lock is held.
582  *
583  * The lock is locked/unlocked using spin_lock()/spin_unlock()
584  * functions which must match the way they are locked/unlocked outside
585  * of this macro.
586  *
587  * wake_up_locked() has to be called after changing any variable that could
588  * change the result of the wait condition.
589  *
590  * The function will return -ERESTARTSYS if it was interrupted by a
591  * signal and 0 if @condition evaluated to true.
592  */
593 #define wait_event_interruptible_locked(wq, condition)			\
594 	((condition)							\
595 	 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
596 
597 /**
598  * wait_event_interruptible_locked_irq - sleep until a condition gets true
599  * @wq: the waitqueue to wait on
600  * @condition: a C expression for the event to wait for
601  *
602  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
603  * @condition evaluates to true or a signal is received.
604  * The @condition is checked each time the waitqueue @wq is woken up.
605  *
606  * It must be called with wq.lock being held.  This spinlock is
607  * unlocked while sleeping but @condition testing is done while lock
608  * is held and when this macro exits the lock is held.
609  *
610  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
611  * functions which must match the way they are locked/unlocked outside
612  * of this macro.
613  *
614  * wake_up_locked() has to be called after changing any variable that could
615  * change the result of the wait condition.
616  *
617  * The function will return -ERESTARTSYS if it was interrupted by a
618  * signal and 0 if @condition evaluated to true.
619  */
620 #define wait_event_interruptible_locked_irq(wq, condition)		\
621 	((condition)							\
622 	 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
623 
624 /**
625  * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
626  * @wq: the waitqueue to wait on
627  * @condition: a C expression for the event to wait for
628  *
629  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
630  * @condition evaluates to true or a signal is received.
631  * The @condition is checked each time the waitqueue @wq is woken up.
632  *
633  * It must be called with wq.lock being held.  This spinlock is
634  * unlocked while sleeping but @condition testing is done while lock
635  * is held and when this macro exits the lock is held.
636  *
637  * The lock is locked/unlocked using spin_lock()/spin_unlock()
638  * functions which must match the way they are locked/unlocked outside
639  * of this macro.
640  *
641  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
642  * set thus when other process waits process on the list if this
643  * process is awaken further processes are not considered.
644  *
645  * wake_up_locked() has to be called after changing any variable that could
646  * change the result of the wait condition.
647  *
648  * The function will return -ERESTARTSYS if it was interrupted by a
649  * signal and 0 if @condition evaluated to true.
650  */
651 #define wait_event_interruptible_exclusive_locked(wq, condition)	\
652 	((condition)							\
653 	 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
654 
655 /**
656  * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
657  * @wq: the waitqueue to wait on
658  * @condition: a C expression for the event to wait for
659  *
660  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
661  * @condition evaluates to true or a signal is received.
662  * The @condition is checked each time the waitqueue @wq is woken up.
663  *
664  * It must be called with wq.lock being held.  This spinlock is
665  * unlocked while sleeping but @condition testing is done while lock
666  * is held and when this macro exits the lock is held.
667  *
668  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
669  * functions which must match the way they are locked/unlocked outside
670  * of this macro.
671  *
672  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
673  * set thus when other process waits process on the list if this
674  * process is awaken further processes are not considered.
675  *
676  * wake_up_locked() has to be called after changing any variable that could
677  * change the result of the wait condition.
678  *
679  * The function will return -ERESTARTSYS if it was interrupted by a
680  * signal and 0 if @condition evaluated to true.
681  */
682 #define wait_event_interruptible_exclusive_locked_irq(wq, condition)	\
683 	((condition)							\
684 	 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
685 
686 
687 #define __wait_event_killable(wq, condition)				\
688 	___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
689 
690 /**
691  * wait_event_killable - sleep until a condition gets true
692  * @wq: the waitqueue to wait on
693  * @condition: a C expression for the event to wait for
694  *
695  * The process is put to sleep (TASK_KILLABLE) until the
696  * @condition evaluates to true or a signal is received.
697  * The @condition is checked each time the waitqueue @wq is woken up.
698  *
699  * wake_up() has to be called after changing any variable that could
700  * change the result of the wait condition.
701  *
702  * The function will return -ERESTARTSYS if it was interrupted by a
703  * signal and 0 if @condition evaluated to true.
704  */
705 #define wait_event_killable(wq, condition)				\
706 ({									\
707 	int __ret = 0;							\
708 	might_sleep();							\
709 	if (!(condition))						\
710 		__ret = __wait_event_killable(wq, condition);		\
711 	__ret;								\
712 })
713 
714 
715 #define __wait_event_lock_irq(wq, condition, lock, cmd)			\
716 	(void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
717 			    spin_unlock_irq(&lock);			\
718 			    cmd;					\
719 			    schedule();					\
720 			    spin_lock_irq(&lock))
721 
722 /**
723  * wait_event_lock_irq_cmd - sleep until a condition gets true. The
724  *			     condition is checked under the lock. This
725  *			     is expected to be called with the lock
726  *			     taken.
727  * @wq: the waitqueue to wait on
728  * @condition: a C expression for the event to wait for
729  * @lock: a locked spinlock_t, which will be released before cmd
730  *	  and schedule() and reacquired afterwards.
731  * @cmd: a command which is invoked outside the critical section before
732  *	 sleep
733  *
734  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
735  * @condition evaluates to true. The @condition is checked each time
736  * the waitqueue @wq is woken up.
737  *
738  * wake_up() has to be called after changing any variable that could
739  * change the result of the wait condition.
740  *
741  * This is supposed to be called while holding the lock. The lock is
742  * dropped before invoking the cmd and going to sleep and is reacquired
743  * afterwards.
744  */
745 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd)		\
746 do {									\
747 	if (condition)							\
748 		break;							\
749 	__wait_event_lock_irq(wq, condition, lock, cmd);		\
750 } while (0)
751 
752 /**
753  * wait_event_lock_irq - sleep until a condition gets true. The
754  *			 condition is checked under the lock. This
755  *			 is expected to be called with the lock
756  *			 taken.
757  * @wq: the waitqueue to wait on
758  * @condition: a C expression for the event to wait for
759  * @lock: a locked spinlock_t, which will be released before schedule()
760  *	  and reacquired afterwards.
761  *
762  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
763  * @condition evaluates to true. The @condition is checked each time
764  * the waitqueue @wq is woken up.
765  *
766  * wake_up() has to be called after changing any variable that could
767  * change the result of the wait condition.
768  *
769  * This is supposed to be called while holding the lock. The lock is
770  * dropped before going to sleep and is reacquired afterwards.
771  */
772 #define wait_event_lock_irq(wq, condition, lock)			\
773 do {									\
774 	if (condition)							\
775 		break;							\
776 	__wait_event_lock_irq(wq, condition, lock, );			\
777 } while (0)
778 
779 
780 #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd)	\
781 	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0,		\
782 		      spin_unlock_irq(&lock);				\
783 		      cmd;						\
784 		      schedule();					\
785 		      spin_lock_irq(&lock))
786 
787 /**
788  * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
789  *		The condition is checked under the lock. This is expected to
790  *		be called with the lock taken.
791  * @wq: the waitqueue to wait on
792  * @condition: a C expression for the event to wait for
793  * @lock: a locked spinlock_t, which will be released before cmd and
794  *	  schedule() and reacquired afterwards.
795  * @cmd: a command which is invoked outside the critical section before
796  *	 sleep
797  *
798  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
799  * @condition evaluates to true or a signal is received. The @condition is
800  * checked each time the waitqueue @wq is woken up.
801  *
802  * wake_up() has to be called after changing any variable that could
803  * change the result of the wait condition.
804  *
805  * This is supposed to be called while holding the lock. The lock is
806  * dropped before invoking the cmd and going to sleep and is reacquired
807  * afterwards.
808  *
809  * The macro will return -ERESTARTSYS if it was interrupted by a signal
810  * and 0 if @condition evaluated to true.
811  */
812 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd)	\
813 ({									\
814 	int __ret = 0;							\
815 	if (!(condition))						\
816 		__ret = __wait_event_interruptible_lock_irq(wq,		\
817 						condition, lock, cmd);	\
818 	__ret;								\
819 })
820 
821 /**
822  * wait_event_interruptible_lock_irq - sleep until a condition gets true.
823  *		The condition is checked under the lock. This is expected
824  *		to be called with the lock taken.
825  * @wq: the waitqueue to wait on
826  * @condition: a C expression for the event to wait for
827  * @lock: a locked spinlock_t, which will be released before schedule()
828  *	  and reacquired afterwards.
829  *
830  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
831  * @condition evaluates to true or signal is received. The @condition is
832  * checked each time the waitqueue @wq is woken up.
833  *
834  * wake_up() has to be called after changing any variable that could
835  * change the result of the wait condition.
836  *
837  * This is supposed to be called while holding the lock. The lock is
838  * dropped before going to sleep and is reacquired afterwards.
839  *
840  * The macro will return -ERESTARTSYS if it was interrupted by a signal
841  * and 0 if @condition evaluated to true.
842  */
843 #define wait_event_interruptible_lock_irq(wq, condition, lock)		\
844 ({									\
845 	int __ret = 0;							\
846 	if (!(condition))						\
847 		__ret = __wait_event_interruptible_lock_irq(wq,		\
848 						condition, lock,);	\
849 	__ret;								\
850 })
851 
852 #define __wait_event_interruptible_lock_irq_timeout(wq, condition,	\
853 						    lock, timeout)	\
854 	___wait_event(wq, ___wait_cond_timeout(condition),		\
855 		      TASK_INTERRUPTIBLE, 0, timeout,			\
856 		      spin_unlock_irq(&lock);				\
857 		      __ret = schedule_timeout(__ret);			\
858 		      spin_lock_irq(&lock));
859 
860 /**
861  * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
862  *		true or a timeout elapses. The condition is checked under
863  *		the lock. This is expected to be called with the lock taken.
864  * @wq: the waitqueue to wait on
865  * @condition: a C expression for the event to wait for
866  * @lock: a locked spinlock_t, which will be released before schedule()
867  *	  and reacquired afterwards.
868  * @timeout: timeout, in jiffies
869  *
870  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
871  * @condition evaluates to true or signal is received. The @condition is
872  * checked each time the waitqueue @wq is woken up.
873  *
874  * wake_up() has to be called after changing any variable that could
875  * change the result of the wait condition.
876  *
877  * This is supposed to be called while holding the lock. The lock is
878  * dropped before going to sleep and is reacquired afterwards.
879  *
880  * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
881  * was interrupted by a signal, and the remaining jiffies otherwise
882  * if the condition evaluated to true before the timeout elapsed.
883  */
884 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock,	\
885 						  timeout)		\
886 ({									\
887 	long __ret = timeout;						\
888 	if (!___wait_cond_timeout(condition))				\
889 		__ret = __wait_event_interruptible_lock_irq_timeout(	\
890 					wq, condition, lock, timeout);	\
891 	__ret;								\
892 })
893 
894 /*
895  * Waitqueues which are removed from the waitqueue_head at wakeup time
896  */
897 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
898 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
899 long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
900 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
901 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
902 long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
903 int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
904 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
905 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
906 
907 #define DEFINE_WAIT_FUNC(name, function)				\
908 	wait_queue_t name = {						\
909 		.private	= current,				\
910 		.func		= function,				\
911 		.task_list	= LIST_HEAD_INIT((name).task_list),	\
912 	}
913 
914 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
915 
916 #define DEFINE_WAIT_BIT(name, word, bit)				\
917 	struct wait_bit_queue name = {					\
918 		.key = __WAIT_BIT_KEY_INITIALIZER(word, bit),		\
919 		.wait	= {						\
920 			.private	= current,			\
921 			.func		= wake_bit_function,		\
922 			.task_list	=				\
923 				LIST_HEAD_INIT((name).wait.task_list),	\
924 		},							\
925 	}
926 
927 #define init_wait(wait)							\
928 	do {								\
929 		(wait)->private = current;				\
930 		(wait)->func = autoremove_wake_function;		\
931 		INIT_LIST_HEAD(&(wait)->task_list);			\
932 		(wait)->flags = 0;					\
933 	} while (0)
934 
935 
936 extern int bit_wait(struct wait_bit_key *);
937 extern int bit_wait_io(struct wait_bit_key *);
938 extern int bit_wait_timeout(struct wait_bit_key *);
939 extern int bit_wait_io_timeout(struct wait_bit_key *);
940 
941 /**
942  * wait_on_bit - wait for a bit to be cleared
943  * @word: the word being waited on, a kernel virtual address
944  * @bit: the bit of the word being waited on
945  * @mode: the task state to sleep in
946  *
947  * There is a standard hashed waitqueue table for generic use. This
948  * is the part of the hashtable's accessor API that waits on a bit.
949  * For instance, if one were to have waiters on a bitflag, one would
950  * call wait_on_bit() in threads waiting for the bit to clear.
951  * One uses wait_on_bit() where one is waiting for the bit to clear,
952  * but has no intention of setting it.
953  * Returned value will be zero if the bit was cleared, or non-zero
954  * if the process received a signal and the mode permitted wakeup
955  * on that signal.
956  */
957 static inline int
958 wait_on_bit(void *word, int bit, unsigned mode)
959 {
960 	might_sleep();
961 	if (!test_bit(bit, word))
962 		return 0;
963 	return out_of_line_wait_on_bit(word, bit,
964 				       bit_wait,
965 				       mode);
966 }
967 
968 /**
969  * wait_on_bit_io - wait for a bit to be cleared
970  * @word: the word being waited on, a kernel virtual address
971  * @bit: the bit of the word being waited on
972  * @mode: the task state to sleep in
973  *
974  * Use the standard hashed waitqueue table to wait for a bit
975  * to be cleared.  This is similar to wait_on_bit(), but calls
976  * io_schedule() instead of schedule() for the actual waiting.
977  *
978  * Returned value will be zero if the bit was cleared, or non-zero
979  * if the process received a signal and the mode permitted wakeup
980  * on that signal.
981  */
982 static inline int
983 wait_on_bit_io(void *word, int bit, unsigned mode)
984 {
985 	might_sleep();
986 	if (!test_bit(bit, word))
987 		return 0;
988 	return out_of_line_wait_on_bit(word, bit,
989 				       bit_wait_io,
990 				       mode);
991 }
992 
993 /**
994  * wait_on_bit_action - wait for a bit to be cleared
995  * @word: the word being waited on, a kernel virtual address
996  * @bit: the bit of the word being waited on
997  * @action: the function used to sleep, which may take special actions
998  * @mode: the task state to sleep in
999  *
1000  * Use the standard hashed waitqueue table to wait for a bit
1001  * to be cleared, and allow the waiting action to be specified.
1002  * This is like wait_on_bit() but allows fine control of how the waiting
1003  * is done.
1004  *
1005  * Returned value will be zero if the bit was cleared, or non-zero
1006  * if the process received a signal and the mode permitted wakeup
1007  * on that signal.
1008  */
1009 static inline int
1010 wait_on_bit_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
1011 {
1012 	might_sleep();
1013 	if (!test_bit(bit, word))
1014 		return 0;
1015 	return out_of_line_wait_on_bit(word, bit, action, mode);
1016 }
1017 
1018 /**
1019  * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1020  * @word: the word being waited on, a kernel virtual address
1021  * @bit: the bit of the word being waited on
1022  * @mode: the task state to sleep in
1023  *
1024  * There is a standard hashed waitqueue table for generic use. This
1025  * is the part of the hashtable's accessor API that waits on a bit
1026  * when one intends to set it, for instance, trying to lock bitflags.
1027  * For instance, if one were to have waiters trying to set bitflag
1028  * and waiting for it to clear before setting it, one would call
1029  * wait_on_bit() in threads waiting to be able to set the bit.
1030  * One uses wait_on_bit_lock() where one is waiting for the bit to
1031  * clear with the intention of setting it, and when done, clearing it.
1032  *
1033  * Returns zero if the bit was (eventually) found to be clear and was
1034  * set.  Returns non-zero if a signal was delivered to the process and
1035  * the @mode allows that signal to wake the process.
1036  */
1037 static inline int
1038 wait_on_bit_lock(void *word, int bit, unsigned mode)
1039 {
1040 	might_sleep();
1041 	if (!test_and_set_bit(bit, word))
1042 		return 0;
1043 	return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1044 }
1045 
1046 /**
1047  * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1048  * @word: the word being waited on, a kernel virtual address
1049  * @bit: the bit of the word being waited on
1050  * @mode: the task state to sleep in
1051  *
1052  * Use the standard hashed waitqueue table to wait for a bit
1053  * to be cleared and then to atomically set it.  This is similar
1054  * to wait_on_bit(), but calls io_schedule() instead of schedule()
1055  * for the actual waiting.
1056  *
1057  * Returns zero if the bit was (eventually) found to be clear and was
1058  * set.  Returns non-zero if a signal was delivered to the process and
1059  * the @mode allows that signal to wake the process.
1060  */
1061 static inline int
1062 wait_on_bit_lock_io(void *word, int bit, unsigned mode)
1063 {
1064 	might_sleep();
1065 	if (!test_and_set_bit(bit, word))
1066 		return 0;
1067 	return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
1068 }
1069 
1070 /**
1071  * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1072  * @word: the word being waited on, a kernel virtual address
1073  * @bit: the bit of the word being waited on
1074  * @action: the function used to sleep, which may take special actions
1075  * @mode: the task state to sleep in
1076  *
1077  * Use the standard hashed waitqueue table to wait for a bit
1078  * to be cleared and then to set it, and allow the waiting action
1079  * to be specified.
1080  * This is like wait_on_bit() but allows fine control of how the waiting
1081  * is done.
1082  *
1083  * Returns zero if the bit was (eventually) found to be clear and was
1084  * set.  Returns non-zero if a signal was delivered to the process and
1085  * the @mode allows that signal to wake the process.
1086  */
1087 static inline int
1088 wait_on_bit_lock_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
1089 {
1090 	might_sleep();
1091 	if (!test_and_set_bit(bit, word))
1092 		return 0;
1093 	return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1094 }
1095 
1096 /**
1097  * wait_on_atomic_t - Wait for an atomic_t to become 0
1098  * @val: The atomic value being waited on, a kernel virtual address
1099  * @action: the function used to sleep, which may take special actions
1100  * @mode: the task state to sleep in
1101  *
1102  * Wait for an atomic_t to become 0.  We abuse the bit-wait waitqueue table for
1103  * the purpose of getting a waitqueue, but we set the key to a bit number
1104  * outside of the target 'word'.
1105  */
1106 static inline
1107 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1108 {
1109 	might_sleep();
1110 	if (atomic_read(val) == 0)
1111 		return 0;
1112 	return out_of_line_wait_on_atomic_t(val, action, mode);
1113 }
1114 
1115 #endif /* _LINUX_WAIT_H */
1116