drm: Add wake_up_interruptible_all()
[dragonfly.git] / sys / dev / drm / include / linux / wait.h
CommitLineData
a6796b4c 1/*
c0e97ee6 2 * Copyright (c) 2014-2015 François Tigeot
63c3939f 3 * Copyright (c) 2014 Imre Vadász
a6796b4c
FT
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
24b50f1a 31#include <sys/spinlock2.h>
19b28dc8 32#include <sys/param.h>
24b50f1a 33
0c55ada2
FT
34typedef struct {
35} wait_queue_t;
36
24b50f1a 37typedef struct {
88c5318e 38 struct lock lock;
24b50f1a
FT
39} wait_queue_head_t;
40
41static inline void
42init_waitqueue_head(wait_queue_head_t *eq)
43{
88c5318e 44 lockinit(&eq->lock, "lwq", 0, LK_CANRECURSE);
24b50f1a 45}
a6796b4c 46
089893e2
FT
47#define wake_up(eq) wakeup_one(eq)
48#define wake_up_all(eq) wakeup(eq)
49#define wake_up_interruptible_all(eq) wakeup(eq)
a6796b4c 50
19b28dc8 51/*
63c3939f 52 * wait_event_interruptible_timeout:
19b28dc8
FT
53 * - The process is put to sleep until the condition evaluates to true.
54 * - The condition is checked each time the waitqueue wq is woken up.
55 * - wake_up has to be called after changing any variable that could change
56 * the result of the wait condition.
57 *
58 * returns:
59 * - 0 if the timeout elapsed
60 * - the remaining jiffies if the condition evaluated to true before
61 * the timeout elapsed.
62 * - remaining jiffies are always at least 1
63c3939f 63 * - -ERESTARTSYS if interrupted by a signal (when PCATCH is set in flags)
19b28dc8
FT
64*/
65#define __wait_event_common(wq, condition, timeout_jiffies, flags) \
66({ \
fbf82e35 67 int start_jiffies, elapsed_jiffies, remaining_jiffies, ret; \
19b28dc8 68 bool timeout_expired = false; \
63c3939f 69 bool interrupted = false; \
19b28dc8
FT
70 long retval; \
71 \
72 start_jiffies = ticks; \
73 \
88c5318e 74 lockmgr(&wq.lock, LK_EXCLUSIVE); \
19b28dc8
FT
75 while (1) { \
76 if (condition) \
77 break; \
78 \
88c5318e 79 ret = lksleep(&wq, &wq.lock, flags, \
19b28dc8 80 "lwe", timeout_jiffies); \
63c3939f
IV
81 if (ret == EINTR || ret == ERESTART) { \
82 interrupted = true; \
83 break; \
84 } \
19b28dc8
FT
85 if (ret == EWOULDBLOCK) { \
86 timeout_expired = true; \
87 break; \
88 } \
89 } \
88c5318e 90 lockmgr(&wq.lock, LK_RELEASE); \
19b28dc8
FT
91 \
92 elapsed_jiffies = ticks - start_jiffies; \
93 remaining_jiffies = timeout_jiffies - elapsed_jiffies; \
63c3939f 94 if (remaining_jiffies <= 0) \
19b28dc8
FT
95 remaining_jiffies = 1; \
96 \
97 if (timeout_expired) \
98 retval = 0; \
63c3939f
IV
99 else if (interrupted) \
100 retval = -ERESTARTSYS; \
101 else if (timeout_jiffies > 0) \
19b28dc8 102 retval = remaining_jiffies; \
63c3939f
IV
103 else \
104 retval = 1; \
19b28dc8
FT
105 \
106 retval; \
107})
108
fbf82e35
FT
109#define wait_event(wq, condition) \
110 __wait_event_common(wq, condition, 0, 0)
111
19b28dc8
FT
112#define wait_event_timeout(wq, condition, timeout) \
113 __wait_event_common(wq, condition, timeout, 0)
114
e434fbb5 115#define wait_event_interruptible(wq, condition) \
63c3939f
IV
116({ \
117 long retval; \
118 \
119 retval = __wait_event_common(wq, condition, 0, PCATCH); \
120 if (retval != -ERESTARTSYS) \
121 retval = 0; \
122 retval; \
123})
e434fbb5 124
19b28dc8
FT
125#define wait_event_interruptible_timeout(wq, condition, timeout) \
126 __wait_event_common(wq, condition, timeout, PCATCH)
127
c0e97ee6
FT
128static inline int
129waitqueue_active(wait_queue_head_t *q)
130{
131 return 0; /* XXX: not really implemented */
132}
133
0c55ada2
FT
134#define DEFINE_WAIT(name) \
135 wait_queue_t name = {}
136
137static inline void
138prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
139{
140}
141
142static inline void
143finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
144{
145}
146
a6796b4c 147#endif /* _LINUX_WAIT_H_ */