xref: /dflybsd-src/sys/dev/drm/include/linux/wait.h (revision a62226e46c982d037de05e1bb0894805c0b7a32f)
1 /*
2  * Copyright (c) 2014 François Tigeot
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef _LINUX_WAIT_H_
28 #define _LINUX_WAIT_H_
29 
30 #include <sys/spinlock2.h>
31 #include <sys/param.h>
32 
33 typedef struct {
34 	struct spinlock	lock;
35 } wait_queue_head_t;
36 
37 static inline void
38 init_waitqueue_head(wait_queue_head_t *eq)
39 {
40 	spin_init(&eq->lock);
41 }
42 
43 #define wake_up_all(eq)		wakeup(eq)
44 
45 /*
46  * wait_event_timeout:
47  * - The process is put to sleep until the condition evaluates to true.
48  * - The condition is checked each time the waitqueue wq is woken up.
49  * - wake_up has to be called after changing any variable that could change
50  * the result of the wait condition.
51  *
52  * returns:
53  *   - 0 if the timeout elapsed
54  *   - the remaining jiffies if the condition evaluated to true before
55  *   the timeout elapsed.
56  *   - remaining jiffies are always at least 1
57 */
58 #define __wait_event_common(wq, condition, timeout_jiffies, flags)	\
59 ({									\
60 	int start_jiffies, elapsed_jiffies, remaining_jiffies;		\
61 	bool timeout_expired = false;					\
62 	long retval;							\
63 									\
64 	start_jiffies = ticks;						\
65 									\
66 	spin_lock(&wq.lock);						\
67 	while (1) {							\
68 		if (condition)						\
69 			break;						\
70 									\
71 		ret = ssleep(&wq, &wq.lock, flags,			\
72 					"lwe", timeout_jiffies);	\
73 		if (ret == EWOULDBLOCK) {				\
74 			timeout_expired = true;				\
75 			break;						\
76 		}							\
77 	}								\
78 	spin_unlock(&wq.lock);						\
79 									\
80 	elapsed_jiffies = ticks - start_jiffies;			\
81 	remaining_jiffies = timeout_jiffies - elapsed_jiffies;		\
82 	if (remaining_jiffies == 0)					\
83 		remaining_jiffies = 1;					\
84 									\
85 	if (timeout_expired)						\
86 		retval = 0;						\
87 	else 								\
88 		retval = remaining_jiffies;				\
89 									\
90 	retval;								\
91 })
92 
93 #define wait_event_timeout(wq, condition, timeout)			\
94 		__wait_event_common(wq, condition, timeout, 0)
95 
96 #define wait_event_interruptible_timeout(wq, condition, timeout)	\
97 		__wait_event_common(wq, condition, timeout, PCATCH)
98 
99 #endif	/* _LINUX_WAIT_H_ */
100