xref: /freebsd/sys/compat/linuxkpi/common/include/linux/wait.h (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
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  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUX_WAIT_H_
32 #define	_LINUX_WAIT_H_
33 
34 #include <linux/spinlock.h>
35 #include <linux/sched.h>
36 #include <linux/list.h>
37 #include <linux/jiffies.h>
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sleepqueue.h>
42 #include <sys/kernel.h>
43 #include <sys/proc.h>
44 
45 typedef struct {
46 } wait_queue_t;
47 
48 typedef struct {
49 	unsigned int	wchan;
50 } wait_queue_head_t;
51 
52 #define	init_waitqueue_head(x) \
53     do { } while (0)
54 
55 static inline void
56 __wake_up(wait_queue_head_t *q, int all)
57 {
58 	int wakeup_swapper;
59 	void *c;
60 
61 	c = &q->wchan;
62 	sleepq_lock(c);
63 	if (all)
64 		wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
65 	else
66 		wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
67 	sleepq_release(c);
68 	if (wakeup_swapper)
69 		kick_proc0();
70 }
71 
72 #define	wake_up(q)				__wake_up(q, 0)
73 #define	wake_up_nr(q, nr)			__wake_up(q, 1)
74 #define	wake_up_all(q)				__wake_up(q, 1)
75 #define	wake_up_interruptible(q)		__wake_up(q, 0)
76 #define	wake_up_interruptible_nr(q, nr)		__wake_up(q, 1)
77 #define	wake_up_interruptible_all(q, nr)	__wake_up(q, 1)
78 
79 #define	wait_event(q, cond)						\
80 do {									\
81 	void *c = &(q).wchan;						\
82 	if (!(cond)) {							\
83 		for (;;) {						\
84 			sleepq_lock(c);					\
85 			if (cond) {					\
86 				sleepq_release(c);			\
87 				break;					\
88 			}						\
89 			sleepq_add(c, NULL, "completion", SLEEPQ_SLEEP, 0); \
90 			sleepq_wait(c, 0);				\
91 		}							\
92 	}								\
93 } while (0)
94 
95 #define	wait_event_interruptible(q, cond)				\
96 ({									\
97 	void *c = &(q).wchan;						\
98 	int _error;							\
99 									\
100 	_error = 0;							\
101 	if (!(cond)) {							\
102 		for (; _error == 0;) {					\
103 			sleepq_lock(c);					\
104 			if (cond) {					\
105 				sleepq_release(c);			\
106 				break;					\
107 			}						\
108 			sleepq_add(c, NULL, "completion",		\
109 			    SLEEPQ_SLEEP | SLEEPQ_INTERRUPTIBLE, 0);	\
110 			if (sleepq_wait_sig(c, 0))			\
111 				_error = -ERESTARTSYS;			\
112 		}							\
113 	}								\
114 	-_error;							\
115 })
116 
117 #define	wait_event_interruptible_timeout(q, cond, timeout)		\
118 ({									\
119 	void *c = &(q).wchan;						\
120 	long end = jiffies + timeout;					\
121 	int __ret = 0;	 						\
122 	int __rc = 0;							\
123 									\
124 	if (!(cond)) {							\
125 		for (; __rc == 0;) {					\
126 			sleepq_lock(c);					\
127 			if (cond) {					\
128 				sleepq_release(c);			\
129 				__ret = 1;				\
130 				break;					\
131 			}						\
132 			sleepq_add(c, NULL, "completion",		\
133 			SLEEPQ_SLEEP | SLEEPQ_INTERRUPTIBLE, 0);	\
134 			sleepq_set_timeout(c, linux_timer_jiffies_until(end));\
135 			__rc = sleepq_timedwait_sig (c, 0);		\
136 			if (__rc != 0) {				\
137 				/* check for timeout or signal. 	\
138 				 * 0 if the condition evaluated to false\
139 				 * after the timeout elapsed,  1 if the \
140 				 * condition evaluated to true after the\
141 				 * timeout elapsed.			\
142 				 */					\
143 				if (__rc == EWOULDBLOCK)		\
144 					__ret = (cond);			\
145 				 else					\
146 					__ret = -ERESTARTSYS;		\
147 			}						\
148 									\
149 		}							\
150 	} else {							\
151 		/* return remaining jiffies (at least 1) if the 	\
152 		 * condition evaluated to true before the timeout	\
153 		 * elapsed.						\
154 		 */							\
155 		__ret = (end - jiffies);				\
156 		if( __ret < 1 )						\
157 			__ret = 1;					\
158 	}								\
159 	__ret;								\
160 })
161 
162 
163 static inline int
164 waitqueue_active(wait_queue_head_t *q)
165 {
166 	return 0;	/* XXX: not really implemented */
167 }
168 
169 #define DEFINE_WAIT(name)	\
170 	wait_queue_t name = {}
171 
172 static inline void
173 prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
174 {
175 }
176 
177 static inline void
178 finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
179 {
180 }
181 
182 #endif	/* _LINUX_WAIT_H_ */
183