2 * Copyright (c) 2014 François Tigeot
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
27 #ifndef _LINUX_WAIT_H_
28 #define _LINUX_WAIT_H_
30 #include <sys/spinlock2.h>
31 #include <sys/param.h>
38 init_waitqueue_head(wait_queue_head_t *eq)
40 spin_init(&eq->lock, "linux_waitqueue");
43 #define wake_up(eq) wakeup_one(eq)
44 #define wake_up_all(eq) wakeup(eq)
48 * - The process is put to sleep until the condition evaluates to true.
49 * - The condition is checked each time the waitqueue wq is woken up.
50 * - wake_up has to be called after changing any variable that could change
51 * the result of the wait condition.
54 * - 0 if the timeout elapsed
55 * - the remaining jiffies if the condition evaluated to true before
56 * the timeout elapsed.
57 * - remaining jiffies are always at least 1
59 #define __wait_event_common(wq, condition, timeout_jiffies, flags) \
61 int start_jiffies, elapsed_jiffies, remaining_jiffies, ret; \
62 bool timeout_expired = false; \
65 start_jiffies = ticks; \
67 spin_lock(&wq.lock); \
72 ret = ssleep(&wq, &wq.lock, flags, \
73 "lwe", timeout_jiffies); \
74 if (ret == EWOULDBLOCK) { \
75 timeout_expired = true; \
79 spin_unlock(&wq.lock); \
81 elapsed_jiffies = ticks - start_jiffies; \
82 remaining_jiffies = timeout_jiffies - elapsed_jiffies; \
83 if (remaining_jiffies == 0) \
84 remaining_jiffies = 1; \
86 if (timeout_expired) \
89 retval = remaining_jiffies; \
94 #define wait_event(wq, condition) \
95 __wait_event_common(wq, condition, 0, 0)
97 #define wait_event_timeout(wq, condition, timeout) \
98 __wait_event_common(wq, condition, timeout, 0)
100 #define wait_event_interruptible_timeout(wq, condition, timeout) \
101 __wait_event_common(wq, condition, timeout, PCATCH)
103 #endif /* _LINUX_WAIT_H_ */