1 /* 2 * Copyright (c) 2014-2019 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 c->done++; 64 lockmgr(&c->wait.lock, LK_RELEASE); 65 wakeup_one(&c->wait); 66 } 67 68 static inline void 69 complete_all(struct completion *c) 70 { 71 lockmgr(&c->wait.lock, LK_EXCLUSIVE); 72 c->done++; 73 lockmgr(&c->wait.lock, LK_RELEASE); 74 wakeup(&c->wait); 75 } 76 77 static inline long 78 __wait_for_completion_generic(struct completion *c, 79 unsigned long timeout, int flags) 80 { 81 int start_jiffies, elapsed_jiffies, remaining_jiffies; 82 bool timeout_expired = false, awakened = false; 83 long ret = 1; 84 85 start_jiffies = ticks; 86 87 lockmgr(&c->wait.lock, LK_EXCLUSIVE); 88 while (c->done == 0 && !timeout_expired) { 89 ret = lksleep(&c->wait, &c->wait.lock, flags, "lwfcg", timeout); 90 switch(ret) { 91 case EWOULDBLOCK: 92 timeout_expired = true; 93 ret = 0; 94 break; 95 case ERESTART: 96 ret = -ERESTARTSYS; 97 break; 98 case 0: 99 awakened = true; 100 break; 101 } 102 } 103 lockmgr(&c->wait.lock, LK_RELEASE); 104 105 if (awakened) { 106 elapsed_jiffies = ticks - start_jiffies; 107 remaining_jiffies = timeout - elapsed_jiffies; 108 if (remaining_jiffies > 0) 109 ret = remaining_jiffies; 110 } 111 112 return ret; 113 } 114 115 static inline long 116 wait_for_completion_interruptible_timeout(struct completion *c, 117 unsigned long timeout) 118 { 119 return __wait_for_completion_generic(c, timeout, PCATCH); 120 } 121 122 static inline unsigned long 123 wait_for_completion_timeout(struct completion *c, unsigned long timeout) 124 { 125 return __wait_for_completion_generic(c, timeout, 0); 126 } 127 128 void wait_for_completion(struct completion *c); 129 130 /* 131 * try_wait_for_completion: try to decrement a completion without blocking 132 * its thread 133 * return: false if the completion thread would need to be blocked/queued 134 * true if a non-blocking decrement was successful 135 */ 136 static inline bool 137 try_wait_for_completion(struct completion *c) 138 { 139 bool ret = false; 140 141 /* we can't decrement c->done below 0 */ 142 if (READ_ONCE(c->done) == 0) 143 return false; 144 145 lockmgr(&c->wait.lock, LK_EXCLUSIVE); 146 if (c->done > 0) { 147 c->done--; 148 ret = true; 149 } 150 lockmgr(&c->wait.lock, LK_RELEASE); 151 152 return ret; 153 } 154 155 #endif /* _LINUX_COMPLETION_H_ */ 156