wait.h revision 335425
1/*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6 * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice unmodified, this list of conditions, and the following
14 *    disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/wait.h 335425 2018-06-20 06:49:26Z hselasky $
31 */
32
33#ifndef _LINUX_WAIT_H_
34#define	_LINUX_WAIT_H_
35
36#include <linux/compiler.h>
37#include <linux/list.h>
38#include <linux/spinlock.h>
39
40#include <asm/atomic.h>
41
42#include <sys/param.h>
43#include <sys/systm.h>
44
45#define	SKIP_SLEEP() (SCHEDULER_STOPPED() || kdb_active)
46
47#define	might_sleep()							\
48	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "might_sleep()")
49
50#define	might_sleep_if(cond) do { \
51	if (cond) { might_sleep(); } \
52} while (0)
53
54struct wait_queue;
55struct wait_queue_head;
56
57typedef struct wait_queue wait_queue_t;
58typedef struct wait_queue_head wait_queue_head_t;
59
60typedef int wait_queue_func_t(wait_queue_t *, unsigned int, int, void *);
61
62/*
63 * Many API consumers directly reference these fields and those of
64 * wait_queue_head.
65 */
66struct wait_queue {
67	unsigned int flags;	/* always 0 */
68	void *private;
69	wait_queue_func_t *func;
70	union {
71		struct list_head task_list; /* < v4.13 */
72		struct list_head entry; /* >= v4.13 */
73	};
74};
75
76struct wait_queue_head {
77	spinlock_t lock;
78	union {
79		struct list_head task_list; /* < v4.13 */
80		struct list_head head; /* >= v4.13 */
81	};
82};
83
84/*
85 * This function is referenced by at least one DRM driver, so it may not be
86 * renamed and furthermore must be the default wait queue callback.
87 */
88extern wait_queue_func_t autoremove_wake_function;
89extern wait_queue_func_t default_wake_function;
90
91#define	DEFINE_WAIT_FUNC(name, function)				\
92	wait_queue_t name = {						\
93		.private = current,					\
94		.func = function,					\
95		.task_list = LINUX_LIST_HEAD_INIT(name.task_list)	\
96	}
97
98#define	DEFINE_WAIT(name) \
99	DEFINE_WAIT_FUNC(name, autoremove_wake_function)
100
101#define	DECLARE_WAITQUEUE(name, task)					\
102	wait_queue_t name = {						\
103		.private = task,					\
104		.task_list = LINUX_LIST_HEAD_INIT(name.task_list)	\
105	}
106
107#define	DECLARE_WAIT_QUEUE_HEAD(name)					\
108	wait_queue_head_t name = {					\
109		.task_list = LINUX_LIST_HEAD_INIT(name.task_list),	\
110	};								\
111	MTX_SYSINIT(name, &(name).lock.m, spin_lock_name("wqhead"), MTX_DEF)
112
113#define	init_waitqueue_head(wqh) do {					\
114	mtx_init(&(wqh)->lock.m, spin_lock_name("wqhead"),		\
115	    NULL, MTX_DEF | MTX_NEW | MTX_NOWITNESS);			\
116	INIT_LIST_HEAD(&(wqh)->task_list);				\
117} while (0)
118
119void linux_init_wait_entry(wait_queue_t *, int);
120void linux_wake_up(wait_queue_head_t *, unsigned int, int, bool);
121
122#define	init_wait_entry(wq, flags)					\
123        linux_init_wait_entry(wq, flags)
124#define	wake_up(wqh)							\
125	linux_wake_up(wqh, TASK_NORMAL, 1, false)
126#define	wake_up_all(wqh)						\
127	linux_wake_up(wqh, TASK_NORMAL, 0, false)
128#define	wake_up_locked(wqh)						\
129	linux_wake_up(wqh, TASK_NORMAL, 1, true)
130#define	wake_up_all_locked(wqh)						\
131	linux_wake_up(wqh, TASK_NORMAL, 0, true)
132#define	wake_up_interruptible(wqh)					\
133	linux_wake_up(wqh, TASK_INTERRUPTIBLE, 1, false)
134#define	wake_up_interruptible_all(wqh)					\
135	linux_wake_up(wqh, TASK_INTERRUPTIBLE, 0, false)
136
137int linux_wait_event_common(wait_queue_head_t *, wait_queue_t *, int,
138    unsigned int, spinlock_t *);
139
140/*
141 * Returns -ERESTARTSYS for a signal, 0 if cond is false after timeout, 1 if
142 * cond is true after timeout, remaining jiffies (> 0) if cond is true before
143 * timeout.
144 */
145#define	__wait_event_common(wqh, cond, timeout, state, lock) ({	\
146	DEFINE_WAIT(__wq);					\
147	const int __timeout = ((int)(timeout)) < 1 ? 1 : (timeout);	\
148	int __start = ticks;					\
149	int __ret = 0;						\
150								\
151	for (;;) {						\
152		linux_prepare_to_wait(&(wqh), &__wq, state);	\
153		if (cond)					\
154			break;					\
155		__ret = linux_wait_event_common(&(wqh), &__wq,	\
156		    __timeout, state, lock);			\
157		if (__ret != 0)					\
158			break;					\
159	}							\
160	linux_finish_wait(&(wqh), &__wq);			\
161	if (__timeout != MAX_SCHEDULE_TIMEOUT) {		\
162		if (__ret == -EWOULDBLOCK)			\
163			__ret = !!(cond);			\
164		else if (__ret != -ERESTARTSYS) {		\
165			__ret = __timeout + __start - ticks;	\
166			/* range check return value */		\
167			if (__ret < 1)				\
168				__ret = 1;			\
169			else if (__ret > __timeout)		\
170				__ret = __timeout;		\
171		}						\
172	}							\
173	__ret;							\
174})
175
176#define	wait_event(wqh, cond) do {					\
177	(void) __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,	\
178	    TASK_UNINTERRUPTIBLE, NULL);				\
179} while (0)
180
181#define	wait_event_timeout(wqh, cond, timeout) ({			\
182	__wait_event_common(wqh, cond, timeout, TASK_UNINTERRUPTIBLE,	\
183	    NULL);							\
184})
185
186#define	wait_event_killable(wqh, cond) ({				\
187	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,		\
188	    TASK_INTERRUPTIBLE, NULL);					\
189})
190
191#define	wait_event_interruptible(wqh, cond) ({				\
192	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,		\
193	    TASK_INTERRUPTIBLE, NULL);					\
194})
195
196#define	wait_event_interruptible_timeout(wqh, cond, timeout) ({		\
197	__wait_event_common(wqh, cond, timeout, TASK_INTERRUPTIBLE,	\
198	    NULL);							\
199})
200
201/*
202 * Wait queue is already locked.
203 */
204#define	wait_event_interruptible_locked(wqh, cond) ({			\
205	int __ret;							\
206									\
207	spin_unlock(&(wqh).lock);					\
208	__ret = __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,	\
209	    TASK_INTERRUPTIBLE, NULL);					\
210	spin_lock(&(wqh).lock);						\
211	__ret;								\
212})
213
214/*
215 * The passed spinlock is held when testing the condition.
216 */
217#define	wait_event_interruptible_lock_irq(wqh, cond, lock) ({		\
218	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,		\
219	    TASK_INTERRUPTIBLE, &(lock));				\
220})
221
222/*
223 * The passed spinlock is held when testing the condition.
224 */
225#define	wait_event_lock_irq(wqh, cond, lock) ({			\
226	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,	\
227	    TASK_UNINTERRUPTIBLE, &(lock));			\
228})
229
230static inline void
231__add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
232{
233	list_add(&wq->task_list, &wqh->task_list);
234}
235
236static inline void
237add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
238{
239
240	spin_lock(&wqh->lock);
241	__add_wait_queue(wqh, wq);
242	spin_unlock(&wqh->lock);
243}
244
245static inline void
246__add_wait_queue_tail(wait_queue_head_t *wqh, wait_queue_t *wq)
247{
248	list_add_tail(&wq->task_list, &wqh->task_list);
249}
250
251static inline void
252__remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
253{
254	list_del(&wq->task_list);
255}
256
257static inline void
258remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
259{
260
261	spin_lock(&wqh->lock);
262	__remove_wait_queue(wqh, wq);
263	spin_unlock(&wqh->lock);
264}
265
266bool linux_waitqueue_active(wait_queue_head_t *);
267
268#define	waitqueue_active(wqh)		linux_waitqueue_active(wqh)
269
270void linux_prepare_to_wait(wait_queue_head_t *, wait_queue_t *, int);
271void linux_finish_wait(wait_queue_head_t *, wait_queue_t *);
272
273#define	prepare_to_wait(wqh, wq, state)	linux_prepare_to_wait(wqh, wq, state)
274#define	finish_wait(wqh, wq)		linux_finish_wait(wqh, wq)
275
276void linux_wake_up_bit(void *, int);
277int linux_wait_on_bit_timeout(unsigned long *, int, unsigned int, int);
278void linux_wake_up_atomic_t(atomic_t *);
279int linux_wait_on_atomic_t(atomic_t *, unsigned int);
280
281#define	wake_up_bit(word, bit)		linux_wake_up_bit(word, bit)
282#define	wait_on_bit(word, bit, state)					\
283	linux_wait_on_bit_timeout(word, bit, state, MAX_SCHEDULE_TIMEOUT)
284#define	wait_on_bit_timeout(word, bit, state, timeout)			\
285	linux_wait_on_bit_timeout(word, bit, state, timeout)
286#define	wake_up_atomic_t(a)		linux_wake_up_atomic_t(a)
287/*
288 * All existing callers have a cb that just schedule()s. To avoid adding
289 * complexity, just emulate that internally. The prototype is different so that
290 * callers must be manually modified; a cb that does something other than call
291 * schedule() will require special treatment.
292 */
293#define	wait_on_atomic_t(a, state)	linux_wait_on_atomic_t(a, state)
294
295struct task_struct;
296bool linux_wake_up_state(struct task_struct *, unsigned int);
297
298#define	wake_up_process(task)		linux_wake_up_state(task, TASK_NORMAL)
299#define	wake_up_state(task, state)	linux_wake_up_state(task, state)
300
301#endif /* _LINUX_WAIT_H_ */
302