Update from upstream to 2.4.0 version
[platform/core/security/tef-optee_os.git] / core / arch / arm / include / kernel / wait_queue.h
1 /*
2  * Copyright (c) 2015, Linaro Limited
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #ifndef KERNEL_WAIT_QUEUE_H
28 #define KERNEL_WAIT_QUEUE_H
29
30 #include <types_ext.h>
31 #include <sys/queue.h>
32
33 struct wait_queue_elem;
34 SLIST_HEAD(wait_queue, wait_queue_elem);
35
36 #define WAIT_QUEUE_INITIALIZER { .slh_first = NULL }
37
38 struct condvar;
39 struct wait_queue_elem {
40         short handle;
41         bool done;
42         struct condvar *cv;
43         SLIST_ENTRY(wait_queue_elem) link;
44 };
45
46 /*
47  * Initializes a wait queue
48  */
49 void wq_init(struct wait_queue *wq);
50
51 /*
52  * Initializes a wait queue element and adds it to the wait queue.  This
53  * function is supposed to be called before the lock that protects the
54  * resource we need to wait for is released.
55  *
56  * One call to this function must be followed by one call to wq_wait_final()
57  * on the same wait queue element.
58  */
59 void wq_wait_init_condvar(struct wait_queue *wq, struct wait_queue_elem *wqe,
60                         struct condvar *cv);
61
62 static inline void wq_wait_init(struct wait_queue *wq,
63                         struct wait_queue_elem *wqe)
64 {
65         wq_wait_init_condvar(wq, wqe, NULL);
66 }
67
68 /* Waits for the wait queue element to the awakened. */
69 void wq_wait_final(struct wait_queue *wq, struct wait_queue_elem *wqe,
70                    const void *sync_obj, int owner, const char *fname,
71                    int lineno);
72
73 /* Wakes up the first wait queue element in the wait queue, if there is one */
74 void wq_wake_one(struct wait_queue *wq, const void *sync_obj,
75                 const char *fname, int lineno);
76
77 /* Returns true if the wait queue doesn't contain any elements */
78 bool wq_is_empty(struct wait_queue *wq);
79
80 void wq_promote_condvar(struct wait_queue *wq, struct condvar *cv,
81                         bool only_one, const void *sync_obj, const char *fname,
82                         int lineno);
83 bool wq_have_condvar(struct wait_queue *wq, struct condvar *cv);
84
85 #endif /*KERNEL_WAIT_QUEUE_H*/
86