Update from upstream to 2.4.0 version
[platform/core/security/tef-optee_os.git] / core / arch / arm / include / kernel / mutex.h
1 /*
2  * Copyright (c) 2014, 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_MUTEX_H
28 #define KERNEL_MUTEX_H
29
30 #include <types_ext.h>
31 #include <sys/queue.h>
32 #include <kernel/wait_queue.h>
33
34 enum mutex_value {
35         MUTEX_VALUE_UNLOCKED,
36         MUTEX_VALUE_LOCKED,
37 };
38
39 /*
40  * Positive owner ids signifies actual threads, negative ids has special
41  * meanings according to the defines below. Note that only the first of the
42  * defines is allowed in struct mutex::owener_id.
43  */
44 #define MUTEX_OWNER_ID_NONE             -1
45 #define MUTEX_OWNER_ID_CONDVAR_SLEEP    -2
46 #define MUTEX_OWNER_ID_MUTEX_UNLOCK     -3
47
48 struct mutex {
49         enum mutex_value value;
50         unsigned spin_lock;     /* used when operating on this struct */
51         struct wait_queue wq;
52         int owner_id;
53         TAILQ_ENTRY(mutex) link;
54 };
55 #define MUTEX_INITIALIZER \
56         { .value = MUTEX_VALUE_UNLOCKED, .owner_id = MUTEX_OWNER_ID_NONE, \
57           .wq = WAIT_QUEUE_INITIALIZER, }
58
59 TAILQ_HEAD(mutex_head, mutex);
60
61 void mutex_init(struct mutex *m);
62 void mutex_destroy(struct mutex *m);
63
64 #ifdef CFG_MUTEX_DEBUG
65 void mutex_unlock_debug(struct mutex *m, const char *fname, int lineno);
66 #define mutex_unlock(m) mutex_unlock_debug((m), __FILE__, __LINE__)
67
68 void mutex_lock_debug(struct mutex *m, const char *fname, int lineno);
69 #define mutex_lock(m) mutex_lock_debug((m), __FILE__, __LINE__)
70
71 bool mutex_trylock_debug(struct mutex *m, const char *fname, int lineno);
72 #define mutex_trylock(m) mutex_trylock_debug((m), __FILE__, __LINE__)
73
74 #else
75 void mutex_unlock(struct mutex *m);
76 void mutex_lock(struct mutex *m);
77 bool mutex_trylock(struct mutex *m);
78 #endif
79
80
81 struct condvar {
82         unsigned spin_lock;
83         struct mutex *m;
84 };
85 #define CONDVAR_INITIALIZER { .m = NULL }
86
87 void condvar_init(struct condvar *cv);
88 void condvar_destroy(struct condvar *cv);
89
90 #ifdef CFG_MUTEX_DEBUG
91 void condvar_signal_debug(struct condvar *cv, const char *fname, int lineno);
92 #define condvar_signal(cv) condvar_signal_debug((cv), __FILE__, __LINE__)
93
94 void condvar_broadcast_debug(struct condvar *cv, const char *fname, int lineno);
95 #define condvar_broadcast(cv) condvar_broadcast_debug((cv), __FILE__, __LINE__)
96
97 void condvar_wait_debug(struct condvar *cv, struct mutex *m,
98                         const char *fname, int lineno);
99 #define condvar_wait(cv, m) condvar_wait_debug((cv), (m), __FILE__, __LINE__)
100 #else
101 void condvar_signal(struct condvar *cv);
102 void condvar_broadcast(struct condvar *cv);
103 void condvar_wait(struct condvar *cv, struct mutex *m);
104 #endif
105
106 #endif /*KERNEL_MUTEX_H*/
107