1 /* 2 * Copyright (c) 2014 Imre Vadász 3 * Copyright (c) 2014-2020 François Tigeot <ftigeot@wolfpond.org> 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 <linux/list.h> 32 #include <linux/stddef.h> 33 #include <linux/spinlock.h> 34 #include <asm/current.h> 35 36 typedef struct __wait_queue wait_queue_t; 37 38 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key); 39 40 int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key); 41 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key); 42 43 struct __wait_queue { 44 unsigned int flags; 45 void *private; 46 wait_queue_func_t func; 47 struct list_head task_list; 48 }; 49 50 typedef struct { 51 struct lock lock; 52 struct list_head task_list; 53 } wait_queue_head_t; 54 55 void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *); 56 57 static inline void 58 init_waitqueue_head(wait_queue_head_t *q) 59 { 60 __init_waitqueue_head(q, "", NULL); 61 } 62 63 void __wake_up_core(wait_queue_head_t *q, int num_to_wake_up); 64 65 static inline void 66 wake_up(wait_queue_head_t *q) 67 { 68 lockmgr(&q->lock, LK_EXCLUSIVE); 69 __wake_up_core(q, 1); 70 lockmgr(&q->lock, LK_RELEASE); 71 wakeup_one(q); 72 } 73 74 static inline void 75 wake_up_all(wait_queue_head_t *q) 76 { 77 lockmgr(&q->lock, LK_EXCLUSIVE); 78 __wake_up_core(q, 0); 79 lockmgr(&q->lock, LK_RELEASE); 80 wakeup(q); 81 } 82 83 void wake_up_bit(void *, int); 84 85 #define wake_up_all_locked(eq) __wake_up_core(eq, 0) 86 87 #define wake_up_interruptible(eq) wake_up(eq) 88 #define wake_up_interruptible_all(eq) wake_up_all(eq) 89 90 void __wait_event_prefix(wait_queue_head_t *wq, int flags); 91 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state); 92 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait); 93 94 /* 95 * wait_event_interruptible_timeout: 96 * - The process is put to sleep until the condition evaluates to true. 97 * - The condition is checked each time the waitqueue wq is woken up. 98 * - wake_up has to be called after changing any variable that could change 99 * the result of the wait condition. 100 * 101 * returns: 102 * - 0 if the timeout elapsed 103 * - the remaining jiffies if the condition evaluated to true before 104 * the timeout elapsed. 105 * - remaining jiffies are always at least 1 106 * - -ERESTARTSYS if interrupted by a signal (when PCATCH is set in flags) 107 */ 108 #define __wait_event_common(wq, condition, timeout_jiffies, flags, \ 109 locked) \ 110 ({ \ 111 int start_jiffies, elapsed_jiffies, remaining_jiffies, ret; \ 112 bool timeout_expired = false; \ 113 bool interrupted = false; \ 114 long retval; \ 115 wait_queue_t tmp_wq; \ 116 \ 117 start_jiffies = ticks; \ 118 INIT_LIST_HEAD(&tmp_wq.task_list); \ 119 \ 120 while (1) { \ 121 __wait_event_prefix(&wq, flags); \ 122 \ 123 if (condition) \ 124 break; \ 125 \ 126 ret = tsleep(&wq, flags, "lwe", timeout_jiffies); \ 127 if (ret == EINTR || ret == ERESTART) { \ 128 interrupted = true; \ 129 break; \ 130 } \ 131 if (ret == EWOULDBLOCK) { \ 132 timeout_expired = true; \ 133 break; \ 134 } \ 135 } \ 136 \ 137 elapsed_jiffies = ticks - start_jiffies; \ 138 remaining_jiffies = timeout_jiffies - elapsed_jiffies; \ 139 if (remaining_jiffies <= 0) \ 140 remaining_jiffies = 1; \ 141 \ 142 if (timeout_expired) \ 143 retval = 0; \ 144 else if (interrupted) \ 145 retval = -ERESTARTSYS; \ 146 else if (timeout_jiffies > 0) \ 147 retval = remaining_jiffies; \ 148 else \ 149 retval = 1; \ 150 \ 151 finish_wait(&wq, &tmp_wq); \ 152 retval; \ 153 }) 154 155 #define wait_event(wq, condition) \ 156 __wait_event_common(wq, condition, 0, 0, false) 157 158 #define wait_event_timeout(wq, condition, timeout) \ 159 __wait_event_common(wq, condition, timeout, 0, false) 160 161 #define wait_event_interruptible(wq, condition) \ 162 ({ \ 163 long retval; \ 164 \ 165 retval = __wait_event_common(wq, condition, 0, PCATCH, false); \ 166 if (retval != -ERESTARTSYS) \ 167 retval = 0; \ 168 retval; \ 169 }) 170 171 #define wait_event_interruptible_locked(wq, condition) \ 172 ({ \ 173 long retval; \ 174 \ 175 retval = __wait_event_common(wq, condition, 0, PCATCH, true); \ 176 if (retval != -ERESTARTSYS) \ 177 retval = 0; \ 178 retval; \ 179 }) 180 181 #define wait_event_interruptible_timeout(wq, condition, timeout) \ 182 __wait_event_common(wq, condition, timeout, PCATCH, false) 183 184 static inline int 185 waitqueue_active(wait_queue_head_t *q) 186 { 187 return !list_empty(&q->task_list); 188 } 189 190 #define DEFINE_WAIT(name) \ 191 wait_queue_t name = { \ 192 .private = current, \ 193 .task_list = LIST_HEAD_INIT((name).task_list), \ 194 .func = autoremove_wake_function, \ 195 } 196 197 static inline void 198 __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new) 199 { 200 list_add(&new->task_list, &head->task_list); 201 } 202 203 static inline void 204 add_wait_queue(wait_queue_head_t *head, wait_queue_t *wq) 205 { 206 lockmgr(&head->lock, LK_EXCLUSIVE); 207 __add_wait_queue(head, wq); 208 lockmgr(&head->lock, LK_RELEASE); 209 } 210 211 #define DECLARE_WAIT_QUEUE_HEAD(name) \ 212 wait_queue_head_t name = { \ 213 .lock = LOCK_INITIALIZER("name", 0, LK_CANRECURSE), \ 214 .task_list = { &(name).task_list, &(name).task_list } \ 215 } 216 217 static inline void 218 __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old) 219 { 220 list_del(&old->task_list); 221 } 222 223 static inline void 224 remove_wait_queue(wait_queue_head_t *head, wait_queue_t *wq) 225 { 226 lockmgr(&head->lock, LK_EXCLUSIVE); 227 __remove_wait_queue(head, wq); 228 lockmgr(&head->lock, LK_RELEASE); 229 } 230 231 static inline void 232 __add_wait_queue_tail(wait_queue_head_t *wqh, wait_queue_t *wq) 233 { 234 list_add_tail(&wq->task_list, &wqh->task_list); 235 } 236 237 int wait_on_bit_timeout(unsigned long *word, int bit, 238 unsigned mode, unsigned long timeout); 239 240 #endif /* _LINUX_WAIT_H_ */ 241