xref: /dragonfly/sys/dev/drm/include/linux/wait.h (revision eb67213a)
1 /*
2  * Copyright (c) 2014 Imre Vadász
3  * Copyright (c) 2014-2019 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 struct __wait_queue {
41 	unsigned int flags;
42 	void *private;
43 	wait_queue_func_t func;
44 };
45 
46 typedef struct {
47 	struct lock	lock;
48 } wait_queue_head_t;
49 
50 static inline void
51 init_waitqueue_head(wait_queue_head_t *eq)
52 {
53 	lockinit(&eq->lock, "lwq", 0, LK_CANRECURSE);
54 }
55 
56 #define wake_up(eq)			wakeup_one(eq)
57 #define wake_up_all(eq)			wakeup(eq)
58 #define wake_up_all_locked(eq)		wakeup(eq)
59 #define wake_up_interruptible(eq)	wakeup_one(eq)
60 #define wake_up_interruptible_all(eq)	wakeup(eq)
61 
62 /*
63  * wait_event_interruptible_timeout:
64  * - The process is put to sleep until the condition evaluates to true.
65  * - The condition is checked each time the waitqueue wq is woken up.
66  * - wake_up has to be called after changing any variable that could change
67  * the result of the wait condition.
68  *
69  * returns:
70  *   - 0 if the timeout elapsed
71  *   - the remaining jiffies if the condition evaluated to true before
72  *   the timeout elapsed.
73  *   - remaining jiffies are always at least 1
74  *   - -ERESTARTSYS if interrupted by a signal (when PCATCH is set in flags)
75 */
76 #define __wait_event_common(wq, condition, timeout_jiffies, flags)	\
77 ({									\
78 	int start_jiffies, elapsed_jiffies, remaining_jiffies, ret;	\
79 	bool timeout_expired = false;					\
80 	bool interrupted = false;					\
81 	long retval;							\
82 									\
83 	start_jiffies = ticks;						\
84 									\
85 	lockmgr(&wq.lock, LK_EXCLUSIVE);				\
86 	while (1) {							\
87 		if (condition)						\
88 			break;						\
89 									\
90 		ret = lksleep(&wq, &wq.lock, flags,			\
91 					"lwe", timeout_jiffies);	\
92 		if (ret == EINTR || ret == ERESTART) {			\
93 			interrupted = true;				\
94 			break;						\
95 		}							\
96 		if (ret == EWOULDBLOCK) {				\
97 			timeout_expired = true;				\
98 			break;						\
99 		}							\
100 	}								\
101 	lockmgr(&wq.lock, LK_RELEASE);					\
102 									\
103 	elapsed_jiffies = ticks - start_jiffies;			\
104 	remaining_jiffies = timeout_jiffies - elapsed_jiffies;		\
105 	if (remaining_jiffies <= 0)					\
106 		remaining_jiffies = 1;					\
107 									\
108 	if (timeout_expired)						\
109 		retval = 0;						\
110 	else if (interrupted)						\
111 		retval = -ERESTARTSYS;					\
112 	else if (timeout_jiffies > 0)					\
113 		retval = remaining_jiffies;				\
114 	else								\
115 		retval = 1;						\
116 									\
117 	retval;								\
118 })
119 
120 #define wait_event(wq, condition)					\
121 		__wait_event_common(wq, condition, 0, 0)
122 
123 #define wait_event_timeout(wq, condition, timeout)			\
124 		__wait_event_common(wq, condition, timeout, 0)
125 
126 #define wait_event_interruptible(wq, condition)				\
127 ({									\
128 	long retval;							\
129 									\
130 	retval = __wait_event_common(wq, condition, 0, PCATCH);		\
131 	if (retval != -ERESTARTSYS)					\
132 		retval = 0;						\
133 	retval;								\
134 })
135 
136 #define wait_event_interruptible_timeout(wq, condition, timeout)	\
137 		__wait_event_common(wq, condition, timeout, PCATCH)
138 
139 static inline int
140 waitqueue_active(wait_queue_head_t *q)
141 {
142 	return 0;	/* XXX: not really implemented */
143 }
144 
145 #define DEFINE_WAIT(name)	\
146 	wait_queue_t name = {}
147 
148 static inline void
149 prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
150 {
151 }
152 
153 static inline void
154 finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
155 {
156 }
157 
158 static inline void
159 add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
160 {
161 }
162 
163 static inline void
164 __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
165 {
166 }
167 
168 #define DECLARE_WAIT_QUEUE_HEAD(name)					\
169 	wait_queue_head_t name = {					\
170 		.lock = LOCK_INITIALIZER("name", 0, LK_CANRECURSE)	\
171 	}
172 
173 static inline void
174 __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
175 {
176 }
177 
178 #endif	/* _LINUX_WAIT_H_ */
179