xref: /dragonfly/sys/dev/drm/include/linux/wait.h (revision f503b4c4)
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, "linux_waitqueue");
41 }
42 
43 #define wake_up(eq)		wakeup_one(eq)
44 #define wake_up_all(eq)		wakeup(eq)
45 
46 /*
47  * wait_event_timeout:
48  * - The process is put to sleep until the condition evaluates to true.
49  * - The condition is checked each time the waitqueue wq is woken up.
50  * - wake_up has to be called after changing any variable that could change
51  * the result of the wait condition.
52  *
53  * returns:
54  *   - 0 if the timeout elapsed
55  *   - the remaining jiffies if the condition evaluated to true before
56  *   the timeout elapsed.
57  *   - remaining jiffies are always at least 1
58 */
59 #define __wait_event_common(wq, condition, timeout_jiffies, flags)	\
60 ({									\
61 	int start_jiffies, elapsed_jiffies, remaining_jiffies, ret;	\
62 	bool timeout_expired = false;					\
63 	long retval;							\
64 									\
65 	start_jiffies = ticks;						\
66 									\
67 	spin_lock(&wq.lock);						\
68 	while (1) {							\
69 		if (condition)						\
70 			break;						\
71 									\
72 		ret = ssleep(&wq, &wq.lock, flags,			\
73 					"lwe", timeout_jiffies);	\
74 		if (ret == EWOULDBLOCK) {				\
75 			timeout_expired = true;				\
76 			break;						\
77 		}							\
78 	}								\
79 	spin_unlock(&wq.lock);						\
80 									\
81 	elapsed_jiffies = ticks - start_jiffies;			\
82 	remaining_jiffies = timeout_jiffies - elapsed_jiffies;		\
83 	if (remaining_jiffies == 0)					\
84 		remaining_jiffies = 1;					\
85 									\
86 	if (timeout_expired)						\
87 		retval = 0;						\
88 	else 								\
89 		retval = remaining_jiffies;				\
90 									\
91 	retval;								\
92 })
93 
94 #define wait_event(wq, condition)					\
95 		__wait_event_common(wq, condition, 0, 0)
96 
97 #define wait_event_timeout(wq, condition, timeout)			\
98 		__wait_event_common(wq, condition, timeout, 0)
99 
100 #define wait_event_interruptible_timeout(wq, condition, timeout)	\
101 		__wait_event_common(wq, condition, timeout, PCATCH)
102 
103 #endif	/* _LINUX_WAIT_H_ */
104