162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _LINUX_WAIT_H
362306a36Sopenharmony_ci#define _LINUX_WAIT_H
462306a36Sopenharmony_ci/*
562306a36Sopenharmony_ci * Linux wait queue related types and methods
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci#include <linux/list.h>
862306a36Sopenharmony_ci#include <linux/stddef.h>
962306a36Sopenharmony_ci#include <linux/spinlock.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <asm/current.h>
1262306a36Sopenharmony_ci#include <uapi/linux/wait.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_citypedef struct wait_queue_entry wait_queue_entry_t;
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_citypedef int (*wait_queue_func_t)(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
1762306a36Sopenharmony_ciint default_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/* wait_queue_entry::flags */
2062306a36Sopenharmony_ci#define WQ_FLAG_EXCLUSIVE	0x01
2162306a36Sopenharmony_ci#define WQ_FLAG_WOKEN		0x02
2262306a36Sopenharmony_ci#define WQ_FLAG_BOOKMARK	0x04
2362306a36Sopenharmony_ci#define WQ_FLAG_CUSTOM		0x08
2462306a36Sopenharmony_ci#define WQ_FLAG_DONE		0x10
2562306a36Sopenharmony_ci#define WQ_FLAG_PRIORITY	0x20
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/*
2862306a36Sopenharmony_ci * A single wait-queue entry structure:
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_cistruct wait_queue_entry {
3162306a36Sopenharmony_ci	unsigned int		flags;
3262306a36Sopenharmony_ci	void			*private;
3362306a36Sopenharmony_ci	wait_queue_func_t	func;
3462306a36Sopenharmony_ci	struct list_head	entry;
3562306a36Sopenharmony_ci};
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistruct wait_queue_head {
3862306a36Sopenharmony_ci	spinlock_t		lock;
3962306a36Sopenharmony_ci	struct list_head	head;
4062306a36Sopenharmony_ci};
4162306a36Sopenharmony_citypedef struct wait_queue_head wait_queue_head_t;
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistruct task_struct;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/*
4662306a36Sopenharmony_ci * Macros for declaration and initialisaton of the datatypes
4762306a36Sopenharmony_ci */
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#define __WAITQUEUE_INITIALIZER(name, tsk) {					\
5062306a36Sopenharmony_ci	.private	= tsk,							\
5162306a36Sopenharmony_ci	.func		= default_wake_function,				\
5262306a36Sopenharmony_ci	.entry		= { NULL, NULL } }
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#define DECLARE_WAITQUEUE(name, tsk)						\
5562306a36Sopenharmony_ci	struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci#define __WAIT_QUEUE_HEAD_INITIALIZER(name) {					\
5862306a36Sopenharmony_ci	.lock		= __SPIN_LOCK_UNLOCKED(name.lock),			\
5962306a36Sopenharmony_ci	.head		= LIST_HEAD_INIT(name.head) }
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define DECLARE_WAIT_QUEUE_HEAD(name) \
6262306a36Sopenharmony_ci	struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ciextern void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#define init_waitqueue_head(wq_head)						\
6762306a36Sopenharmony_ci	do {									\
6862306a36Sopenharmony_ci		static struct lock_class_key __key;				\
6962306a36Sopenharmony_ci										\
7062306a36Sopenharmony_ci		__init_waitqueue_head((wq_head), #wq_head, &__key);		\
7162306a36Sopenharmony_ci	} while (0)
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci#ifdef CONFIG_LOCKDEP
7462306a36Sopenharmony_ci# define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
7562306a36Sopenharmony_ci	({ init_waitqueue_head(&name); name; })
7662306a36Sopenharmony_ci# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
7762306a36Sopenharmony_ci	struct wait_queue_head name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
7862306a36Sopenharmony_ci#else
7962306a36Sopenharmony_ci# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
8062306a36Sopenharmony_ci#endif
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic inline void init_waitqueue_entry(struct wait_queue_entry *wq_entry, struct task_struct *p)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	wq_entry->flags		= 0;
8562306a36Sopenharmony_ci	wq_entry->private	= p;
8662306a36Sopenharmony_ci	wq_entry->func		= default_wake_function;
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic inline void
9062306a36Sopenharmony_ciinit_waitqueue_func_entry(struct wait_queue_entry *wq_entry, wait_queue_func_t func)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	wq_entry->flags		= 0;
9362306a36Sopenharmony_ci	wq_entry->private	= NULL;
9462306a36Sopenharmony_ci	wq_entry->func		= func;
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci/**
9862306a36Sopenharmony_ci * waitqueue_active -- locklessly test for waiters on the queue
9962306a36Sopenharmony_ci * @wq_head: the waitqueue to test for waiters
10062306a36Sopenharmony_ci *
10162306a36Sopenharmony_ci * returns true if the wait list is not empty
10262306a36Sopenharmony_ci *
10362306a36Sopenharmony_ci * NOTE: this function is lockless and requires care, incorrect usage _will_
10462306a36Sopenharmony_ci * lead to sporadic and non-obvious failure.
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci * Use either while holding wait_queue_head::lock or when used for wakeups
10762306a36Sopenharmony_ci * with an extra smp_mb() like::
10862306a36Sopenharmony_ci *
10962306a36Sopenharmony_ci *      CPU0 - waker                    CPU1 - waiter
11062306a36Sopenharmony_ci *
11162306a36Sopenharmony_ci *                                      for (;;) {
11262306a36Sopenharmony_ci *      @cond = true;                     prepare_to_wait(&wq_head, &wait, state);
11362306a36Sopenharmony_ci *      smp_mb();                         // smp_mb() from set_current_state()
11462306a36Sopenharmony_ci *      if (waitqueue_active(wq_head))         if (@cond)
11562306a36Sopenharmony_ci *        wake_up(wq_head);                      break;
11662306a36Sopenharmony_ci *                                        schedule();
11762306a36Sopenharmony_ci *                                      }
11862306a36Sopenharmony_ci *                                      finish_wait(&wq_head, &wait);
11962306a36Sopenharmony_ci *
12062306a36Sopenharmony_ci * Because without the explicit smp_mb() it's possible for the
12162306a36Sopenharmony_ci * waitqueue_active() load to get hoisted over the @cond store such that we'll
12262306a36Sopenharmony_ci * observe an empty wait list while the waiter might not observe @cond.
12362306a36Sopenharmony_ci *
12462306a36Sopenharmony_ci * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
12562306a36Sopenharmony_ci * which (when the lock is uncontended) are of roughly equal cost.
12662306a36Sopenharmony_ci */
12762306a36Sopenharmony_cistatic inline int waitqueue_active(struct wait_queue_head *wq_head)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	return !list_empty(&wq_head->head);
13062306a36Sopenharmony_ci}
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci/**
13362306a36Sopenharmony_ci * wq_has_single_sleeper - check if there is only one sleeper
13462306a36Sopenharmony_ci * @wq_head: wait queue head
13562306a36Sopenharmony_ci *
13662306a36Sopenharmony_ci * Returns true of wq_head has only one sleeper on the list.
13762306a36Sopenharmony_ci *
13862306a36Sopenharmony_ci * Please refer to the comment for waitqueue_active.
13962306a36Sopenharmony_ci */
14062306a36Sopenharmony_cistatic inline bool wq_has_single_sleeper(struct wait_queue_head *wq_head)
14162306a36Sopenharmony_ci{
14262306a36Sopenharmony_ci	return list_is_singular(&wq_head->head);
14362306a36Sopenharmony_ci}
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci/**
14662306a36Sopenharmony_ci * wq_has_sleeper - check if there are any waiting processes
14762306a36Sopenharmony_ci * @wq_head: wait queue head
14862306a36Sopenharmony_ci *
14962306a36Sopenharmony_ci * Returns true if wq_head has waiting processes
15062306a36Sopenharmony_ci *
15162306a36Sopenharmony_ci * Please refer to the comment for waitqueue_active.
15262306a36Sopenharmony_ci */
15362306a36Sopenharmony_cistatic inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
15462306a36Sopenharmony_ci{
15562306a36Sopenharmony_ci	/*
15662306a36Sopenharmony_ci	 * We need to be sure we are in sync with the
15762306a36Sopenharmony_ci	 * add_wait_queue modifications to the wait queue.
15862306a36Sopenharmony_ci	 *
15962306a36Sopenharmony_ci	 * This memory barrier should be paired with one on the
16062306a36Sopenharmony_ci	 * waiting side.
16162306a36Sopenharmony_ci	 */
16262306a36Sopenharmony_ci	smp_mb();
16362306a36Sopenharmony_ci	return waitqueue_active(wq_head);
16462306a36Sopenharmony_ci}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ciextern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
16762306a36Sopenharmony_ciextern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
16862306a36Sopenharmony_ciextern void add_wait_queue_priority(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
16962306a36Sopenharmony_ciextern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_cistatic inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
17262306a36Sopenharmony_ci{
17362306a36Sopenharmony_ci	struct list_head *head = &wq_head->head;
17462306a36Sopenharmony_ci	struct wait_queue_entry *wq;
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	list_for_each_entry(wq, &wq_head->head, entry) {
17762306a36Sopenharmony_ci		if (!(wq->flags & WQ_FLAG_PRIORITY))
17862306a36Sopenharmony_ci			break;
17962306a36Sopenharmony_ci		head = &wq->entry;
18062306a36Sopenharmony_ci	}
18162306a36Sopenharmony_ci	list_add(&wq_entry->entry, head);
18262306a36Sopenharmony_ci}
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci/*
18562306a36Sopenharmony_ci * Used for wake-one threads:
18662306a36Sopenharmony_ci */
18762306a36Sopenharmony_cistatic inline void
18862306a36Sopenharmony_ci__add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
18962306a36Sopenharmony_ci{
19062306a36Sopenharmony_ci	wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
19162306a36Sopenharmony_ci	__add_wait_queue(wq_head, wq_entry);
19262306a36Sopenharmony_ci}
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_cistatic inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
19562306a36Sopenharmony_ci{
19662306a36Sopenharmony_ci	list_add_tail(&wq_entry->entry, &wq_head->head);
19762306a36Sopenharmony_ci}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_cistatic inline void
20062306a36Sopenharmony_ci__add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
20162306a36Sopenharmony_ci{
20262306a36Sopenharmony_ci	wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
20362306a36Sopenharmony_ci	__add_wait_queue_entry_tail(wq_head, wq_entry);
20462306a36Sopenharmony_ci}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cistatic inline void
20762306a36Sopenharmony_ci__remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
20862306a36Sopenharmony_ci{
20962306a36Sopenharmony_ci	list_del(&wq_entry->entry);
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ciint __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
21362306a36Sopenharmony_civoid __wake_up_on_current_cpu(struct wait_queue_head *wq_head, unsigned int mode, void *key);
21462306a36Sopenharmony_civoid __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
21562306a36Sopenharmony_civoid __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
21662306a36Sopenharmony_ci		unsigned int mode, void *key, wait_queue_entry_t *bookmark);
21762306a36Sopenharmony_civoid __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
21862306a36Sopenharmony_civoid __wake_up_locked_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
21962306a36Sopenharmony_civoid __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
22062306a36Sopenharmony_civoid __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode);
22162306a36Sopenharmony_civoid __wake_up_pollfree(struct wait_queue_head *wq_head);
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci#define wake_up(x)			__wake_up(x, TASK_NORMAL, 1, NULL)
22462306a36Sopenharmony_ci#define wake_up_nr(x, nr)		__wake_up(x, TASK_NORMAL, nr, NULL)
22562306a36Sopenharmony_ci#define wake_up_all(x)			__wake_up(x, TASK_NORMAL, 0, NULL)
22662306a36Sopenharmony_ci#define wake_up_locked(x)		__wake_up_locked((x), TASK_NORMAL, 1)
22762306a36Sopenharmony_ci#define wake_up_all_locked(x)		__wake_up_locked((x), TASK_NORMAL, 0)
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci#define wake_up_interruptible(x)	__wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
23062306a36Sopenharmony_ci#define wake_up_interruptible_nr(x, nr)	__wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
23162306a36Sopenharmony_ci#define wake_up_interruptible_all(x)	__wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
23262306a36Sopenharmony_ci#define wake_up_interruptible_sync(x)	__wake_up_sync((x), TASK_INTERRUPTIBLE)
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci/*
23562306a36Sopenharmony_ci * Wakeup macros to be used to report events to the targets.
23662306a36Sopenharmony_ci */
23762306a36Sopenharmony_ci#define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
23862306a36Sopenharmony_ci#define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
23962306a36Sopenharmony_ci#define wake_up_poll(x, m)							\
24062306a36Sopenharmony_ci	__wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
24162306a36Sopenharmony_ci#define wake_up_poll_on_current_cpu(x, m)					\
24262306a36Sopenharmony_ci	__wake_up_on_current_cpu(x, TASK_NORMAL, poll_to_key(m))
24362306a36Sopenharmony_ci#define wake_up_locked_poll(x, m)						\
24462306a36Sopenharmony_ci	__wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
24562306a36Sopenharmony_ci#define wake_up_interruptible_poll(x, m)					\
24662306a36Sopenharmony_ci	__wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
24762306a36Sopenharmony_ci#define wake_up_interruptible_sync_poll(x, m)					\
24862306a36Sopenharmony_ci	__wake_up_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
24962306a36Sopenharmony_ci#define wake_up_interruptible_sync_poll_locked(x, m)				\
25062306a36Sopenharmony_ci	__wake_up_locked_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci/**
25362306a36Sopenharmony_ci * wake_up_pollfree - signal that a polled waitqueue is going away
25462306a36Sopenharmony_ci * @wq_head: the wait queue head
25562306a36Sopenharmony_ci *
25662306a36Sopenharmony_ci * In the very rare cases where a ->poll() implementation uses a waitqueue whose
25762306a36Sopenharmony_ci * lifetime is tied to a task rather than to the 'struct file' being polled,
25862306a36Sopenharmony_ci * this function must be called before the waitqueue is freed so that
25962306a36Sopenharmony_ci * non-blocking polls (e.g. epoll) are notified that the queue is going away.
26062306a36Sopenharmony_ci *
26162306a36Sopenharmony_ci * The caller must also RCU-delay the freeing of the wait_queue_head, e.g. via
26262306a36Sopenharmony_ci * an explicit synchronize_rcu() or call_rcu(), or via SLAB_TYPESAFE_BY_RCU.
26362306a36Sopenharmony_ci */
26462306a36Sopenharmony_cistatic inline void wake_up_pollfree(struct wait_queue_head *wq_head)
26562306a36Sopenharmony_ci{
26662306a36Sopenharmony_ci	/*
26762306a36Sopenharmony_ci	 * For performance reasons, we don't always take the queue lock here.
26862306a36Sopenharmony_ci	 * Therefore, we might race with someone removing the last entry from
26962306a36Sopenharmony_ci	 * the queue, and proceed while they still hold the queue lock.
27062306a36Sopenharmony_ci	 * However, rcu_read_lock() is required to be held in such cases, so we
27162306a36Sopenharmony_ci	 * can safely proceed with an RCU-delayed free.
27262306a36Sopenharmony_ci	 */
27362306a36Sopenharmony_ci	if (waitqueue_active(wq_head))
27462306a36Sopenharmony_ci		__wake_up_pollfree(wq_head);
27562306a36Sopenharmony_ci}
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci#define ___wait_cond_timeout(condition)						\
27862306a36Sopenharmony_ci({										\
27962306a36Sopenharmony_ci	bool __cond = (condition);						\
28062306a36Sopenharmony_ci	if (__cond && !__ret)							\
28162306a36Sopenharmony_ci		__ret = 1;							\
28262306a36Sopenharmony_ci	__cond || !__ret;							\
28362306a36Sopenharmony_ci})
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci#define ___wait_is_interruptible(state)						\
28662306a36Sopenharmony_ci	(!__builtin_constant_p(state) ||					\
28762306a36Sopenharmony_ci	 (state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ciextern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci/*
29262306a36Sopenharmony_ci * The below macro ___wait_event() has an explicit shadow of the __ret
29362306a36Sopenharmony_ci * variable when used from the wait_event_*() macros.
29462306a36Sopenharmony_ci *
29562306a36Sopenharmony_ci * This is so that both can use the ___wait_cond_timeout() construct
29662306a36Sopenharmony_ci * to wrap the condition.
29762306a36Sopenharmony_ci *
29862306a36Sopenharmony_ci * The type inconsistency of the wait_event_*() __ret variable is also
29962306a36Sopenharmony_ci * on purpose; we use long where we can return timeout values and int
30062306a36Sopenharmony_ci * otherwise.
30162306a36Sopenharmony_ci */
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci#define ___wait_event(wq_head, condition, state, exclusive, ret, cmd)		\
30462306a36Sopenharmony_ci({										\
30562306a36Sopenharmony_ci	__label__ __out;							\
30662306a36Sopenharmony_ci	struct wait_queue_entry __wq_entry;					\
30762306a36Sopenharmony_ci	long __ret = ret;	/* explicit shadow */				\
30862306a36Sopenharmony_ci										\
30962306a36Sopenharmony_ci	init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0);	\
31062306a36Sopenharmony_ci	for (;;) {								\
31162306a36Sopenharmony_ci		long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
31262306a36Sopenharmony_ci										\
31362306a36Sopenharmony_ci		if (condition)							\
31462306a36Sopenharmony_ci			break;							\
31562306a36Sopenharmony_ci										\
31662306a36Sopenharmony_ci		if (___wait_is_interruptible(state) && __int) {			\
31762306a36Sopenharmony_ci			__ret = __int;						\
31862306a36Sopenharmony_ci			goto __out;						\
31962306a36Sopenharmony_ci		}								\
32062306a36Sopenharmony_ci										\
32162306a36Sopenharmony_ci		cmd;								\
32262306a36Sopenharmony_ci	}									\
32362306a36Sopenharmony_ci	finish_wait(&wq_head, &__wq_entry);					\
32462306a36Sopenharmony_ci__out:	__ret;									\
32562306a36Sopenharmony_ci})
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci#define __wait_event(wq_head, condition)					\
32862306a36Sopenharmony_ci	(void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
32962306a36Sopenharmony_ci			    schedule())
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci/**
33262306a36Sopenharmony_ci * wait_event - sleep until a condition gets true
33362306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
33462306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
33562306a36Sopenharmony_ci *
33662306a36Sopenharmony_ci * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
33762306a36Sopenharmony_ci * @condition evaluates to true. The @condition is checked each time
33862306a36Sopenharmony_ci * the waitqueue @wq_head is woken up.
33962306a36Sopenharmony_ci *
34062306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
34162306a36Sopenharmony_ci * change the result of the wait condition.
34262306a36Sopenharmony_ci */
34362306a36Sopenharmony_ci#define wait_event(wq_head, condition)						\
34462306a36Sopenharmony_cido {										\
34562306a36Sopenharmony_ci	might_sleep();								\
34662306a36Sopenharmony_ci	if (condition)								\
34762306a36Sopenharmony_ci		break;								\
34862306a36Sopenharmony_ci	__wait_event(wq_head, condition);					\
34962306a36Sopenharmony_ci} while (0)
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci#define __io_wait_event(wq_head, condition)					\
35262306a36Sopenharmony_ci	(void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
35362306a36Sopenharmony_ci			    io_schedule())
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci/*
35662306a36Sopenharmony_ci * io_wait_event() -- like wait_event() but with io_schedule()
35762306a36Sopenharmony_ci */
35862306a36Sopenharmony_ci#define io_wait_event(wq_head, condition)					\
35962306a36Sopenharmony_cido {										\
36062306a36Sopenharmony_ci	might_sleep();								\
36162306a36Sopenharmony_ci	if (condition)								\
36262306a36Sopenharmony_ci		break;								\
36362306a36Sopenharmony_ci	__io_wait_event(wq_head, condition);					\
36462306a36Sopenharmony_ci} while (0)
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci#define __wait_event_freezable(wq_head, condition)				\
36762306a36Sopenharmony_ci	___wait_event(wq_head, condition, (TASK_INTERRUPTIBLE|TASK_FREEZABLE),	\
36862306a36Sopenharmony_ci			0, 0, schedule())
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci/**
37162306a36Sopenharmony_ci * wait_event_freezable - sleep (or freeze) until a condition gets true
37262306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
37362306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
37462306a36Sopenharmony_ci *
37562306a36Sopenharmony_ci * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
37662306a36Sopenharmony_ci * to system load) until the @condition evaluates to true. The
37762306a36Sopenharmony_ci * @condition is checked each time the waitqueue @wq_head is woken up.
37862306a36Sopenharmony_ci *
37962306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
38062306a36Sopenharmony_ci * change the result of the wait condition.
38162306a36Sopenharmony_ci */
38262306a36Sopenharmony_ci#define wait_event_freezable(wq_head, condition)				\
38362306a36Sopenharmony_ci({										\
38462306a36Sopenharmony_ci	int __ret = 0;								\
38562306a36Sopenharmony_ci	might_sleep();								\
38662306a36Sopenharmony_ci	if (!(condition))							\
38762306a36Sopenharmony_ci		__ret = __wait_event_freezable(wq_head, condition);		\
38862306a36Sopenharmony_ci	__ret;									\
38962306a36Sopenharmony_ci})
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci#define __wait_event_timeout(wq_head, condition, timeout)			\
39262306a36Sopenharmony_ci	___wait_event(wq_head, ___wait_cond_timeout(condition),			\
39362306a36Sopenharmony_ci		      TASK_UNINTERRUPTIBLE, 0, timeout,				\
39462306a36Sopenharmony_ci		      __ret = schedule_timeout(__ret))
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci/**
39762306a36Sopenharmony_ci * wait_event_timeout - sleep until a condition gets true or a timeout elapses
39862306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
39962306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
40062306a36Sopenharmony_ci * @timeout: timeout, in jiffies
40162306a36Sopenharmony_ci *
40262306a36Sopenharmony_ci * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
40362306a36Sopenharmony_ci * @condition evaluates to true. The @condition is checked each time
40462306a36Sopenharmony_ci * the waitqueue @wq_head is woken up.
40562306a36Sopenharmony_ci *
40662306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
40762306a36Sopenharmony_ci * change the result of the wait condition.
40862306a36Sopenharmony_ci *
40962306a36Sopenharmony_ci * Returns:
41062306a36Sopenharmony_ci * 0 if the @condition evaluated to %false after the @timeout elapsed,
41162306a36Sopenharmony_ci * 1 if the @condition evaluated to %true after the @timeout elapsed,
41262306a36Sopenharmony_ci * or the remaining jiffies (at least 1) if the @condition evaluated
41362306a36Sopenharmony_ci * to %true before the @timeout elapsed.
41462306a36Sopenharmony_ci */
41562306a36Sopenharmony_ci#define wait_event_timeout(wq_head, condition, timeout)				\
41662306a36Sopenharmony_ci({										\
41762306a36Sopenharmony_ci	long __ret = timeout;							\
41862306a36Sopenharmony_ci	might_sleep();								\
41962306a36Sopenharmony_ci	if (!___wait_cond_timeout(condition))					\
42062306a36Sopenharmony_ci		__ret = __wait_event_timeout(wq_head, condition, timeout);	\
42162306a36Sopenharmony_ci	__ret;									\
42262306a36Sopenharmony_ci})
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci#define __wait_event_freezable_timeout(wq_head, condition, timeout)		\
42562306a36Sopenharmony_ci	___wait_event(wq_head, ___wait_cond_timeout(condition),			\
42662306a36Sopenharmony_ci		      (TASK_INTERRUPTIBLE|TASK_FREEZABLE), 0, timeout,		\
42762306a36Sopenharmony_ci		      __ret = schedule_timeout(__ret))
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci/*
43062306a36Sopenharmony_ci * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
43162306a36Sopenharmony_ci * increasing load and is freezable.
43262306a36Sopenharmony_ci */
43362306a36Sopenharmony_ci#define wait_event_freezable_timeout(wq_head, condition, timeout)		\
43462306a36Sopenharmony_ci({										\
43562306a36Sopenharmony_ci	long __ret = timeout;							\
43662306a36Sopenharmony_ci	might_sleep();								\
43762306a36Sopenharmony_ci	if (!___wait_cond_timeout(condition))					\
43862306a36Sopenharmony_ci		__ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
43962306a36Sopenharmony_ci	__ret;									\
44062306a36Sopenharmony_ci})
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_ci#define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2)		\
44362306a36Sopenharmony_ci	(void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0,	\
44462306a36Sopenharmony_ci			    cmd1; schedule(); cmd2)
44562306a36Sopenharmony_ci/*
44662306a36Sopenharmony_ci * Just like wait_event_cmd(), except it sets exclusive flag
44762306a36Sopenharmony_ci */
44862306a36Sopenharmony_ci#define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2)		\
44962306a36Sopenharmony_cido {										\
45062306a36Sopenharmony_ci	if (condition)								\
45162306a36Sopenharmony_ci		break;								\
45262306a36Sopenharmony_ci	__wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2);		\
45362306a36Sopenharmony_ci} while (0)
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci#define __wait_event_cmd(wq_head, condition, cmd1, cmd2)			\
45662306a36Sopenharmony_ci	(void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
45762306a36Sopenharmony_ci			    cmd1; schedule(); cmd2)
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci/**
46062306a36Sopenharmony_ci * wait_event_cmd - sleep until a condition gets true
46162306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
46262306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
46362306a36Sopenharmony_ci * @cmd1: the command will be executed before sleep
46462306a36Sopenharmony_ci * @cmd2: the command will be executed after sleep
46562306a36Sopenharmony_ci *
46662306a36Sopenharmony_ci * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
46762306a36Sopenharmony_ci * @condition evaluates to true. The @condition is checked each time
46862306a36Sopenharmony_ci * the waitqueue @wq_head is woken up.
46962306a36Sopenharmony_ci *
47062306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
47162306a36Sopenharmony_ci * change the result of the wait condition.
47262306a36Sopenharmony_ci */
47362306a36Sopenharmony_ci#define wait_event_cmd(wq_head, condition, cmd1, cmd2)				\
47462306a36Sopenharmony_cido {										\
47562306a36Sopenharmony_ci	if (condition)								\
47662306a36Sopenharmony_ci		break;								\
47762306a36Sopenharmony_ci	__wait_event_cmd(wq_head, condition, cmd1, cmd2);			\
47862306a36Sopenharmony_ci} while (0)
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci#define __wait_event_interruptible(wq_head, condition)				\
48162306a36Sopenharmony_ci	___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,		\
48262306a36Sopenharmony_ci		      schedule())
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci/**
48562306a36Sopenharmony_ci * wait_event_interruptible - sleep until a condition gets true
48662306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
48762306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
48862306a36Sopenharmony_ci *
48962306a36Sopenharmony_ci * The process is put to sleep (TASK_INTERRUPTIBLE) until the
49062306a36Sopenharmony_ci * @condition evaluates to true or a signal is received.
49162306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq_head is woken up.
49262306a36Sopenharmony_ci *
49362306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
49462306a36Sopenharmony_ci * change the result of the wait condition.
49562306a36Sopenharmony_ci *
49662306a36Sopenharmony_ci * The function will return -ERESTARTSYS if it was interrupted by a
49762306a36Sopenharmony_ci * signal and 0 if @condition evaluated to true.
49862306a36Sopenharmony_ci */
49962306a36Sopenharmony_ci#define wait_event_interruptible(wq_head, condition)				\
50062306a36Sopenharmony_ci({										\
50162306a36Sopenharmony_ci	int __ret = 0;								\
50262306a36Sopenharmony_ci	might_sleep();								\
50362306a36Sopenharmony_ci	if (!(condition))							\
50462306a36Sopenharmony_ci		__ret = __wait_event_interruptible(wq_head, condition);		\
50562306a36Sopenharmony_ci	__ret;									\
50662306a36Sopenharmony_ci})
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci#define __wait_event_interruptible_timeout(wq_head, condition, timeout)		\
50962306a36Sopenharmony_ci	___wait_event(wq_head, ___wait_cond_timeout(condition),			\
51062306a36Sopenharmony_ci		      TASK_INTERRUPTIBLE, 0, timeout,				\
51162306a36Sopenharmony_ci		      __ret = schedule_timeout(__ret))
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci/**
51462306a36Sopenharmony_ci * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
51562306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
51662306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
51762306a36Sopenharmony_ci * @timeout: timeout, in jiffies
51862306a36Sopenharmony_ci *
51962306a36Sopenharmony_ci * The process is put to sleep (TASK_INTERRUPTIBLE) until the
52062306a36Sopenharmony_ci * @condition evaluates to true or a signal is received.
52162306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq_head is woken up.
52262306a36Sopenharmony_ci *
52362306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
52462306a36Sopenharmony_ci * change the result of the wait condition.
52562306a36Sopenharmony_ci *
52662306a36Sopenharmony_ci * Returns:
52762306a36Sopenharmony_ci * 0 if the @condition evaluated to %false after the @timeout elapsed,
52862306a36Sopenharmony_ci * 1 if the @condition evaluated to %true after the @timeout elapsed,
52962306a36Sopenharmony_ci * the remaining jiffies (at least 1) if the @condition evaluated
53062306a36Sopenharmony_ci * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
53162306a36Sopenharmony_ci * interrupted by a signal.
53262306a36Sopenharmony_ci */
53362306a36Sopenharmony_ci#define wait_event_interruptible_timeout(wq_head, condition, timeout)		\
53462306a36Sopenharmony_ci({										\
53562306a36Sopenharmony_ci	long __ret = timeout;							\
53662306a36Sopenharmony_ci	might_sleep();								\
53762306a36Sopenharmony_ci	if (!___wait_cond_timeout(condition))					\
53862306a36Sopenharmony_ci		__ret = __wait_event_interruptible_timeout(wq_head,		\
53962306a36Sopenharmony_ci						condition, timeout);		\
54062306a36Sopenharmony_ci	__ret;									\
54162306a36Sopenharmony_ci})
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ci#define __wait_event_hrtimeout(wq_head, condition, timeout, state)		\
54462306a36Sopenharmony_ci({										\
54562306a36Sopenharmony_ci	int __ret = 0;								\
54662306a36Sopenharmony_ci	struct hrtimer_sleeper __t;						\
54762306a36Sopenharmony_ci										\
54862306a36Sopenharmony_ci	hrtimer_init_sleeper_on_stack(&__t, CLOCK_MONOTONIC,			\
54962306a36Sopenharmony_ci				      HRTIMER_MODE_REL);			\
55062306a36Sopenharmony_ci	if ((timeout) != KTIME_MAX) {						\
55162306a36Sopenharmony_ci		hrtimer_set_expires_range_ns(&__t.timer, timeout,		\
55262306a36Sopenharmony_ci					current->timer_slack_ns);		\
55362306a36Sopenharmony_ci		hrtimer_sleeper_start_expires(&__t, HRTIMER_MODE_REL);		\
55462306a36Sopenharmony_ci	}									\
55562306a36Sopenharmony_ci										\
55662306a36Sopenharmony_ci	__ret = ___wait_event(wq_head, condition, state, 0, 0,			\
55762306a36Sopenharmony_ci		if (!__t.task) {						\
55862306a36Sopenharmony_ci			__ret = -ETIME;						\
55962306a36Sopenharmony_ci			break;							\
56062306a36Sopenharmony_ci		}								\
56162306a36Sopenharmony_ci		schedule());							\
56262306a36Sopenharmony_ci										\
56362306a36Sopenharmony_ci	hrtimer_cancel(&__t.timer);						\
56462306a36Sopenharmony_ci	destroy_hrtimer_on_stack(&__t.timer);					\
56562306a36Sopenharmony_ci	__ret;									\
56662306a36Sopenharmony_ci})
56762306a36Sopenharmony_ci
56862306a36Sopenharmony_ci/**
56962306a36Sopenharmony_ci * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
57062306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
57162306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
57262306a36Sopenharmony_ci * @timeout: timeout, as a ktime_t
57362306a36Sopenharmony_ci *
57462306a36Sopenharmony_ci * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
57562306a36Sopenharmony_ci * @condition evaluates to true or a signal is received.
57662306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq_head is woken up.
57762306a36Sopenharmony_ci *
57862306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
57962306a36Sopenharmony_ci * change the result of the wait condition.
58062306a36Sopenharmony_ci *
58162306a36Sopenharmony_ci * The function returns 0 if @condition became true, or -ETIME if the timeout
58262306a36Sopenharmony_ci * elapsed.
58362306a36Sopenharmony_ci */
58462306a36Sopenharmony_ci#define wait_event_hrtimeout(wq_head, condition, timeout)			\
58562306a36Sopenharmony_ci({										\
58662306a36Sopenharmony_ci	int __ret = 0;								\
58762306a36Sopenharmony_ci	might_sleep();								\
58862306a36Sopenharmony_ci	if (!(condition))							\
58962306a36Sopenharmony_ci		__ret = __wait_event_hrtimeout(wq_head, condition, timeout,	\
59062306a36Sopenharmony_ci					       TASK_UNINTERRUPTIBLE);		\
59162306a36Sopenharmony_ci	__ret;									\
59262306a36Sopenharmony_ci})
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci/**
59562306a36Sopenharmony_ci * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
59662306a36Sopenharmony_ci * @wq: the waitqueue to wait on
59762306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
59862306a36Sopenharmony_ci * @timeout: timeout, as a ktime_t
59962306a36Sopenharmony_ci *
60062306a36Sopenharmony_ci * The process is put to sleep (TASK_INTERRUPTIBLE) until the
60162306a36Sopenharmony_ci * @condition evaluates to true or a signal is received.
60262306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq is woken up.
60362306a36Sopenharmony_ci *
60462306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
60562306a36Sopenharmony_ci * change the result of the wait condition.
60662306a36Sopenharmony_ci *
60762306a36Sopenharmony_ci * The function returns 0 if @condition became true, -ERESTARTSYS if it was
60862306a36Sopenharmony_ci * interrupted by a signal, or -ETIME if the timeout elapsed.
60962306a36Sopenharmony_ci */
61062306a36Sopenharmony_ci#define wait_event_interruptible_hrtimeout(wq, condition, timeout)		\
61162306a36Sopenharmony_ci({										\
61262306a36Sopenharmony_ci	long __ret = 0;								\
61362306a36Sopenharmony_ci	might_sleep();								\
61462306a36Sopenharmony_ci	if (!(condition))							\
61562306a36Sopenharmony_ci		__ret = __wait_event_hrtimeout(wq, condition, timeout,		\
61662306a36Sopenharmony_ci					       TASK_INTERRUPTIBLE);		\
61762306a36Sopenharmony_ci	__ret;									\
61862306a36Sopenharmony_ci})
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_ci#define __wait_event_interruptible_exclusive(wq, condition)			\
62162306a36Sopenharmony_ci	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,			\
62262306a36Sopenharmony_ci		      schedule())
62362306a36Sopenharmony_ci
62462306a36Sopenharmony_ci#define wait_event_interruptible_exclusive(wq, condition)			\
62562306a36Sopenharmony_ci({										\
62662306a36Sopenharmony_ci	int __ret = 0;								\
62762306a36Sopenharmony_ci	might_sleep();								\
62862306a36Sopenharmony_ci	if (!(condition))							\
62962306a36Sopenharmony_ci		__ret = __wait_event_interruptible_exclusive(wq, condition);	\
63062306a36Sopenharmony_ci	__ret;									\
63162306a36Sopenharmony_ci})
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci#define __wait_event_killable_exclusive(wq, condition)				\
63462306a36Sopenharmony_ci	___wait_event(wq, condition, TASK_KILLABLE, 1, 0,			\
63562306a36Sopenharmony_ci		      schedule())
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ci#define wait_event_killable_exclusive(wq, condition)				\
63862306a36Sopenharmony_ci({										\
63962306a36Sopenharmony_ci	int __ret = 0;								\
64062306a36Sopenharmony_ci	might_sleep();								\
64162306a36Sopenharmony_ci	if (!(condition))							\
64262306a36Sopenharmony_ci		__ret = __wait_event_killable_exclusive(wq, condition);		\
64362306a36Sopenharmony_ci	__ret;									\
64462306a36Sopenharmony_ci})
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ci
64762306a36Sopenharmony_ci#define __wait_event_freezable_exclusive(wq, condition)				\
64862306a36Sopenharmony_ci	___wait_event(wq, condition, (TASK_INTERRUPTIBLE|TASK_FREEZABLE), 1, 0,\
64962306a36Sopenharmony_ci			schedule())
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci#define wait_event_freezable_exclusive(wq, condition)				\
65262306a36Sopenharmony_ci({										\
65362306a36Sopenharmony_ci	int __ret = 0;								\
65462306a36Sopenharmony_ci	might_sleep();								\
65562306a36Sopenharmony_ci	if (!(condition))							\
65662306a36Sopenharmony_ci		__ret = __wait_event_freezable_exclusive(wq, condition);	\
65762306a36Sopenharmony_ci	__ret;									\
65862306a36Sopenharmony_ci})
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci/**
66162306a36Sopenharmony_ci * wait_event_idle - wait for a condition without contributing to system load
66262306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
66362306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
66462306a36Sopenharmony_ci *
66562306a36Sopenharmony_ci * The process is put to sleep (TASK_IDLE) until the
66662306a36Sopenharmony_ci * @condition evaluates to true.
66762306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq_head is woken up.
66862306a36Sopenharmony_ci *
66962306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
67062306a36Sopenharmony_ci * change the result of the wait condition.
67162306a36Sopenharmony_ci *
67262306a36Sopenharmony_ci */
67362306a36Sopenharmony_ci#define wait_event_idle(wq_head, condition)					\
67462306a36Sopenharmony_cido {										\
67562306a36Sopenharmony_ci	might_sleep();								\
67662306a36Sopenharmony_ci	if (!(condition))							\
67762306a36Sopenharmony_ci		___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule());	\
67862306a36Sopenharmony_ci} while (0)
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_ci/**
68162306a36Sopenharmony_ci * wait_event_idle_exclusive - wait for a condition with contributing to system load
68262306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
68362306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
68462306a36Sopenharmony_ci *
68562306a36Sopenharmony_ci * The process is put to sleep (TASK_IDLE) until the
68662306a36Sopenharmony_ci * @condition evaluates to true.
68762306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq_head is woken up.
68862306a36Sopenharmony_ci *
68962306a36Sopenharmony_ci * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
69062306a36Sopenharmony_ci * set thus if other processes wait on the same list, when this
69162306a36Sopenharmony_ci * process is woken further processes are not considered.
69262306a36Sopenharmony_ci *
69362306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
69462306a36Sopenharmony_ci * change the result of the wait condition.
69562306a36Sopenharmony_ci *
69662306a36Sopenharmony_ci */
69762306a36Sopenharmony_ci#define wait_event_idle_exclusive(wq_head, condition)				\
69862306a36Sopenharmony_cido {										\
69962306a36Sopenharmony_ci	might_sleep();								\
70062306a36Sopenharmony_ci	if (!(condition))							\
70162306a36Sopenharmony_ci		___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule());	\
70262306a36Sopenharmony_ci} while (0)
70362306a36Sopenharmony_ci
70462306a36Sopenharmony_ci#define __wait_event_idle_timeout(wq_head, condition, timeout)			\
70562306a36Sopenharmony_ci	___wait_event(wq_head, ___wait_cond_timeout(condition),			\
70662306a36Sopenharmony_ci		      TASK_IDLE, 0, timeout,					\
70762306a36Sopenharmony_ci		      __ret = schedule_timeout(__ret))
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci/**
71062306a36Sopenharmony_ci * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
71162306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
71262306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
71362306a36Sopenharmony_ci * @timeout: timeout, in jiffies
71462306a36Sopenharmony_ci *
71562306a36Sopenharmony_ci * The process is put to sleep (TASK_IDLE) until the
71662306a36Sopenharmony_ci * @condition evaluates to true. The @condition is checked each time
71762306a36Sopenharmony_ci * the waitqueue @wq_head is woken up.
71862306a36Sopenharmony_ci *
71962306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
72062306a36Sopenharmony_ci * change the result of the wait condition.
72162306a36Sopenharmony_ci *
72262306a36Sopenharmony_ci * Returns:
72362306a36Sopenharmony_ci * 0 if the @condition evaluated to %false after the @timeout elapsed,
72462306a36Sopenharmony_ci * 1 if the @condition evaluated to %true after the @timeout elapsed,
72562306a36Sopenharmony_ci * or the remaining jiffies (at least 1) if the @condition evaluated
72662306a36Sopenharmony_ci * to %true before the @timeout elapsed.
72762306a36Sopenharmony_ci */
72862306a36Sopenharmony_ci#define wait_event_idle_timeout(wq_head, condition, timeout)			\
72962306a36Sopenharmony_ci({										\
73062306a36Sopenharmony_ci	long __ret = timeout;							\
73162306a36Sopenharmony_ci	might_sleep();								\
73262306a36Sopenharmony_ci	if (!___wait_cond_timeout(condition))					\
73362306a36Sopenharmony_ci		__ret = __wait_event_idle_timeout(wq_head, condition, timeout);	\
73462306a36Sopenharmony_ci	__ret;									\
73562306a36Sopenharmony_ci})
73662306a36Sopenharmony_ci
73762306a36Sopenharmony_ci#define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout)	\
73862306a36Sopenharmony_ci	___wait_event(wq_head, ___wait_cond_timeout(condition),			\
73962306a36Sopenharmony_ci		      TASK_IDLE, 1, timeout,					\
74062306a36Sopenharmony_ci		      __ret = schedule_timeout(__ret))
74162306a36Sopenharmony_ci
74262306a36Sopenharmony_ci/**
74362306a36Sopenharmony_ci * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
74462306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
74562306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
74662306a36Sopenharmony_ci * @timeout: timeout, in jiffies
74762306a36Sopenharmony_ci *
74862306a36Sopenharmony_ci * The process is put to sleep (TASK_IDLE) until the
74962306a36Sopenharmony_ci * @condition evaluates to true. The @condition is checked each time
75062306a36Sopenharmony_ci * the waitqueue @wq_head is woken up.
75162306a36Sopenharmony_ci *
75262306a36Sopenharmony_ci * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
75362306a36Sopenharmony_ci * set thus if other processes wait on the same list, when this
75462306a36Sopenharmony_ci * process is woken further processes are not considered.
75562306a36Sopenharmony_ci *
75662306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
75762306a36Sopenharmony_ci * change the result of the wait condition.
75862306a36Sopenharmony_ci *
75962306a36Sopenharmony_ci * Returns:
76062306a36Sopenharmony_ci * 0 if the @condition evaluated to %false after the @timeout elapsed,
76162306a36Sopenharmony_ci * 1 if the @condition evaluated to %true after the @timeout elapsed,
76262306a36Sopenharmony_ci * or the remaining jiffies (at least 1) if the @condition evaluated
76362306a36Sopenharmony_ci * to %true before the @timeout elapsed.
76462306a36Sopenharmony_ci */
76562306a36Sopenharmony_ci#define wait_event_idle_exclusive_timeout(wq_head, condition, timeout)		\
76662306a36Sopenharmony_ci({										\
76762306a36Sopenharmony_ci	long __ret = timeout;							\
76862306a36Sopenharmony_ci	might_sleep();								\
76962306a36Sopenharmony_ci	if (!___wait_cond_timeout(condition))					\
77062306a36Sopenharmony_ci		__ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
77162306a36Sopenharmony_ci	__ret;									\
77262306a36Sopenharmony_ci})
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ciextern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
77562306a36Sopenharmony_ciextern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
77662306a36Sopenharmony_ci
77762306a36Sopenharmony_ci#define __wait_event_interruptible_locked(wq, condition, exclusive, fn)		\
77862306a36Sopenharmony_ci({										\
77962306a36Sopenharmony_ci	int __ret;								\
78062306a36Sopenharmony_ci	DEFINE_WAIT(__wait);							\
78162306a36Sopenharmony_ci	if (exclusive)								\
78262306a36Sopenharmony_ci		__wait.flags |= WQ_FLAG_EXCLUSIVE;				\
78362306a36Sopenharmony_ci	do {									\
78462306a36Sopenharmony_ci		__ret = fn(&(wq), &__wait);					\
78562306a36Sopenharmony_ci		if (__ret)							\
78662306a36Sopenharmony_ci			break;							\
78762306a36Sopenharmony_ci	} while (!(condition));							\
78862306a36Sopenharmony_ci	__remove_wait_queue(&(wq), &__wait);					\
78962306a36Sopenharmony_ci	__set_current_state(TASK_RUNNING);					\
79062306a36Sopenharmony_ci	__ret;									\
79162306a36Sopenharmony_ci})
79262306a36Sopenharmony_ci
79362306a36Sopenharmony_ci
79462306a36Sopenharmony_ci/**
79562306a36Sopenharmony_ci * wait_event_interruptible_locked - sleep until a condition gets true
79662306a36Sopenharmony_ci * @wq: the waitqueue to wait on
79762306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
79862306a36Sopenharmony_ci *
79962306a36Sopenharmony_ci * The process is put to sleep (TASK_INTERRUPTIBLE) until the
80062306a36Sopenharmony_ci * @condition evaluates to true or a signal is received.
80162306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq is woken up.
80262306a36Sopenharmony_ci *
80362306a36Sopenharmony_ci * It must be called with wq.lock being held.  This spinlock is
80462306a36Sopenharmony_ci * unlocked while sleeping but @condition testing is done while lock
80562306a36Sopenharmony_ci * is held and when this macro exits the lock is held.
80662306a36Sopenharmony_ci *
80762306a36Sopenharmony_ci * The lock is locked/unlocked using spin_lock()/spin_unlock()
80862306a36Sopenharmony_ci * functions which must match the way they are locked/unlocked outside
80962306a36Sopenharmony_ci * of this macro.
81062306a36Sopenharmony_ci *
81162306a36Sopenharmony_ci * wake_up_locked() has to be called after changing any variable that could
81262306a36Sopenharmony_ci * change the result of the wait condition.
81362306a36Sopenharmony_ci *
81462306a36Sopenharmony_ci * The function will return -ERESTARTSYS if it was interrupted by a
81562306a36Sopenharmony_ci * signal and 0 if @condition evaluated to true.
81662306a36Sopenharmony_ci */
81762306a36Sopenharmony_ci#define wait_event_interruptible_locked(wq, condition)				\
81862306a36Sopenharmony_ci	((condition)								\
81962306a36Sopenharmony_ci	 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
82062306a36Sopenharmony_ci
82162306a36Sopenharmony_ci/**
82262306a36Sopenharmony_ci * wait_event_interruptible_locked_irq - sleep until a condition gets true
82362306a36Sopenharmony_ci * @wq: the waitqueue to wait on
82462306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
82562306a36Sopenharmony_ci *
82662306a36Sopenharmony_ci * The process is put to sleep (TASK_INTERRUPTIBLE) until the
82762306a36Sopenharmony_ci * @condition evaluates to true or a signal is received.
82862306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq is woken up.
82962306a36Sopenharmony_ci *
83062306a36Sopenharmony_ci * It must be called with wq.lock being held.  This spinlock is
83162306a36Sopenharmony_ci * unlocked while sleeping but @condition testing is done while lock
83262306a36Sopenharmony_ci * is held and when this macro exits the lock is held.
83362306a36Sopenharmony_ci *
83462306a36Sopenharmony_ci * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
83562306a36Sopenharmony_ci * functions which must match the way they are locked/unlocked outside
83662306a36Sopenharmony_ci * of this macro.
83762306a36Sopenharmony_ci *
83862306a36Sopenharmony_ci * wake_up_locked() has to be called after changing any variable that could
83962306a36Sopenharmony_ci * change the result of the wait condition.
84062306a36Sopenharmony_ci *
84162306a36Sopenharmony_ci * The function will return -ERESTARTSYS if it was interrupted by a
84262306a36Sopenharmony_ci * signal and 0 if @condition evaluated to true.
84362306a36Sopenharmony_ci */
84462306a36Sopenharmony_ci#define wait_event_interruptible_locked_irq(wq, condition)			\
84562306a36Sopenharmony_ci	((condition)								\
84662306a36Sopenharmony_ci	 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
84762306a36Sopenharmony_ci
84862306a36Sopenharmony_ci/**
84962306a36Sopenharmony_ci * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
85062306a36Sopenharmony_ci * @wq: the waitqueue to wait on
85162306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
85262306a36Sopenharmony_ci *
85362306a36Sopenharmony_ci * The process is put to sleep (TASK_INTERRUPTIBLE) until the
85462306a36Sopenharmony_ci * @condition evaluates to true or a signal is received.
85562306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq is woken up.
85662306a36Sopenharmony_ci *
85762306a36Sopenharmony_ci * It must be called with wq.lock being held.  This spinlock is
85862306a36Sopenharmony_ci * unlocked while sleeping but @condition testing is done while lock
85962306a36Sopenharmony_ci * is held and when this macro exits the lock is held.
86062306a36Sopenharmony_ci *
86162306a36Sopenharmony_ci * The lock is locked/unlocked using spin_lock()/spin_unlock()
86262306a36Sopenharmony_ci * functions which must match the way they are locked/unlocked outside
86362306a36Sopenharmony_ci * of this macro.
86462306a36Sopenharmony_ci *
86562306a36Sopenharmony_ci * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
86662306a36Sopenharmony_ci * set thus when other process waits process on the list if this
86762306a36Sopenharmony_ci * process is awaken further processes are not considered.
86862306a36Sopenharmony_ci *
86962306a36Sopenharmony_ci * wake_up_locked() has to be called after changing any variable that could
87062306a36Sopenharmony_ci * change the result of the wait condition.
87162306a36Sopenharmony_ci *
87262306a36Sopenharmony_ci * The function will return -ERESTARTSYS if it was interrupted by a
87362306a36Sopenharmony_ci * signal and 0 if @condition evaluated to true.
87462306a36Sopenharmony_ci */
87562306a36Sopenharmony_ci#define wait_event_interruptible_exclusive_locked(wq, condition)		\
87662306a36Sopenharmony_ci	((condition)								\
87762306a36Sopenharmony_ci	 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
87862306a36Sopenharmony_ci
87962306a36Sopenharmony_ci/**
88062306a36Sopenharmony_ci * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
88162306a36Sopenharmony_ci * @wq: the waitqueue to wait on
88262306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
88362306a36Sopenharmony_ci *
88462306a36Sopenharmony_ci * The process is put to sleep (TASK_INTERRUPTIBLE) until the
88562306a36Sopenharmony_ci * @condition evaluates to true or a signal is received.
88662306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq is woken up.
88762306a36Sopenharmony_ci *
88862306a36Sopenharmony_ci * It must be called with wq.lock being held.  This spinlock is
88962306a36Sopenharmony_ci * unlocked while sleeping but @condition testing is done while lock
89062306a36Sopenharmony_ci * is held and when this macro exits the lock is held.
89162306a36Sopenharmony_ci *
89262306a36Sopenharmony_ci * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
89362306a36Sopenharmony_ci * functions which must match the way they are locked/unlocked outside
89462306a36Sopenharmony_ci * of this macro.
89562306a36Sopenharmony_ci *
89662306a36Sopenharmony_ci * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
89762306a36Sopenharmony_ci * set thus when other process waits process on the list if this
89862306a36Sopenharmony_ci * process is awaken further processes are not considered.
89962306a36Sopenharmony_ci *
90062306a36Sopenharmony_ci * wake_up_locked() has to be called after changing any variable that could
90162306a36Sopenharmony_ci * change the result of the wait condition.
90262306a36Sopenharmony_ci *
90362306a36Sopenharmony_ci * The function will return -ERESTARTSYS if it was interrupted by a
90462306a36Sopenharmony_ci * signal and 0 if @condition evaluated to true.
90562306a36Sopenharmony_ci */
90662306a36Sopenharmony_ci#define wait_event_interruptible_exclusive_locked_irq(wq, condition)		\
90762306a36Sopenharmony_ci	((condition)								\
90862306a36Sopenharmony_ci	 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
90962306a36Sopenharmony_ci
91062306a36Sopenharmony_ci
91162306a36Sopenharmony_ci#define __wait_event_killable(wq, condition)					\
91262306a36Sopenharmony_ci	___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
91362306a36Sopenharmony_ci
91462306a36Sopenharmony_ci/**
91562306a36Sopenharmony_ci * wait_event_killable - sleep until a condition gets true
91662306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
91762306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
91862306a36Sopenharmony_ci *
91962306a36Sopenharmony_ci * The process is put to sleep (TASK_KILLABLE) until the
92062306a36Sopenharmony_ci * @condition evaluates to true or a signal is received.
92162306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq_head is woken up.
92262306a36Sopenharmony_ci *
92362306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
92462306a36Sopenharmony_ci * change the result of the wait condition.
92562306a36Sopenharmony_ci *
92662306a36Sopenharmony_ci * The function will return -ERESTARTSYS if it was interrupted by a
92762306a36Sopenharmony_ci * signal and 0 if @condition evaluated to true.
92862306a36Sopenharmony_ci */
92962306a36Sopenharmony_ci#define wait_event_killable(wq_head, condition)					\
93062306a36Sopenharmony_ci({										\
93162306a36Sopenharmony_ci	int __ret = 0;								\
93262306a36Sopenharmony_ci	might_sleep();								\
93362306a36Sopenharmony_ci	if (!(condition))							\
93462306a36Sopenharmony_ci		__ret = __wait_event_killable(wq_head, condition);		\
93562306a36Sopenharmony_ci	__ret;									\
93662306a36Sopenharmony_ci})
93762306a36Sopenharmony_ci
93862306a36Sopenharmony_ci#define __wait_event_state(wq, condition, state)				\
93962306a36Sopenharmony_ci	___wait_event(wq, condition, state, 0, 0, schedule())
94062306a36Sopenharmony_ci
94162306a36Sopenharmony_ci/**
94262306a36Sopenharmony_ci * wait_event_state - sleep until a condition gets true
94362306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
94462306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
94562306a36Sopenharmony_ci * @state: state to sleep in
94662306a36Sopenharmony_ci *
94762306a36Sopenharmony_ci * The process is put to sleep (@state) until the @condition evaluates to true
94862306a36Sopenharmony_ci * or a signal is received (when allowed by @state).  The @condition is checked
94962306a36Sopenharmony_ci * each time the waitqueue @wq_head is woken up.
95062306a36Sopenharmony_ci *
95162306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
95262306a36Sopenharmony_ci * change the result of the wait condition.
95362306a36Sopenharmony_ci *
95462306a36Sopenharmony_ci * The function will return -ERESTARTSYS if it was interrupted by a signal
95562306a36Sopenharmony_ci * (when allowed by @state) and 0 if @condition evaluated to true.
95662306a36Sopenharmony_ci */
95762306a36Sopenharmony_ci#define wait_event_state(wq_head, condition, state)				\
95862306a36Sopenharmony_ci({										\
95962306a36Sopenharmony_ci	int __ret = 0;								\
96062306a36Sopenharmony_ci	might_sleep();								\
96162306a36Sopenharmony_ci	if (!(condition))							\
96262306a36Sopenharmony_ci		__ret = __wait_event_state(wq_head, condition, state);		\
96362306a36Sopenharmony_ci	__ret;									\
96462306a36Sopenharmony_ci})
96562306a36Sopenharmony_ci
96662306a36Sopenharmony_ci#define __wait_event_killable_timeout(wq_head, condition, timeout)		\
96762306a36Sopenharmony_ci	___wait_event(wq_head, ___wait_cond_timeout(condition),			\
96862306a36Sopenharmony_ci		      TASK_KILLABLE, 0, timeout,				\
96962306a36Sopenharmony_ci		      __ret = schedule_timeout(__ret))
97062306a36Sopenharmony_ci
97162306a36Sopenharmony_ci/**
97262306a36Sopenharmony_ci * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
97362306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
97462306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
97562306a36Sopenharmony_ci * @timeout: timeout, in jiffies
97662306a36Sopenharmony_ci *
97762306a36Sopenharmony_ci * The process is put to sleep (TASK_KILLABLE) until the
97862306a36Sopenharmony_ci * @condition evaluates to true or a kill signal is received.
97962306a36Sopenharmony_ci * The @condition is checked each time the waitqueue @wq_head is woken up.
98062306a36Sopenharmony_ci *
98162306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
98262306a36Sopenharmony_ci * change the result of the wait condition.
98362306a36Sopenharmony_ci *
98462306a36Sopenharmony_ci * Returns:
98562306a36Sopenharmony_ci * 0 if the @condition evaluated to %false after the @timeout elapsed,
98662306a36Sopenharmony_ci * 1 if the @condition evaluated to %true after the @timeout elapsed,
98762306a36Sopenharmony_ci * the remaining jiffies (at least 1) if the @condition evaluated
98862306a36Sopenharmony_ci * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
98962306a36Sopenharmony_ci * interrupted by a kill signal.
99062306a36Sopenharmony_ci *
99162306a36Sopenharmony_ci * Only kill signals interrupt this process.
99262306a36Sopenharmony_ci */
99362306a36Sopenharmony_ci#define wait_event_killable_timeout(wq_head, condition, timeout)		\
99462306a36Sopenharmony_ci({										\
99562306a36Sopenharmony_ci	long __ret = timeout;							\
99662306a36Sopenharmony_ci	might_sleep();								\
99762306a36Sopenharmony_ci	if (!___wait_cond_timeout(condition))					\
99862306a36Sopenharmony_ci		__ret = __wait_event_killable_timeout(wq_head,			\
99962306a36Sopenharmony_ci						condition, timeout);		\
100062306a36Sopenharmony_ci	__ret;									\
100162306a36Sopenharmony_ci})
100262306a36Sopenharmony_ci
100362306a36Sopenharmony_ci
100462306a36Sopenharmony_ci#define __wait_event_lock_irq(wq_head, condition, lock, cmd)			\
100562306a36Sopenharmony_ci	(void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
100662306a36Sopenharmony_ci			    spin_unlock_irq(&lock);				\
100762306a36Sopenharmony_ci			    cmd;						\
100862306a36Sopenharmony_ci			    schedule();						\
100962306a36Sopenharmony_ci			    spin_lock_irq(&lock))
101062306a36Sopenharmony_ci
101162306a36Sopenharmony_ci/**
101262306a36Sopenharmony_ci * wait_event_lock_irq_cmd - sleep until a condition gets true. The
101362306a36Sopenharmony_ci *			     condition is checked under the lock. This
101462306a36Sopenharmony_ci *			     is expected to be called with the lock
101562306a36Sopenharmony_ci *			     taken.
101662306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
101762306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
101862306a36Sopenharmony_ci * @lock: a locked spinlock_t, which will be released before cmd
101962306a36Sopenharmony_ci *	  and schedule() and reacquired afterwards.
102062306a36Sopenharmony_ci * @cmd: a command which is invoked outside the critical section before
102162306a36Sopenharmony_ci *	 sleep
102262306a36Sopenharmony_ci *
102362306a36Sopenharmony_ci * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
102462306a36Sopenharmony_ci * @condition evaluates to true. The @condition is checked each time
102562306a36Sopenharmony_ci * the waitqueue @wq_head is woken up.
102662306a36Sopenharmony_ci *
102762306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
102862306a36Sopenharmony_ci * change the result of the wait condition.
102962306a36Sopenharmony_ci *
103062306a36Sopenharmony_ci * This is supposed to be called while holding the lock. The lock is
103162306a36Sopenharmony_ci * dropped before invoking the cmd and going to sleep and is reacquired
103262306a36Sopenharmony_ci * afterwards.
103362306a36Sopenharmony_ci */
103462306a36Sopenharmony_ci#define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd)			\
103562306a36Sopenharmony_cido {										\
103662306a36Sopenharmony_ci	if (condition)								\
103762306a36Sopenharmony_ci		break;								\
103862306a36Sopenharmony_ci	__wait_event_lock_irq(wq_head, condition, lock, cmd);			\
103962306a36Sopenharmony_ci} while (0)
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci/**
104262306a36Sopenharmony_ci * wait_event_lock_irq - sleep until a condition gets true. The
104362306a36Sopenharmony_ci *			 condition is checked under the lock. This
104462306a36Sopenharmony_ci *			 is expected to be called with the lock
104562306a36Sopenharmony_ci *			 taken.
104662306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
104762306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
104862306a36Sopenharmony_ci * @lock: a locked spinlock_t, which will be released before schedule()
104962306a36Sopenharmony_ci *	  and reacquired afterwards.
105062306a36Sopenharmony_ci *
105162306a36Sopenharmony_ci * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
105262306a36Sopenharmony_ci * @condition evaluates to true. The @condition is checked each time
105362306a36Sopenharmony_ci * the waitqueue @wq_head is woken up.
105462306a36Sopenharmony_ci *
105562306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
105662306a36Sopenharmony_ci * change the result of the wait condition.
105762306a36Sopenharmony_ci *
105862306a36Sopenharmony_ci * This is supposed to be called while holding the lock. The lock is
105962306a36Sopenharmony_ci * dropped before going to sleep and is reacquired afterwards.
106062306a36Sopenharmony_ci */
106162306a36Sopenharmony_ci#define wait_event_lock_irq(wq_head, condition, lock)				\
106262306a36Sopenharmony_cido {										\
106362306a36Sopenharmony_ci	if (condition)								\
106462306a36Sopenharmony_ci		break;								\
106562306a36Sopenharmony_ci	__wait_event_lock_irq(wq_head, condition, lock, );			\
106662306a36Sopenharmony_ci} while (0)
106762306a36Sopenharmony_ci
106862306a36Sopenharmony_ci
106962306a36Sopenharmony_ci#define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd)	\
107062306a36Sopenharmony_ci	___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,		\
107162306a36Sopenharmony_ci		      spin_unlock_irq(&lock);					\
107262306a36Sopenharmony_ci		      cmd;							\
107362306a36Sopenharmony_ci		      schedule();						\
107462306a36Sopenharmony_ci		      spin_lock_irq(&lock))
107562306a36Sopenharmony_ci
107662306a36Sopenharmony_ci/**
107762306a36Sopenharmony_ci * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
107862306a36Sopenharmony_ci *		The condition is checked under the lock. This is expected to
107962306a36Sopenharmony_ci *		be called with the lock taken.
108062306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
108162306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
108262306a36Sopenharmony_ci * @lock: a locked spinlock_t, which will be released before cmd and
108362306a36Sopenharmony_ci *	  schedule() and reacquired afterwards.
108462306a36Sopenharmony_ci * @cmd: a command which is invoked outside the critical section before
108562306a36Sopenharmony_ci *	 sleep
108662306a36Sopenharmony_ci *
108762306a36Sopenharmony_ci * The process is put to sleep (TASK_INTERRUPTIBLE) until the
108862306a36Sopenharmony_ci * @condition evaluates to true or a signal is received. The @condition is
108962306a36Sopenharmony_ci * checked each time the waitqueue @wq_head is woken up.
109062306a36Sopenharmony_ci *
109162306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
109262306a36Sopenharmony_ci * change the result of the wait condition.
109362306a36Sopenharmony_ci *
109462306a36Sopenharmony_ci * This is supposed to be called while holding the lock. The lock is
109562306a36Sopenharmony_ci * dropped before invoking the cmd and going to sleep and is reacquired
109662306a36Sopenharmony_ci * afterwards.
109762306a36Sopenharmony_ci *
109862306a36Sopenharmony_ci * The macro will return -ERESTARTSYS if it was interrupted by a signal
109962306a36Sopenharmony_ci * and 0 if @condition evaluated to true.
110062306a36Sopenharmony_ci */
110162306a36Sopenharmony_ci#define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd)	\
110262306a36Sopenharmony_ci({										\
110362306a36Sopenharmony_ci	int __ret = 0;								\
110462306a36Sopenharmony_ci	if (!(condition))							\
110562306a36Sopenharmony_ci		__ret = __wait_event_interruptible_lock_irq(wq_head,		\
110662306a36Sopenharmony_ci						condition, lock, cmd);		\
110762306a36Sopenharmony_ci	__ret;									\
110862306a36Sopenharmony_ci})
110962306a36Sopenharmony_ci
111062306a36Sopenharmony_ci/**
111162306a36Sopenharmony_ci * wait_event_interruptible_lock_irq - sleep until a condition gets true.
111262306a36Sopenharmony_ci *		The condition is checked under the lock. This is expected
111362306a36Sopenharmony_ci *		to be called with the lock taken.
111462306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
111562306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
111662306a36Sopenharmony_ci * @lock: a locked spinlock_t, which will be released before schedule()
111762306a36Sopenharmony_ci *	  and reacquired afterwards.
111862306a36Sopenharmony_ci *
111962306a36Sopenharmony_ci * The process is put to sleep (TASK_INTERRUPTIBLE) until the
112062306a36Sopenharmony_ci * @condition evaluates to true or signal is received. The @condition is
112162306a36Sopenharmony_ci * checked each time the waitqueue @wq_head is woken up.
112262306a36Sopenharmony_ci *
112362306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
112462306a36Sopenharmony_ci * change the result of the wait condition.
112562306a36Sopenharmony_ci *
112662306a36Sopenharmony_ci * This is supposed to be called while holding the lock. The lock is
112762306a36Sopenharmony_ci * dropped before going to sleep and is reacquired afterwards.
112862306a36Sopenharmony_ci *
112962306a36Sopenharmony_ci * The macro will return -ERESTARTSYS if it was interrupted by a signal
113062306a36Sopenharmony_ci * and 0 if @condition evaluated to true.
113162306a36Sopenharmony_ci */
113262306a36Sopenharmony_ci#define wait_event_interruptible_lock_irq(wq_head, condition, lock)		\
113362306a36Sopenharmony_ci({										\
113462306a36Sopenharmony_ci	int __ret = 0;								\
113562306a36Sopenharmony_ci	if (!(condition))							\
113662306a36Sopenharmony_ci		__ret = __wait_event_interruptible_lock_irq(wq_head,		\
113762306a36Sopenharmony_ci						condition, lock,);		\
113862306a36Sopenharmony_ci	__ret;									\
113962306a36Sopenharmony_ci})
114062306a36Sopenharmony_ci
114162306a36Sopenharmony_ci#define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state)	\
114262306a36Sopenharmony_ci	___wait_event(wq_head, ___wait_cond_timeout(condition),			\
114362306a36Sopenharmony_ci		      state, 0, timeout,					\
114462306a36Sopenharmony_ci		      spin_unlock_irq(&lock);					\
114562306a36Sopenharmony_ci		      __ret = schedule_timeout(__ret);				\
114662306a36Sopenharmony_ci		      spin_lock_irq(&lock));
114762306a36Sopenharmony_ci
114862306a36Sopenharmony_ci/**
114962306a36Sopenharmony_ci * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
115062306a36Sopenharmony_ci *		true or a timeout elapses. The condition is checked under
115162306a36Sopenharmony_ci *		the lock. This is expected to be called with the lock taken.
115262306a36Sopenharmony_ci * @wq_head: the waitqueue to wait on
115362306a36Sopenharmony_ci * @condition: a C expression for the event to wait for
115462306a36Sopenharmony_ci * @lock: a locked spinlock_t, which will be released before schedule()
115562306a36Sopenharmony_ci *	  and reacquired afterwards.
115662306a36Sopenharmony_ci * @timeout: timeout, in jiffies
115762306a36Sopenharmony_ci *
115862306a36Sopenharmony_ci * The process is put to sleep (TASK_INTERRUPTIBLE) until the
115962306a36Sopenharmony_ci * @condition evaluates to true or signal is received. The @condition is
116062306a36Sopenharmony_ci * checked each time the waitqueue @wq_head is woken up.
116162306a36Sopenharmony_ci *
116262306a36Sopenharmony_ci * wake_up() has to be called after changing any variable that could
116362306a36Sopenharmony_ci * change the result of the wait condition.
116462306a36Sopenharmony_ci *
116562306a36Sopenharmony_ci * This is supposed to be called while holding the lock. The lock is
116662306a36Sopenharmony_ci * dropped before going to sleep and is reacquired afterwards.
116762306a36Sopenharmony_ci *
116862306a36Sopenharmony_ci * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
116962306a36Sopenharmony_ci * was interrupted by a signal, and the remaining jiffies otherwise
117062306a36Sopenharmony_ci * if the condition evaluated to true before the timeout elapsed.
117162306a36Sopenharmony_ci */
117262306a36Sopenharmony_ci#define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock,	\
117362306a36Sopenharmony_ci						  timeout)			\
117462306a36Sopenharmony_ci({										\
117562306a36Sopenharmony_ci	long __ret = timeout;							\
117662306a36Sopenharmony_ci	if (!___wait_cond_timeout(condition))					\
117762306a36Sopenharmony_ci		__ret = __wait_event_lock_irq_timeout(				\
117862306a36Sopenharmony_ci					wq_head, condition, lock, timeout,	\
117962306a36Sopenharmony_ci					TASK_INTERRUPTIBLE);			\
118062306a36Sopenharmony_ci	__ret;									\
118162306a36Sopenharmony_ci})
118262306a36Sopenharmony_ci
118362306a36Sopenharmony_ci#define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout)		\
118462306a36Sopenharmony_ci({										\
118562306a36Sopenharmony_ci	long __ret = timeout;							\
118662306a36Sopenharmony_ci	if (!___wait_cond_timeout(condition))					\
118762306a36Sopenharmony_ci		__ret = __wait_event_lock_irq_timeout(				\
118862306a36Sopenharmony_ci					wq_head, condition, lock, timeout,	\
118962306a36Sopenharmony_ci					TASK_UNINTERRUPTIBLE);			\
119062306a36Sopenharmony_ci	__ret;									\
119162306a36Sopenharmony_ci})
119262306a36Sopenharmony_ci
119362306a36Sopenharmony_ci/*
119462306a36Sopenharmony_ci * Waitqueues which are removed from the waitqueue_head at wakeup time
119562306a36Sopenharmony_ci */
119662306a36Sopenharmony_civoid prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
119762306a36Sopenharmony_cibool prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
119862306a36Sopenharmony_cilong prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
119962306a36Sopenharmony_civoid finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
120062306a36Sopenharmony_cilong wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
120162306a36Sopenharmony_ciint woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
120262306a36Sopenharmony_ciint autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
120362306a36Sopenharmony_ci
120462306a36Sopenharmony_ci#define DEFINE_WAIT_FUNC(name, function)					\
120562306a36Sopenharmony_ci	struct wait_queue_entry name = {					\
120662306a36Sopenharmony_ci		.private	= current,					\
120762306a36Sopenharmony_ci		.func		= function,					\
120862306a36Sopenharmony_ci		.entry		= LIST_HEAD_INIT((name).entry),			\
120962306a36Sopenharmony_ci	}
121062306a36Sopenharmony_ci
121162306a36Sopenharmony_ci#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
121262306a36Sopenharmony_ci
121362306a36Sopenharmony_ci#define init_wait(wait)								\
121462306a36Sopenharmony_ci	do {									\
121562306a36Sopenharmony_ci		(wait)->private = current;					\
121662306a36Sopenharmony_ci		(wait)->func = autoremove_wake_function;			\
121762306a36Sopenharmony_ci		INIT_LIST_HEAD(&(wait)->entry);					\
121862306a36Sopenharmony_ci		(wait)->flags = 0;						\
121962306a36Sopenharmony_ci	} while (0)
122062306a36Sopenharmony_ci
122162306a36Sopenharmony_citypedef int (*task_call_f)(struct task_struct *p, void *arg);
122262306a36Sopenharmony_ciextern int task_call_func(struct task_struct *p, task_call_f func, void *arg);
122362306a36Sopenharmony_ci
122462306a36Sopenharmony_ci#endif /* _LINUX_WAIT_H */
1225