1 /*
2 * Copyright (c) 2014-2020 François Tigeot <ftigeot@wolfpond.org>
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 #ifndef _LINUX_COMPLETION_H_
27 #define _LINUX_COMPLETION_H_
28
29 #include <linux/wait.h>
30 #include <linux/errno.h>
31
32 #include <sys/kernel.h>
33
34 struct completion {
35 unsigned int done;
36 wait_queue_head_t wait;
37 };
38
39 static inline void
init_completion(struct completion * c)40 init_completion(struct completion *c)
41 {
42 c->done = 0;
43 init_waitqueue_head(&c->wait);
44 }
45
46 static inline void
reinit_completion(struct completion * c)47 reinit_completion(struct completion *c)
48 {
49 c->done = 0;
50 }
51
52 #define INIT_COMPLETION(c) (c.done = 0)
53
54 /*
55 * Completion interlock and wakeup. Be careful not to execute the wakeup
56 * from inside the spinlock as this can deadlock if the IPIQ fifo is full.
57 * (also note that wakeup() is asynchronous anyway, so no point doing that).
58 */
59 static inline void
complete(struct completion * c)60 complete(struct completion *c)
61 {
62 lockmgr(&c->wait.lock, LK_EXCLUSIVE);
63 if (c->done != UINT_MAX)
64 c->done++;
65 lockmgr(&c->wait.lock, LK_RELEASE);
66 wakeup_one(&c->wait);
67 }
68
69 static inline void
complete_all(struct completion * c)70 complete_all(struct completion *c)
71 {
72 lockmgr(&c->wait.lock, LK_EXCLUSIVE);
73 c->done = UINT_MAX;
74 lockmgr(&c->wait.lock, LK_RELEASE);
75 wakeup(&c->wait);
76 }
77
78 static inline long
__wait_for_completion_generic(struct completion * c,unsigned long timeout,int flags)79 __wait_for_completion_generic(struct completion *c,
80 unsigned long timeout, int flags)
81 {
82 int start_jiffies, elapsed_jiffies, remaining_jiffies;
83 bool timeout_expired = false, awakened = false;
84 long ret = 1;
85
86 start_jiffies = ticks;
87
88 lockmgr(&c->wait.lock, LK_EXCLUSIVE);
89 while (c->done == 0 && !timeout_expired) {
90 ret = lksleep(&c->wait, &c->wait.lock, flags, "lwfcg", timeout);
91 switch(ret) {
92 case EWOULDBLOCK:
93 timeout_expired = true;
94 ret = 0;
95 break;
96 case ERESTART:
97 ret = -ERESTARTSYS;
98 break;
99 case 0:
100 awakened = true;
101 break;
102 }
103 }
104 lockmgr(&c->wait.lock, LK_RELEASE);
105
106 if (awakened) {
107 elapsed_jiffies = ticks - start_jiffies;
108 remaining_jiffies = timeout - elapsed_jiffies;
109 if (remaining_jiffies > 0)
110 ret = remaining_jiffies;
111 }
112
113 return ret;
114 }
115
116 int wait_for_completion_interruptible(struct completion *c);
117
118 static inline long
wait_for_completion_interruptible_timeout(struct completion * c,unsigned long timeout)119 wait_for_completion_interruptible_timeout(struct completion *c,
120 unsigned long timeout)
121 {
122 return __wait_for_completion_generic(c, timeout, PCATCH);
123 }
124
125 static inline unsigned long
wait_for_completion_timeout(struct completion * c,unsigned long timeout)126 wait_for_completion_timeout(struct completion *c, unsigned long timeout)
127 {
128 return __wait_for_completion_generic(c, timeout, 0);
129 }
130
131 void wait_for_completion(struct completion *c);
132
133 /*
134 * try_wait_for_completion: try to decrement a completion without blocking
135 * its thread
136 * return: false if the completion thread would need to be blocked/queued
137 * true if a non-blocking decrement was successful
138 */
139 static inline bool
try_wait_for_completion(struct completion * c)140 try_wait_for_completion(struct completion *c)
141 {
142 bool ret = false;
143
144 /* we can't decrement c->done below 0 */
145 if (READ_ONCE(c->done) == 0)
146 return false;
147
148 lockmgr(&c->wait.lock, LK_EXCLUSIVE);
149 if (c->done > 0) {
150 c->done--;
151 ret = true;
152 }
153 lockmgr(&c->wait.lock, LK_RELEASE);
154
155 return ret;
156 }
157
158 #endif /* _LINUX_COMPLETION_H_ */
159