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 40 init_completion(struct completion *c) 41 { 42 c->done = 0; 43 init_waitqueue_head(&c->wait); 44 } 45 46 static inline void 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 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 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 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 static inline long 117 wait_for_completion_interruptible_timeout(struct completion *c, 118 unsigned long timeout) 119 { 120 return __wait_for_completion_generic(c, timeout, PCATCH); 121 } 122 123 static inline unsigned long 124 wait_for_completion_timeout(struct completion *c, unsigned long timeout) 125 { 126 return __wait_for_completion_generic(c, timeout, 0); 127 } 128 129 void wait_for_completion(struct completion *c); 130 131 /* 132 * try_wait_for_completion: try to decrement a completion without blocking 133 * its thread 134 * return: false if the completion thread would need to be blocked/queued 135 * true if a non-blocking decrement was successful 136 */ 137 static inline bool 138 try_wait_for_completion(struct completion *c) 139 { 140 bool ret = false; 141 142 /* we can't decrement c->done below 0 */ 143 if (READ_ONCE(c->done) == 0) 144 return false; 145 146 lockmgr(&c->wait.lock, LK_EXCLUSIVE); 147 if (c->done > 0) { 148 c->done--; 149 ret = true; 150 } 151 lockmgr(&c->wait.lock, LK_RELEASE); 152 153 return ret; 154 } 155 156 #endif /* _LINUX_COMPLETION_H_ */ 157