xref: /dragonfly/sys/dev/drm/include/linux/wait.h (revision 66fa3dc1)
1 /*
2  * Copyright (c) 2014 Imre Vadász
3  * Copyright (c) 2014-2020 François Tigeot <ftigeot@wolfpond.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef _LINUX_WAIT_H_
29 #define _LINUX_WAIT_H_
30 
31 #include <linux/list.h>
32 #include <linux/stddef.h>
33 #include <linux/spinlock.h>
34 #include <asm/current.h>
35 
36 typedef struct __wait_queue wait_queue_t;
37 
38 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
39 
40 int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
41 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
42 
43 struct __wait_queue {
44 	unsigned int flags;
45 	void *private;
46 	wait_queue_func_t func;
47 	struct list_head task_list;
48 };
49 
50 typedef struct {
51 	struct lock		lock;
52 	struct list_head	task_list;
53 } wait_queue_head_t;
54 
55 void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
56 
57 static inline void
58 init_waitqueue_head(wait_queue_head_t *q)
59 {
60 	__init_waitqueue_head(q, "", NULL);
61 }
62 
63 void __wake_up_core(wait_queue_head_t *q, int num_to_wake_up);
64 
65 static inline void
66 wake_up(wait_queue_head_t *q)
67 {
68 	lockmgr(&q->lock, LK_EXCLUSIVE);
69 	__wake_up_core(q, 1);
70 	lockmgr(&q->lock, LK_RELEASE);
71 	wakeup_one(q);
72 }
73 
74 static inline void
75 wake_up_all(wait_queue_head_t *q)
76 {
77 	lockmgr(&q->lock, LK_EXCLUSIVE);
78 	__wake_up_core(q, 0);
79 	lockmgr(&q->lock, LK_RELEASE);
80 	wakeup(q);
81 }
82 
83 void wake_up_bit(void *, int);
84 
85 #define wake_up_all_locked(eq)		__wake_up_core(eq, 0)
86 
87 #define wake_up_interruptible(eq)	wake_up(eq)
88 #define wake_up_interruptible_all(eq)	wake_up_all(eq)
89 
90 void __wait_event_prefix(wait_queue_head_t *wq, int flags);
91 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
92 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
93 
94 /*
95  * wait_event_interruptible_timeout:
96  * - The process is put to sleep until the condition evaluates to true.
97  * - The condition is checked each time the waitqueue wq is woken up.
98  * - wake_up has to be called after changing any variable that could change
99  * the result of the wait condition.
100  *
101  * returns:
102  *   - 0 if the timeout elapsed
103  *   - the remaining jiffies if the condition evaluated to true before
104  *   the timeout elapsed.
105  *   - remaining jiffies are always at least 1
106  *   - -ERESTARTSYS if interrupted by a signal (when PCATCH is set in flags)
107 */
108 #define __wait_event_common(wq, condition, timeout_jiffies, flags,	\
109 			    locked)					\
110 ({									\
111 	int start_jiffies, elapsed_jiffies, remaining_jiffies, ret;	\
112 	bool timeout_expired = false;					\
113 	bool interrupted = false;					\
114 	long retval;							\
115 	int state;							\
116 	DEFINE_WAIT(tmp_wq);						\
117 									\
118 	start_jiffies = ticks;						\
119 	state = (flags & PCATCH) ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE; \
120 	prepare_to_wait(&wq, &tmp_wq, state);				\
121 									\
122 	while (1) {							\
123 		__wait_event_prefix(&wq, flags);			\
124 									\
125 		if (condition)						\
126 			break;						\
127 									\
128 		tsleep_interlock(current, flags);			\
129 									\
130 		if ((timeout_jiffies) != 0) {				\
131 			ret = tsleep(current, PINTERLOCKED|flags, "lwe", timeout_jiffies);	\
132 		} else {						\
133 			ret = tsleep(current, PINTERLOCKED|flags, "lwe", hz);\
134 			if (ret == EWOULDBLOCK) {			\
135 				kprintf("F");				\
136 				print_backtrace(-1);			\
137 				ret = 0;				\
138 			}						\
139 		}							\
140 									\
141 		if (ret == EINTR || ret == ERESTART) {			\
142 			interrupted = true;				\
143 			break;						\
144 		}							\
145 		if (ret == EWOULDBLOCK) {				\
146 			timeout_expired = true;				\
147 			break;						\
148 		}							\
149 	}								\
150 									\
151 	elapsed_jiffies = ticks - start_jiffies;			\
152 	remaining_jiffies = timeout_jiffies - elapsed_jiffies;		\
153 	if (remaining_jiffies <= 0)					\
154 		remaining_jiffies = 1;					\
155 									\
156 	if (timeout_expired)						\
157 		retval = 0;						\
158 	else if (interrupted)						\
159 		retval = -ERESTARTSYS;					\
160 	else if (timeout_jiffies > 0)					\
161 		retval = remaining_jiffies;				\
162 	else								\
163 		retval = 1;						\
164 									\
165 	finish_wait(&wq, &tmp_wq);					\
166 	retval;								\
167 })
168 
169 #define wait_event(wq, condition)					\
170 		__wait_event_common(wq, condition, 0, 0, false)
171 
172 #define wait_event_timeout(wq, condition, timeout)			\
173 		__wait_event_common(wq, condition, timeout, 0, false)
174 
175 #define wait_event_interruptible(wq, condition)				\
176 ({									\
177 	long retval;							\
178 									\
179 	retval = __wait_event_common(wq, condition, 0, PCATCH, false);	\
180 	if (retval != -ERESTARTSYS)					\
181 		retval = 0;						\
182 	retval;								\
183 })
184 
185 #define wait_event_interruptible_locked(wq, condition)			\
186 ({									\
187 	long retval;							\
188 									\
189 	retval = __wait_event_common(wq, condition, 0, PCATCH, true);	\
190 	if (retval != -ERESTARTSYS)					\
191 		retval = 0;						\
192 	retval;								\
193 })
194 
195 #define wait_event_interruptible_timeout(wq, condition, timeout)	\
196 		__wait_event_common(wq, condition, timeout, PCATCH, false)
197 
198 static inline int
199 waitqueue_active(wait_queue_head_t *q)
200 {
201 	return !list_empty(&q->task_list);
202 }
203 
204 #define DEFINE_WAIT(name)					\
205 	wait_queue_t name = {					\
206 		.private = current,				\
207 		.task_list = LIST_HEAD_INIT((name).task_list),	\
208 		.func = autoremove_wake_function,		\
209 	}
210 
211 #define DEFINE_WAIT_FUNC(name, _function)			\
212 	wait_queue_t name = {					\
213 		.private = current,				\
214 		.task_list = LIST_HEAD_INIT((name).task_list),	\
215 		.func = _function,				\
216 	}
217 
218 static inline void
219 __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
220 {
221 	list_add(&new->task_list, &head->task_list);
222 }
223 
224 static inline void
225 add_wait_queue(wait_queue_head_t *head, wait_queue_t *wq)
226 {
227 	lockmgr(&head->lock, LK_EXCLUSIVE);
228 	__add_wait_queue(head, wq);
229 	lockmgr(&head->lock, LK_RELEASE);
230 }
231 
232 #define DECLARE_WAIT_QUEUE_HEAD(name)					\
233 	wait_queue_head_t name = {					\
234 		.lock = LOCK_INITIALIZER("name", 0, LK_CANRECURSE),	\
235 		.task_list = { &(name).task_list, &(name).task_list }	\
236 	}
237 
238 static inline void
239 __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
240 {
241 	list_del(&old->task_list);
242 }
243 
244 static inline void
245 remove_wait_queue(wait_queue_head_t *head, wait_queue_t *wq)
246 {
247 	lockmgr(&head->lock, LK_EXCLUSIVE);
248 	__remove_wait_queue(head, wq);
249 	lockmgr(&head->lock, LK_RELEASE);
250 }
251 
252 static inline void
253 __add_wait_queue_tail(wait_queue_head_t *wqh, wait_queue_t *wq)
254 {
255 	list_add_tail(&wq->task_list, &wqh->task_list);
256 }
257 
258 int wait_on_bit_timeout(unsigned long *word, int bit,
259 			unsigned mode, unsigned long timeout);
260 
261 #endif	/* _LINUX_WAIT_H_ */
262