1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2015, Linaro Limited
4  */
5 #ifndef KERNEL_WAIT_QUEUE_H
6 #define KERNEL_WAIT_QUEUE_H
7 
8 #include <types_ext.h>
9 #include <sys/queue.h>
10 
11 struct wait_queue_elem;
12 SLIST_HEAD(wait_queue, wait_queue_elem);
13 
14 #define WAIT_QUEUE_INITIALIZER { .slh_first = NULL }
15 
16 struct condvar;
17 struct wait_queue_elem {
18 	short handle;
19 	bool done;
20 	bool wait_read;
21 	struct condvar *cv;
22 	SLIST_ENTRY(wait_queue_elem) link;
23 };
24 
25 /*
26  * Initializes a wait queue
27  */
28 void wq_init(struct wait_queue *wq);
29 
30 /*
31  * Initializes a wait queue element and adds it to the wait queue.  This
32  * function is supposed to be called before the lock that protects the
33  * resource we need to wait for is released.
34  *
35  * One call to this function must be followed by one call to wq_wait_final()
36  * on the same wait queue element.
37  */
38 void wq_wait_init_condvar(struct wait_queue *wq, struct wait_queue_elem *wqe,
39 			struct condvar *cv, bool wait_read);
40 
wq_wait_init(struct wait_queue * wq,struct wait_queue_elem * wqe,bool wait_read)41 static inline void wq_wait_init(struct wait_queue *wq,
42 			struct wait_queue_elem *wqe, bool wait_read)
43 {
44 	wq_wait_init_condvar(wq, wqe, NULL, wait_read);
45 }
46 
47 /* Waits for the wait queue element to the awakened. */
48 void wq_wait_final(struct wait_queue *wq, struct wait_queue_elem *wqe,
49 		   const void *sync_obj, const char *fname, int lineno);
50 
51 /* Wakes up the first wait queue element in the wait queue, if there is one */
52 void wq_wake_next(struct wait_queue *wq, const void *sync_obj,
53 		const char *fname, int lineno);
54 
55 /* Returns true if the wait queue doesn't contain any elements */
56 bool wq_is_empty(struct wait_queue *wq);
57 
58 void wq_promote_condvar(struct wait_queue *wq, struct condvar *cv,
59 			bool only_one, const void *sync_obj, const char *fname,
60 			int lineno);
61 bool wq_have_condvar(struct wait_queue *wq, struct condvar *cv);
62 
63 #endif /*KERNEL_WAIT_QUEUE_H*/
64 
65