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