2 * Copyright (c) 2014 Imre Vadász
3 * Copyright (c) 2014-2020 François Tigeot <ftigeot@wolfpond.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
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.
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.
28 #ifndef _LINUX_WAIT_H_
29 #define _LINUX_WAIT_H_
31 #include <linux/list.h>
32 #include <linux/stddef.h>
33 #include <linux/spinlock.h>
34 #include <asm/current.h>
36 typedef struct wait_queue_entry wait_queue_entry_t;
38 typedef int (*wait_queue_func_t)(wait_queue_entry_t *wait, unsigned mode, int flags, void *key);
40 int default_wake_function(wait_queue_entry_t *wait, unsigned mode, int flags, void *key);
41 int autoremove_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key);
43 struct wait_queue_entry {
46 wait_queue_func_t func;
47 struct list_head entry;
50 void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
54 struct list_head head;
57 void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
60 init_waitqueue_head(wait_queue_head_t *q)
62 __init_waitqueue_head(q, "", NULL);
65 void __wake_up_core(wait_queue_head_t *q, int num_to_wake_up);
68 wake_up(wait_queue_head_t *q)
70 lockmgr(&q->lock, LK_EXCLUSIVE);
72 lockmgr(&q->lock, LK_RELEASE);
77 wake_up_all(wait_queue_head_t *q)
79 lockmgr(&q->lock, LK_EXCLUSIVE);
81 lockmgr(&q->lock, LK_RELEASE);
85 void wake_up_bit(void *, int);
87 #define wake_up_all_locked(eq) __wake_up_core(eq, 0)
89 #define wake_up_interruptible(eq) wake_up(eq)
90 #define wake_up_interruptible_all(eq) wake_up_all(eq)
92 void __wait_event_prefix(wait_queue_head_t *wq, int flags);
93 void prepare_to_wait(wait_queue_head_t *q, wait_queue_entry_t *wait, int state);
94 void finish_wait(wait_queue_head_t *q, wait_queue_entry_t *wait);
97 * wait_event_interruptible_timeout:
98 * - The process is put to sleep until the condition evaluates to true.
99 * - The condition is checked each time the waitqueue wq is woken up.
100 * - wake_up has to be called after changing any variable that could change
101 * the result of the wait condition.
104 * - 0 if the timeout elapsed
105 * - the remaining jiffies if the condition evaluated to true before
106 * the timeout elapsed.
107 * - remaining jiffies are always at least 1
108 * - -ERESTARTSYS if interrupted by a signal (when PCATCH is set in flags)
110 #define __wait_event_common(wq, condition, timeout_jiffies, flags, \
113 int start_jiffies, elapsed_jiffies, remaining_jiffies, ret; \
114 bool timeout_expired = false; \
115 bool interrupted = false; \
118 DEFINE_WAIT(tmp_wq); \
120 start_jiffies = ticks; \
121 state = (flags & PCATCH) ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE; \
122 prepare_to_wait(&wq, &tmp_wq, state); \
125 __wait_event_prefix(&wq, flags); \
130 tsleep_interlock(current, flags); \
132 if ((timeout_jiffies) != 0) { \
133 ret = tsleep(current, PINTERLOCKED|flags, "lwe", timeout_jiffies); \
135 ret = tsleep(current, PINTERLOCKED|flags, "lwe", hz);\
136 if (ret == EWOULDBLOCK) { \
138 /*print_backtrace(-1);*/ \
143 if (ret == EINTR || ret == ERESTART) { \
144 interrupted = true; \
147 if (ret == EWOULDBLOCK) { \
148 timeout_expired = true; \
153 elapsed_jiffies = ticks - start_jiffies; \
154 remaining_jiffies = timeout_jiffies - elapsed_jiffies; \
155 if (remaining_jiffies <= 0) \
156 remaining_jiffies = 1; \
158 if (timeout_expired) \
160 else if (interrupted) \
161 retval = -ERESTARTSYS; \
162 else if (timeout_jiffies > 0) \
163 retval = remaining_jiffies; \
167 finish_wait(&wq, &tmp_wq); \
171 #define wait_event(wq, condition) \
172 __wait_event_common(wq, condition, 0, 0, false)
174 #define wait_event_timeout(wq, condition, timeout) \
175 __wait_event_common(wq, condition, timeout, 0, false)
177 #define wait_event_interruptible(wq, condition) \
181 retval = __wait_event_common(wq, condition, 0, PCATCH, false); \
182 if (retval != -ERESTARTSYS) \
187 #define wait_event_interruptible_locked(wq, condition) \
191 retval = __wait_event_common(wq, condition, 0, PCATCH, true); \
192 if (retval != -ERESTARTSYS) \
197 #define wait_event_interruptible_timeout(wq, condition, timeout) \
198 __wait_event_common(wq, condition, timeout, PCATCH, false)
201 waitqueue_active(wait_queue_head_t *q)
203 return !list_empty(&q->head);
206 #define DEFINE_WAIT_FUNC(name, _function) \
207 wait_queue_entry_t name = { \
208 .private = current, \
209 .entry = LIST_HEAD_INIT((name).entry), \
213 #define DEFINE_WAIT(name) \
214 DEFINE_WAIT_FUNC((name), autoremove_wake_function)
217 __add_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *new)
219 list_add(&new->entry, &head->head);
223 add_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *wait)
225 lockmgr(&head->lock, LK_EXCLUSIVE);
226 __add_wait_queue(head, wait);
227 lockmgr(&head->lock, LK_RELEASE);
230 #define DECLARE_WAIT_QUEUE_HEAD(name) \
231 wait_queue_head_t name = { \
232 .lock = LOCK_INITIALIZER("name", 0, LK_CANRECURSE), \
233 .head = { &(name).head, &(name).head } \
237 __remove_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *old)
239 list_del(&old->entry);
243 remove_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *wq)
245 lockmgr(&head->lock, LK_EXCLUSIVE);
246 __remove_wait_queue(head, wq);
247 lockmgr(&head->lock, LK_RELEASE);
251 __add_wait_queue_entry_tail(wait_queue_head_t *wqh, wait_queue_entry_t *wq)
253 list_add_tail(&wq->entry, &wqh->head);
256 #endif /* _LINUX_WAIT_H_ */