xref: /dflybsd-src/sys/dev/drm/include/linux/wait.h (revision 40245fd9cad57ddb46a86213b9ecff8f6370c56b)
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 static inline void
56 init_waitqueue_head(wait_queue_head_t *eq)
57 {
58 	lockinit(&eq->lock, "lwq", 0, LK_CANRECURSE);
59 	INIT_LIST_HEAD(&eq->task_list);
60 }
61 
62 void __wake_up_core(wait_queue_head_t *q, int num_to_wake_up);
63 
64 static inline void
65 wake_up(wait_queue_head_t *q)
66 {
67 	lockmgr(&q->lock, LK_EXCLUSIVE);
68 	__wake_up_core(q, 1);
69 	lockmgr(&q->lock, LK_RELEASE);
70 	wakeup_one(q);
71 }
72 
73 static inline void
74 wake_up_all(wait_queue_head_t *q)
75 {
76 	lockmgr(&q->lock, LK_EXCLUSIVE);
77 	__wake_up_core(q, 0);
78 	lockmgr(&q->lock, LK_RELEASE);
79 	wakeup(q);
80 }
81 
82 #define wake_up_all_locked(eq)		__wake_up_core(eq, 0)
83 
84 #define wake_up_interruptible(eq)	wake_up(eq)
85 #define wake_up_interruptible_all(eq)	wake_up_all(eq)
86 
87 void __wait_event_prefix(wait_queue_head_t *wq, int flags);
88 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
89 
90 /*
91  * wait_event_interruptible_timeout:
92  * - The process is put to sleep until the condition evaluates to true.
93  * - The condition is checked each time the waitqueue wq is woken up.
94  * - wake_up has to be called after changing any variable that could change
95  * the result of the wait condition.
96  *
97  * returns:
98  *   - 0 if the timeout elapsed
99  *   - the remaining jiffies if the condition evaluated to true before
100  *   the timeout elapsed.
101  *   - remaining jiffies are always at least 1
102  *   - -ERESTARTSYS if interrupted by a signal (when PCATCH is set in flags)
103 */
104 #define __wait_event_common(wq, condition, timeout_jiffies, flags,	\
105 			    locked)					\
106 ({									\
107 	int start_jiffies, elapsed_jiffies, remaining_jiffies, ret;	\
108 	bool timeout_expired = false;					\
109 	bool interrupted = false;					\
110 	long retval;							\
111 	wait_queue_t tmp_wq;						\
112 									\
113 	start_jiffies = ticks;						\
114 									\
115 	while (1) {							\
116 		__wait_event_prefix(&wq, flags);			\
117 									\
118 		if (condition)						\
119 			break;						\
120 									\
121 		ret = tsleep(&wq, flags, "lwe", timeout_jiffies);	\
122 		if (ret == EINTR || ret == ERESTART) {			\
123 			interrupted = true;				\
124 			break;						\
125 		}							\
126 		if (ret == EWOULDBLOCK) {				\
127 			timeout_expired = true;				\
128 			break;						\
129 		}							\
130 	}								\
131 									\
132 	elapsed_jiffies = ticks - start_jiffies;			\
133 	remaining_jiffies = timeout_jiffies - elapsed_jiffies;		\
134 	if (remaining_jiffies <= 0)					\
135 		remaining_jiffies = 1;					\
136 									\
137 	if (timeout_expired)						\
138 		retval = 0;						\
139 	else if (interrupted)						\
140 		retval = -ERESTARTSYS;					\
141 	else if (timeout_jiffies > 0)					\
142 		retval = remaining_jiffies;				\
143 	else								\
144 		retval = 1;						\
145 									\
146 	finish_wait(&wq, &tmp_wq);					\
147 	retval;								\
148 })
149 
150 #define wait_event(wq, condition)					\
151 		__wait_event_common(wq, condition, 0, 0, false)
152 
153 #define wait_event_timeout(wq, condition, timeout)			\
154 		__wait_event_common(wq, condition, timeout, 0, false)
155 
156 #define wait_event_interruptible(wq, condition)				\
157 ({									\
158 	long retval;							\
159 									\
160 	retval = __wait_event_common(wq, condition, 0, PCATCH, false);	\
161 	if (retval != -ERESTARTSYS)					\
162 		retval = 0;						\
163 	retval;								\
164 })
165 
166 #define wait_event_interruptible_locked(wq, condition)			\
167 ({									\
168 	long retval;							\
169 									\
170 	retval = __wait_event_common(wq, condition, 0, PCATCH, true);	\
171 	if (retval != -ERESTARTSYS)					\
172 		retval = 0;						\
173 	retval;								\
174 })
175 
176 #define wait_event_interruptible_timeout(wq, condition, timeout)	\
177 		__wait_event_common(wq, condition, timeout, PCATCH, false)
178 
179 static inline int
180 waitqueue_active(wait_queue_head_t *q)
181 {
182 	return !list_empty(&q->task_list);
183 }
184 
185 #define DEFINE_WAIT(name)					\
186 	wait_queue_t name = {					\
187 		.private = current,				\
188 		.task_list = LIST_HEAD_INIT((name).task_list),	\
189 		.func = autoremove_wake_function,		\
190 	}
191 
192 static inline void
193 prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
194 {
195 }
196 
197 static inline void
198 __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
199 {
200 	list_add(&new->task_list, &head->task_list);
201 }
202 
203 static inline void
204 add_wait_queue(wait_queue_head_t *head, wait_queue_t *wq)
205 {
206 	lockmgr(&head->lock, LK_EXCLUSIVE);
207 	__add_wait_queue(head, wq);
208 	lockmgr(&head->lock, LK_RELEASE);
209 }
210 
211 #define DECLARE_WAIT_QUEUE_HEAD(name)					\
212 	wait_queue_head_t name = {					\
213 		.lock = LOCK_INITIALIZER("name", 0, LK_CANRECURSE),	\
214 		.task_list = { &(name).task_list, &(name).task_list }	\
215 	}
216 
217 static inline void
218 __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
219 {
220 	list_del(&old->task_list);
221 }
222 
223 static inline void
224 remove_wait_queue(wait_queue_head_t *head, wait_queue_t *wq)
225 {
226 	lockmgr(&head->lock, LK_EXCLUSIVE);
227 	__remove_wait_queue(head, wq);
228 	lockmgr(&head->lock, LK_RELEASE);
229 }
230 
231 #endif	/* _LINUX_WAIT_H_ */
232