xref: /dragonfly/sys/dev/drm/include/linux/wait.h (revision 5ca0a96d)
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_entry wait_queue_entry_t;
37 
38 typedef int (*wait_queue_func_t)(wait_queue_entry_t *wait, unsigned mode, int flags, void *key);
39 
40 int default_wake_function(wait_queue_entry_t *wait, unsigned mode, int flags, void *key);
41 int autoremove_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key);
42 
43 struct wait_queue_entry {
44 	unsigned int flags;
45 	void *private;
46 	wait_queue_func_t func;
47 	struct list_head entry;
48 };
49 
50 void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
51 
52 typedef struct {
53 	struct lock		lock;
54 	struct list_head	head;
55 } wait_queue_head_t;
56 
57 void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
58 
59 static inline void
60 init_waitqueue_head(wait_queue_head_t *q)
61 {
62 	__init_waitqueue_head(q, "", NULL);
63 }
64 
65 void __wake_up_core(wait_queue_head_t *q, int num_to_wake_up);
66 
67 static inline void
68 wake_up(wait_queue_head_t *q)
69 {
70 	lockmgr(&q->lock, LK_EXCLUSIVE);
71 	__wake_up_core(q, 1);
72 	lockmgr(&q->lock, LK_RELEASE);
73 	wakeup_one(q);
74 }
75 
76 static inline void
77 wake_up_all(wait_queue_head_t *q)
78 {
79 	lockmgr(&q->lock, LK_EXCLUSIVE);
80 	__wake_up_core(q, 0);
81 	lockmgr(&q->lock, LK_RELEASE);
82 	wakeup(q);
83 }
84 
85 void wake_up_bit(void *, int);
86 
87 #define wake_up_all_locked(eq)		__wake_up_core(eq, 0)
88 
89 #define wake_up_interruptible(eq)	wake_up(eq)
90 #define wake_up_interruptible_all(eq)	wake_up_all(eq)
91 
92 void __wait_event_prefix(wait_queue_head_t *wq, int flags);
93 void prepare_to_wait(wait_queue_head_t *q, wait_queue_entry_t *wait, int state);
94 void finish_wait(wait_queue_head_t *q, wait_queue_entry_t *wait);
95 
96 /*
97  * wait_event_interruptible_timeout:
98  * - The process is put to sleep until the condition evaluates to true.
99  * - The condition is checked each time the waitqueue wq is woken up.
100  * - wake_up has to be called after changing any variable that could change
101  * the result of the wait condition.
102  *
103  * returns:
104  *   - 0 if the timeout elapsed
105  *   - the remaining jiffies if the condition evaluated to true before
106  *   the timeout elapsed.
107  *   - remaining jiffies are always at least 1
108  *   - -ERESTARTSYS if interrupted by a signal (when PCATCH is set in flags)
109 */
110 #define __wait_event_common(wq, condition, timeout_jiffies, flags,	\
111 			    locked)					\
112 ({									\
113 	int start_jiffies, elapsed_jiffies, remaining_jiffies, ret;	\
114 	bool timeout_expired = false;					\
115 	bool interrupted = false;					\
116 	long retval;							\
117 	int state;							\
118 	DEFINE_WAIT(tmp_wq);						\
119 									\
120 	start_jiffies = ticks;						\
121 	state = (flags & PCATCH) ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE; \
122 	prepare_to_wait(&wq, &tmp_wq, state);				\
123 									\
124 	while (1) {							\
125 		__wait_event_prefix(&wq, flags);			\
126 									\
127 		if (condition)						\
128 			break;						\
129 									\
130 		tsleep_interlock(current, flags);			\
131 									\
132 		if ((timeout_jiffies) != 0) {				\
133 			ret = tsleep(current, PINTERLOCKED|flags, "lwe", timeout_jiffies);	\
134 		} else {						\
135 			ret = tsleep(current, PINTERLOCKED|flags, "lwe", hz);\
136 			if (ret == EWOULDBLOCK) {			\
137 				kprintf("F");				\
138 				print_backtrace(-1);			\
139 				ret = 0;				\
140 			}						\
141 		}							\
142 									\
143 		if (ret == EINTR || ret == ERESTART) {			\
144 			interrupted = true;				\
145 			break;						\
146 		}							\
147 		if (ret == EWOULDBLOCK) {				\
148 			timeout_expired = true;				\
149 			break;						\
150 		}							\
151 	}								\
152 									\
153 	elapsed_jiffies = ticks - start_jiffies;			\
154 	remaining_jiffies = timeout_jiffies - elapsed_jiffies;		\
155 	if (remaining_jiffies <= 0)					\
156 		remaining_jiffies = 1;					\
157 									\
158 	if (timeout_expired)						\
159 		retval = 0;						\
160 	else if (interrupted)						\
161 		retval = -ERESTARTSYS;					\
162 	else if (timeout_jiffies > 0)					\
163 		retval = remaining_jiffies;				\
164 	else								\
165 		retval = 1;						\
166 									\
167 	finish_wait(&wq, &tmp_wq);					\
168 	retval;								\
169 })
170 
171 #define wait_event(wq, condition)					\
172 		__wait_event_common(wq, condition, 0, 0, false)
173 
174 #define wait_event_timeout(wq, condition, timeout)			\
175 		__wait_event_common(wq, condition, timeout, 0, false)
176 
177 #define wait_event_interruptible(wq, condition)				\
178 ({									\
179 	long retval;							\
180 									\
181 	retval = __wait_event_common(wq, condition, 0, PCATCH, false);	\
182 	if (retval != -ERESTARTSYS)					\
183 		retval = 0;						\
184 	retval;								\
185 })
186 
187 #define wait_event_interruptible_locked(wq, condition)			\
188 ({									\
189 	long retval;							\
190 									\
191 	retval = __wait_event_common(wq, condition, 0, PCATCH, true);	\
192 	if (retval != -ERESTARTSYS)					\
193 		retval = 0;						\
194 	retval;								\
195 })
196 
197 #define wait_event_interruptible_timeout(wq, condition, timeout)	\
198 		__wait_event_common(wq, condition, timeout, PCATCH, false)
199 
200 static inline int
201 waitqueue_active(wait_queue_head_t *q)
202 {
203 	return !list_empty(&q->head);
204 }
205 
206 #define DEFINE_WAIT_FUNC(name, _function)			\
207 	wait_queue_entry_t name = {				\
208 		.private = current,				\
209 		.entry = LIST_HEAD_INIT((name).entry),	\
210 		.func = _function,				\
211 	}
212 
213 #define DEFINE_WAIT(name)	\
214 	DEFINE_WAIT_FUNC((name), autoremove_wake_function)
215 
216 static inline void
217 __add_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *new)
218 {
219 	list_add(&new->entry, &head->head);
220 }
221 
222 static inline void
223 add_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *wait)
224 {
225 	lockmgr(&head->lock, LK_EXCLUSIVE);
226 	__add_wait_queue(head, wait);
227 	lockmgr(&head->lock, LK_RELEASE);
228 }
229 
230 #define DECLARE_WAIT_QUEUE_HEAD(name)					\
231 	wait_queue_head_t name = {					\
232 		.lock = LOCK_INITIALIZER("name", 0, LK_CANRECURSE),	\
233 		.head = { &(name).head, &(name).head }	\
234 	}
235 
236 static inline void
237 __remove_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *old)
238 {
239 	list_del(&old->entry);
240 }
241 
242 static inline void
243 remove_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *wq)
244 {
245 	lockmgr(&head->lock, LK_EXCLUSIVE);
246 	__remove_wait_queue(head, wq);
247 	lockmgr(&head->lock, LK_RELEASE);
248 }
249 
250 static inline void
251 __add_wait_queue_entry_tail(wait_queue_head_t *wqh, wait_queue_entry_t *wq)
252 {
253 	list_add_tail(&wq->entry, &wqh->head);
254 }
255 
256 #endif	/* _LINUX_WAIT_H_ */
257