1a6796b4cSFrançois Tigeot /*
263c3939fSImre Vadasz * Copyright (c) 2014 Imre Vadász
3edbc586dSFrançois Tigeot * Copyright (c) 2014-2020 François Tigeot <ftigeot@wolfpond.org>
4a6796b4cSFrançois Tigeot * All rights reserved.
5a6796b4cSFrançois Tigeot *
6a6796b4cSFrançois Tigeot * Redistribution and use in source and binary forms, with or without
7a6796b4cSFrançois Tigeot * modification, are permitted provided that the following conditions
8a6796b4cSFrançois Tigeot * are met:
9a6796b4cSFrançois Tigeot * 1. Redistributions of source code must retain the above copyright
10a6796b4cSFrançois Tigeot * notice unmodified, this list of conditions, and the following
11a6796b4cSFrançois Tigeot * disclaimer.
12a6796b4cSFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright
13a6796b4cSFrançois Tigeot * notice, this list of conditions and the following disclaimer in the
14a6796b4cSFrançois Tigeot * documentation and/or other materials provided with the distribution.
15a6796b4cSFrançois Tigeot *
16a6796b4cSFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17a6796b4cSFrançois Tigeot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18a6796b4cSFrançois Tigeot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19a6796b4cSFrançois Tigeot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20a6796b4cSFrançois Tigeot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21a6796b4cSFrançois Tigeot * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22a6796b4cSFrançois Tigeot * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23a6796b4cSFrançois Tigeot * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24a6796b4cSFrançois Tigeot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25a6796b4cSFrançois Tigeot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26a6796b4cSFrançois Tigeot */
27a6796b4cSFrançois Tigeot
28a6796b4cSFrançois Tigeot #ifndef _LINUX_WAIT_H_
29a6796b4cSFrançois Tigeot #define _LINUX_WAIT_H_
30a6796b4cSFrançois Tigeot
3139cfddd2SFrançois Tigeot #include <linux/list.h>
32a8601baeSFrançois Tigeot #include <linux/stddef.h>
335377fbbdSFrançois Tigeot #include <linux/spinlock.h>
34d6aa1cc5SFrançois Tigeot #include <asm/current.h>
35a6796b4cSFrançois Tigeot
363f2dd94aSFrançois Tigeot typedef struct wait_queue_entry wait_queue_entry_t;
37252524e5SFrançois Tigeot
383f2dd94aSFrançois Tigeot typedef int (*wait_queue_func_t)(wait_queue_entry_t *wait, unsigned mode, int flags, void *key);
39252524e5SFrançois Tigeot
403f2dd94aSFrançois Tigeot int default_wake_function(wait_queue_entry_t *wait, unsigned mode, int flags, void *key);
413f2dd94aSFrançois Tigeot int autoremove_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key);
42d658c120SFrançois Tigeot
433f2dd94aSFrançois Tigeot struct wait_queue_entry {
44252524e5SFrançois Tigeot unsigned int flags;
45252524e5SFrançois Tigeot void *private;
46252524e5SFrançois Tigeot wait_queue_func_t func;
473f2dd94aSFrançois Tigeot struct list_head entry;
48252524e5SFrançois Tigeot };
490c55ada2SFrançois Tigeot
503f2dd94aSFrançois Tigeot void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
513f2dd94aSFrançois Tigeot
520c55ada2SFrançois Tigeot typedef struct {
5388c5318eSFrançois Tigeot struct lock lock;
543f2dd94aSFrançois Tigeot struct list_head head;
5524b50f1aSFrançois Tigeot } wait_queue_head_t;
5624b50f1aSFrançois Tigeot
57e2a4a6b1SFrançois Tigeot void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
58e2a4a6b1SFrançois Tigeot
5924b50f1aSFrançois Tigeot static inline void
init_waitqueue_head(wait_queue_head_t * q)60e2a4a6b1SFrançois Tigeot init_waitqueue_head(wait_queue_head_t *q)
6124b50f1aSFrançois Tigeot {
62e2a4a6b1SFrançois Tigeot __init_waitqueue_head(q, "", NULL);
6324b50f1aSFrançois Tigeot }
6424b50f1aSFrançois Tigeot
65d658c120SFrançois Tigeot void __wake_up_core(wait_queue_head_t *q, int num_to_wake_up);
66d658c120SFrançois Tigeot
67d658c120SFrançois Tigeot static inline void
wake_up(wait_queue_head_t * q)68d658c120SFrançois Tigeot wake_up(wait_queue_head_t *q)
69d658c120SFrançois Tigeot {
70d658c120SFrançois Tigeot lockmgr(&q->lock, LK_EXCLUSIVE);
71d658c120SFrançois Tigeot __wake_up_core(q, 1);
72d658c120SFrançois Tigeot lockmgr(&q->lock, LK_RELEASE);
7306f0d6a9SFrançois Tigeot wakeup_one(q);
74d658c120SFrançois Tigeot }
75d658c120SFrançois Tigeot
76d658c120SFrançois Tigeot static inline void
wake_up_all(wait_queue_head_t * q)77d658c120SFrançois Tigeot wake_up_all(wait_queue_head_t *q)
78d658c120SFrançois Tigeot {
79d658c120SFrançois Tigeot lockmgr(&q->lock, LK_EXCLUSIVE);
80d658c120SFrançois Tigeot __wake_up_core(q, 0);
81d658c120SFrançois Tigeot lockmgr(&q->lock, LK_RELEASE);
8206f0d6a9SFrançois Tigeot wakeup(q);
83d658c120SFrançois Tigeot }
84d658c120SFrançois Tigeot
8567c69cdfSFrançois Tigeot void wake_up_bit(void *, int);
8667c69cdfSFrançois Tigeot
87d658c120SFrançois Tigeot #define wake_up_all_locked(eq) __wake_up_core(eq, 0)
88d658c120SFrançois Tigeot
89d658c120SFrançois Tigeot #define wake_up_interruptible(eq) wake_up(eq)
90d658c120SFrançois Tigeot #define wake_up_interruptible_all(eq) wake_up_all(eq)
91a6796b4cSFrançois Tigeot
92edbc586dSFrançois Tigeot void __wait_event_prefix(wait_queue_head_t *wq, int flags);
933f2dd94aSFrançois Tigeot void prepare_to_wait(wait_queue_head_t *q, wait_queue_entry_t *wait, int state);
943f2dd94aSFrançois Tigeot void finish_wait(wait_queue_head_t *q, wait_queue_entry_t *wait);
95edbc586dSFrançois Tigeot
9619b28dc8SFrançois Tigeot /*
9763c3939fSImre Vadasz * wait_event_interruptible_timeout:
9819b28dc8SFrançois Tigeot * - The process is put to sleep until the condition evaluates to true.
9919b28dc8SFrançois Tigeot * - The condition is checked each time the waitqueue wq is woken up.
10019b28dc8SFrançois Tigeot * - wake_up has to be called after changing any variable that could change
10119b28dc8SFrançois Tigeot * the result of the wait condition.
10219b28dc8SFrançois Tigeot *
10319b28dc8SFrançois Tigeot * returns:
10419b28dc8SFrançois Tigeot * - 0 if the timeout elapsed
10519b28dc8SFrançois Tigeot * - the remaining jiffies if the condition evaluated to true before
10619b28dc8SFrançois Tigeot * the timeout elapsed.
10719b28dc8SFrançois Tigeot * - remaining jiffies are always at least 1
10863c3939fSImre Vadasz * - -ERESTARTSYS if interrupted by a signal (when PCATCH is set in flags)
10919b28dc8SFrançois Tigeot */
1105a6e97faSFrançois Tigeot #define __wait_event_common(wq, condition, timeout_jiffies, flags, \
1115a6e97faSFrançois Tigeot locked) \
11219b28dc8SFrançois Tigeot ({ \
113fbf82e35SFrançois Tigeot int start_jiffies, elapsed_jiffies, remaining_jiffies, ret; \
11419b28dc8SFrançois Tigeot bool timeout_expired = false; \
11563c3939fSImre Vadasz bool interrupted = false; \
11619b28dc8SFrançois Tigeot long retval; \
1172e29c338SFrançois Tigeot int state; \
1182e29c338SFrançois Tigeot DEFINE_WAIT(tmp_wq); \
11919b28dc8SFrançois Tigeot \
12019b28dc8SFrançois Tigeot start_jiffies = ticks; \
1212e29c338SFrançois Tigeot state = (flags & PCATCH) ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE; \
1222e29c338SFrançois Tigeot prepare_to_wait(&wq, &tmp_wq, state); \
12319b28dc8SFrançois Tigeot \
12419b28dc8SFrançois Tigeot while (1) { \
125edbc586dSFrançois Tigeot __wait_event_prefix(&wq, flags); \
12685bc18adSFrançois Tigeot \
12719b28dc8SFrançois Tigeot if (condition) \
12819b28dc8SFrançois Tigeot break; \
12919b28dc8SFrançois Tigeot \
1308440b9cdSFrançois Tigeot tsleep_interlock(current, flags); \
1318440b9cdSFrançois Tigeot \
1322e29c338SFrançois Tigeot if ((timeout_jiffies) != 0) { \
1332e29c338SFrançois Tigeot ret = tsleep(current, PINTERLOCKED|flags, "lwe", timeout_jiffies); \
1342e29c338SFrançois Tigeot } else { \
1352e29c338SFrançois Tigeot ret = tsleep(current, PINTERLOCKED|flags, "lwe", hz);\
1362e29c338SFrançois Tigeot if (ret == EWOULDBLOCK) { \
137*78973132SSergey Zigachev /*kprintf("F");*/ \
138*78973132SSergey Zigachev /*print_backtrace(-1);*/ \
1392e29c338SFrançois Tigeot ret = 0; \
1402e29c338SFrançois Tigeot } \
1412e29c338SFrançois Tigeot } \
1428440b9cdSFrançois Tigeot \
14363c3939fSImre Vadasz if (ret == EINTR || ret == ERESTART) { \
14463c3939fSImre Vadasz interrupted = true; \
14563c3939fSImre Vadasz break; \
14663c3939fSImre Vadasz } \
14719b28dc8SFrançois Tigeot if (ret == EWOULDBLOCK) { \
14819b28dc8SFrançois Tigeot timeout_expired = true; \
14919b28dc8SFrançois Tigeot break; \
15019b28dc8SFrançois Tigeot } \
15119b28dc8SFrançois Tigeot } \
15219b28dc8SFrançois Tigeot \
15319b28dc8SFrançois Tigeot elapsed_jiffies = ticks - start_jiffies; \
15419b28dc8SFrançois Tigeot remaining_jiffies = timeout_jiffies - elapsed_jiffies; \
15563c3939fSImre Vadasz if (remaining_jiffies <= 0) \
15619b28dc8SFrançois Tigeot remaining_jiffies = 1; \
15719b28dc8SFrançois Tigeot \
15819b28dc8SFrançois Tigeot if (timeout_expired) \
15919b28dc8SFrançois Tigeot retval = 0; \
16063c3939fSImre Vadasz else if (interrupted) \
16163c3939fSImre Vadasz retval = -ERESTARTSYS; \
16263c3939fSImre Vadasz else if (timeout_jiffies > 0) \
16319b28dc8SFrançois Tigeot retval = remaining_jiffies; \
16463c3939fSImre Vadasz else \
16563c3939fSImre Vadasz retval = 1; \
16619b28dc8SFrançois Tigeot \
167edbc586dSFrançois Tigeot finish_wait(&wq, &tmp_wq); \
16819b28dc8SFrançois Tigeot retval; \
16919b28dc8SFrançois Tigeot })
17019b28dc8SFrançois Tigeot
171fbf82e35SFrançois Tigeot #define wait_event(wq, condition) \
1725a6e97faSFrançois Tigeot __wait_event_common(wq, condition, 0, 0, false)
173fbf82e35SFrançois Tigeot
17419b28dc8SFrançois Tigeot #define wait_event_timeout(wq, condition, timeout) \
1755a6e97faSFrançois Tigeot __wait_event_common(wq, condition, timeout, 0, false)
17619b28dc8SFrançois Tigeot
177e434fbb5SFrançois Tigeot #define wait_event_interruptible(wq, condition) \
17863c3939fSImre Vadasz ({ \
17963c3939fSImre Vadasz long retval; \
18063c3939fSImre Vadasz \
1815a6e97faSFrançois Tigeot retval = __wait_event_common(wq, condition, 0, PCATCH, false); \
1825a6e97faSFrançois Tigeot if (retval != -ERESTARTSYS) \
1835a6e97faSFrançois Tigeot retval = 0; \
1845a6e97faSFrançois Tigeot retval; \
1855a6e97faSFrançois Tigeot })
1865a6e97faSFrançois Tigeot
1875a6e97faSFrançois Tigeot #define wait_event_interruptible_locked(wq, condition) \
1885a6e97faSFrançois Tigeot ({ \
1895a6e97faSFrançois Tigeot long retval; \
1905a6e97faSFrançois Tigeot \
1915a6e97faSFrançois Tigeot retval = __wait_event_common(wq, condition, 0, PCATCH, true); \
19263c3939fSImre Vadasz if (retval != -ERESTARTSYS) \
19363c3939fSImre Vadasz retval = 0; \
19463c3939fSImre Vadasz retval; \
19563c3939fSImre Vadasz })
196e434fbb5SFrançois Tigeot
19719b28dc8SFrançois Tigeot #define wait_event_interruptible_timeout(wq, condition, timeout) \
1985a6e97faSFrançois Tigeot __wait_event_common(wq, condition, timeout, PCATCH, false)
19919b28dc8SFrançois Tigeot
200c0e97ee6SFrançois Tigeot static inline int
waitqueue_active(wait_queue_head_t * q)201c0e97ee6SFrançois Tigeot waitqueue_active(wait_queue_head_t *q)
202c0e97ee6SFrançois Tigeot {
2033f2dd94aSFrançois Tigeot return !list_empty(&q->head);
204d658c120SFrançois Tigeot }
2050c55ada2SFrançois Tigeot
206a85cb24fSFrançois Tigeot #define DEFINE_WAIT_FUNC(name, _function) \
2073f2dd94aSFrançois Tigeot wait_queue_entry_t name = { \
208a85cb24fSFrançois Tigeot .private = current, \
2093f2dd94aSFrançois Tigeot .entry = LIST_HEAD_INIT((name).entry), \
210a85cb24fSFrançois Tigeot .func = _function, \
211a85cb24fSFrançois Tigeot }
212a85cb24fSFrançois Tigeot
2133f2dd94aSFrançois Tigeot #define DEFINE_WAIT(name) \
2143f2dd94aSFrançois Tigeot DEFINE_WAIT_FUNC((name), autoremove_wake_function)
2153f2dd94aSFrançois Tigeot
2160c55ada2SFrançois Tigeot static inline void
__add_wait_queue(wait_queue_head_t * head,wait_queue_entry_t * new)2173f2dd94aSFrançois Tigeot __add_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *new)
218252524e5SFrançois Tigeot {
2193f2dd94aSFrançois Tigeot list_add(&new->entry, &head->head);
220252524e5SFrançois Tigeot }
221252524e5SFrançois Tigeot
222580a27abSFrançois Tigeot static inline void
add_wait_queue(wait_queue_head_t * head,wait_queue_entry_t * wait)2233f2dd94aSFrançois Tigeot add_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *wait)
224580a27abSFrançois Tigeot {
225580a27abSFrançois Tigeot lockmgr(&head->lock, LK_EXCLUSIVE);
2263f2dd94aSFrançois Tigeot __add_wait_queue(head, wait);
227580a27abSFrançois Tigeot lockmgr(&head->lock, LK_RELEASE);
228580a27abSFrançois Tigeot }
229580a27abSFrançois Tigeot
23015dba7a9SFrançois Tigeot #define DECLARE_WAIT_QUEUE_HEAD(name) \
23115dba7a9SFrançois Tigeot wait_queue_head_t name = { \
232d658c120SFrançois Tigeot .lock = LOCK_INITIALIZER("name", 0, LK_CANRECURSE), \
2333f2dd94aSFrançois Tigeot .head = { &(name).head, &(name).head } \
23415dba7a9SFrançois Tigeot }
23515dba7a9SFrançois Tigeot
236252524e5SFrançois Tigeot static inline void
__remove_wait_queue(wait_queue_head_t * head,wait_queue_entry_t * old)2373f2dd94aSFrançois Tigeot __remove_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *old)
238252524e5SFrançois Tigeot {
2393f2dd94aSFrançois Tigeot list_del(&old->entry);
240252524e5SFrançois Tigeot }
241252524e5SFrançois Tigeot
242580a27abSFrançois Tigeot static inline void
remove_wait_queue(wait_queue_head_t * head,wait_queue_entry_t * wq)2433f2dd94aSFrançois Tigeot remove_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *wq)
244580a27abSFrançois Tigeot {
245580a27abSFrançois Tigeot lockmgr(&head->lock, LK_EXCLUSIVE);
246580a27abSFrançois Tigeot __remove_wait_queue(head, wq);
247580a27abSFrançois Tigeot lockmgr(&head->lock, LK_RELEASE);
248580a27abSFrançois Tigeot }
249580a27abSFrançois Tigeot
250d43f67b3SFrançois Tigeot static inline void
__add_wait_queue_entry_tail(wait_queue_head_t * wqh,wait_queue_entry_t * wq)2513f2dd94aSFrançois Tigeot __add_wait_queue_entry_tail(wait_queue_head_t *wqh, wait_queue_entry_t *wq)
252d43f67b3SFrançois Tigeot {
2533f2dd94aSFrançois Tigeot list_add_tail(&wq->entry, &wqh->head);
254d43f67b3SFrançois Tigeot }
255d43f67b3SFrançois Tigeot
256a6796b4cSFrançois Tigeot #endif /* _LINUX_WAIT_H_ */
257