wait.h revision 335424
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 335424 2018-06-20 06:48:41Z 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
50struct wait_queue;
51struct wait_queue_head;
52
53typedef struct wait_queue wait_queue_t;
54typedef struct wait_queue_head wait_queue_head_t;
55
56typedef int wait_queue_func_t(wait_queue_t *, unsigned int, int, void *);
57
58/*
59 * Many API consumers directly reference these fields and those of
60 * wait_queue_head.
61 */
62struct wait_queue {
63	unsigned int flags;	/* always 0 */
64	void *private;
65	wait_queue_func_t *func;
66	union {
67		struct list_head task_list; /* < v4.13 */
68		struct list_head entry; /* >= v4.13 */
69	};
70};
71
72struct wait_queue_head {
73	spinlock_t lock;
74	union {
75		struct list_head task_list; /* < v4.13 */
76		struct list_head head; /* >= v4.13 */
77	};
78};
79
80/*
81 * This function is referenced by at least one DRM driver, so it may not be
82 * renamed and furthermore must be the default wait queue callback.
83 */
84extern wait_queue_func_t autoremove_wake_function;
85extern wait_queue_func_t default_wake_function;
86
87#define	DEFINE_WAIT_FUNC(name, function)				\
88	wait_queue_t name = {						\
89		.private = current,					\
90		.func = function,					\
91		.task_list = LINUX_LIST_HEAD_INIT(name.task_list)	\
92	}
93
94#define	DEFINE_WAIT(name) \
95	DEFINE_WAIT_FUNC(name, autoremove_wake_function)
96
97#define	DECLARE_WAITQUEUE(name, task)					\
98	wait_queue_t name = {						\
99		.private = task,					\
100		.task_list = LINUX_LIST_HEAD_INIT(name.task_list)	\
101	}
102
103#define	DECLARE_WAIT_QUEUE_HEAD(name)					\
104	wait_queue_head_t name = {					\
105		.task_list = LINUX_LIST_HEAD_INIT(name.task_list),	\
106	};								\
107	MTX_SYSINIT(name, &(name).lock.m, spin_lock_name("wqhead"), MTX_DEF)
108
109#define	init_waitqueue_head(wqh) do {					\
110	mtx_init(&(wqh)->lock.m, spin_lock_name("wqhead"),		\
111	    NULL, MTX_DEF | MTX_NEW | MTX_NOWITNESS);			\
112	INIT_LIST_HEAD(&(wqh)->task_list);				\
113} while (0)
114
115void linux_init_wait_entry(wait_queue_t *, int);
116void linux_wake_up(wait_queue_head_t *, unsigned int, int, bool);
117
118#define	init_wait_entry(wq, flags)					\
119        linux_init_wait_entry(wq, flags)
120#define	wake_up(wqh)							\
121	linux_wake_up(wqh, TASK_NORMAL, 1, false)
122#define	wake_up_all(wqh)						\
123	linux_wake_up(wqh, TASK_NORMAL, 0, false)
124#define	wake_up_locked(wqh)						\
125	linux_wake_up(wqh, TASK_NORMAL, 1, true)
126#define	wake_up_all_locked(wqh)						\
127	linux_wake_up(wqh, TASK_NORMAL, 0, true)
128#define	wake_up_interruptible(wqh)					\
129	linux_wake_up(wqh, TASK_INTERRUPTIBLE, 1, false)
130#define	wake_up_interruptible_all(wqh)					\
131	linux_wake_up(wqh, TASK_INTERRUPTIBLE, 0, false)
132
133int linux_wait_event_common(wait_queue_head_t *, wait_queue_t *, int,
134    unsigned int, spinlock_t *);
135
136/*
137 * Returns -ERESTARTSYS for a signal, 0 if cond is false after timeout, 1 if
138 * cond is true after timeout, remaining jiffies (> 0) if cond is true before
139 * timeout.
140 */
141#define	__wait_event_common(wqh, cond, timeout, state, lock) ({	\
142	DEFINE_WAIT(__wq);					\
143	const int __timeout = ((int)(timeout)) < 1 ? 1 : (timeout);	\
144	int __start = ticks;					\
145	int __ret = 0;						\
146								\
147	for (;;) {						\
148		linux_prepare_to_wait(&(wqh), &__wq, state);	\
149		if (cond)					\
150			break;					\
151		__ret = linux_wait_event_common(&(wqh), &__wq,	\
152		    __timeout, state, lock);			\
153		if (__ret != 0)					\
154			break;					\
155	}							\
156	linux_finish_wait(&(wqh), &__wq);			\
157	if (__timeout != MAX_SCHEDULE_TIMEOUT) {		\
158		if (__ret == -EWOULDBLOCK)			\
159			__ret = !!(cond);			\
160		else if (__ret != -ERESTARTSYS) {		\
161			__ret = __timeout + __start - ticks;	\
162			/* range check return value */		\
163			if (__ret < 1)				\
164				__ret = 1;			\
165			else if (__ret > __timeout)		\
166				__ret = __timeout;		\
167		}						\
168	}							\
169	__ret;							\
170})
171
172#define	wait_event(wqh, cond) do {					\
173	(void) __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,	\
174	    TASK_UNINTERRUPTIBLE, NULL);				\
175} while (0)
176
177#define	wait_event_timeout(wqh, cond, timeout) ({			\
178	__wait_event_common(wqh, cond, timeout, TASK_UNINTERRUPTIBLE,	\
179	    NULL);							\
180})
181
182#define	wait_event_killable(wqh, cond) ({				\
183	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,		\
184	    TASK_INTERRUPTIBLE, NULL);					\
185})
186
187#define	wait_event_interruptible(wqh, cond) ({				\
188	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,		\
189	    TASK_INTERRUPTIBLE, NULL);					\
190})
191
192#define	wait_event_interruptible_timeout(wqh, cond, timeout) ({		\
193	__wait_event_common(wqh, cond, timeout, TASK_INTERRUPTIBLE,	\
194	    NULL);							\
195})
196
197/*
198 * Wait queue is already locked.
199 */
200#define	wait_event_interruptible_locked(wqh, cond) ({			\
201	int __ret;							\
202									\
203	spin_unlock(&(wqh).lock);					\
204	__ret = __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,	\
205	    TASK_INTERRUPTIBLE, NULL);					\
206	spin_lock(&(wqh).lock);						\
207	__ret;								\
208})
209
210/*
211 * The passed spinlock is held when testing the condition.
212 */
213#define	wait_event_interruptible_lock_irq(wqh, cond, lock) ({		\
214	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,		\
215	    TASK_INTERRUPTIBLE, &(lock));				\
216})
217
218/*
219 * The passed spinlock is held when testing the condition.
220 */
221#define	wait_event_lock_irq(wqh, cond, lock) ({			\
222	__wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT,	\
223	    TASK_UNINTERRUPTIBLE, &(lock));			\
224})
225
226static inline void
227__add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
228{
229	list_add(&wq->task_list, &wqh->task_list);
230}
231
232static inline void
233add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
234{
235
236	spin_lock(&wqh->lock);
237	__add_wait_queue(wqh, wq);
238	spin_unlock(&wqh->lock);
239}
240
241static inline void
242__add_wait_queue_tail(wait_queue_head_t *wqh, wait_queue_t *wq)
243{
244	list_add_tail(&wq->task_list, &wqh->task_list);
245}
246
247static inline void
248__remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
249{
250	list_del(&wq->task_list);
251}
252
253static inline void
254remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
255{
256
257	spin_lock(&wqh->lock);
258	__remove_wait_queue(wqh, wq);
259	spin_unlock(&wqh->lock);
260}
261
262bool linux_waitqueue_active(wait_queue_head_t *);
263
264#define	waitqueue_active(wqh)		linux_waitqueue_active(wqh)
265
266void linux_prepare_to_wait(wait_queue_head_t *, wait_queue_t *, int);
267void linux_finish_wait(wait_queue_head_t *, wait_queue_t *);
268
269#define	prepare_to_wait(wqh, wq, state)	linux_prepare_to_wait(wqh, wq, state)
270#define	finish_wait(wqh, wq)		linux_finish_wait(wqh, wq)
271
272void linux_wake_up_bit(void *, int);
273int linux_wait_on_bit_timeout(unsigned long *, int, unsigned int, int);
274void linux_wake_up_atomic_t(atomic_t *);
275int linux_wait_on_atomic_t(atomic_t *, unsigned int);
276
277#define	wake_up_bit(word, bit)		linux_wake_up_bit(word, bit)
278#define	wait_on_bit(word, bit, state)					\
279	linux_wait_on_bit_timeout(word, bit, state, MAX_SCHEDULE_TIMEOUT)
280#define	wait_on_bit_timeout(word, bit, state, timeout)			\
281	linux_wait_on_bit_timeout(word, bit, state, timeout)
282#define	wake_up_atomic_t(a)		linux_wake_up_atomic_t(a)
283/*
284 * All existing callers have a cb that just schedule()s. To avoid adding
285 * complexity, just emulate that internally. The prototype is different so that
286 * callers must be manually modified; a cb that does something other than call
287 * schedule() will require special treatment.
288 */
289#define	wait_on_atomic_t(a, state)	linux_wait_on_atomic_t(a, state)
290
291struct task_struct;
292bool linux_wake_up_state(struct task_struct *, unsigned int);
293
294#define	wake_up_process(task)		linux_wake_up_state(task, TASK_NORMAL)
295#define	wake_up_state(task, state)	linux_wake_up_state(task, state)
296
297#endif /* _LINUX_WAIT_H_ */
298