1 | /*
|
---|
2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
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
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - 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.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup sync
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #ifndef KERN_SPINLOCK_H_
|
---|
36 | #define KERN_SPINLOCK_H_
|
---|
37 |
|
---|
38 | #include <assert.h>
|
---|
39 | #include <stdatomic.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 | #include <preemption.h>
|
---|
42 | #include <arch/asm.h>
|
---|
43 |
|
---|
44 | #ifdef CONFIG_SMP
|
---|
45 |
|
---|
46 | typedef struct spinlock {
|
---|
47 | atomic_flag flag;
|
---|
48 |
|
---|
49 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
50 | const char *name;
|
---|
51 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
---|
52 | } spinlock_t;
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * SPINLOCK_DECLARE is to be used for dynamically allocated spinlocks,
|
---|
56 | * where the lock gets initialized in run time.
|
---|
57 | */
|
---|
58 | #define SPINLOCK_DECLARE(lock_name) spinlock_t lock_name
|
---|
59 | #define SPINLOCK_EXTERN(lock_name) extern spinlock_t lock_name
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * SPINLOCK_INITIALIZE and SPINLOCK_STATIC_INITIALIZE are to be used
|
---|
63 | * for statically allocated spinlocks. They declare (either as global
|
---|
64 | * or static) symbol and initialize the lock.
|
---|
65 | */
|
---|
66 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
67 |
|
---|
68 | #define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
69 | spinlock_t lock_name = { \
|
---|
70 | .name = desc_name, \
|
---|
71 | .flag = ATOMIC_FLAG_INIT \
|
---|
72 | }
|
---|
73 |
|
---|
74 | #define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
75 | static spinlock_t lock_name = { \
|
---|
76 | .name = desc_name, \
|
---|
77 | .flag = ATOMIC_FLAG_INIT \
|
---|
78 | }
|
---|
79 |
|
---|
80 | #define ASSERT_SPINLOCK(expr, lock) \
|
---|
81 | assert_verbose(expr, (lock)->name)
|
---|
82 |
|
---|
83 | #define spinlock_lock(lock) spinlock_lock_debug((lock))
|
---|
84 | #define spinlock_unlock(lock) spinlock_unlock_debug((lock))
|
---|
85 |
|
---|
86 | #else /* CONFIG_DEBUG_SPINLOCK */
|
---|
87 |
|
---|
88 | #define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
89 | spinlock_t lock_name = { \
|
---|
90 | .flag = ATOMIC_FLAG_INIT \
|
---|
91 | }
|
---|
92 |
|
---|
93 | #define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
94 | static spinlock_t lock_name = { \
|
---|
95 | .flag = ATOMIC_FLAG_INIT \
|
---|
96 | }
|
---|
97 |
|
---|
98 | #define ASSERT_SPINLOCK(expr, lock) \
|
---|
99 | assert(expr)
|
---|
100 |
|
---|
101 | #define spinlock_lock(lock) atomic_lock_arch(&(lock)->val)
|
---|
102 | #define spinlock_unlock(lock) spinlock_unlock_nondebug((lock))
|
---|
103 |
|
---|
104 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
---|
105 |
|
---|
106 | #define SPINLOCK_INITIALIZE(lock_name) \
|
---|
107 | SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
108 |
|
---|
109 | #define SPINLOCK_STATIC_INITIALIZE(lock_name) \
|
---|
110 | SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
111 |
|
---|
112 | extern void spinlock_initialize(spinlock_t *, const char *);
|
---|
113 | extern bool spinlock_trylock(spinlock_t *);
|
---|
114 | extern void spinlock_lock_debug(spinlock_t *);
|
---|
115 | extern void spinlock_unlock_debug(spinlock_t *);
|
---|
116 | extern bool spinlock_locked(spinlock_t *);
|
---|
117 |
|
---|
118 | /** Unlock spinlock
|
---|
119 | *
|
---|
120 | * Unlock spinlock for non-debug kernels.
|
---|
121 | *
|
---|
122 | * @param sl Pointer to spinlock_t structure.
|
---|
123 | *
|
---|
124 | */
|
---|
125 | NO_TRACE static inline void spinlock_unlock_nondebug(spinlock_t *lock)
|
---|
126 | {
|
---|
127 | atomic_flag_clear_explicit(&lock->flag, memory_order_release);
|
---|
128 | preemption_enable();
|
---|
129 | }
|
---|
130 |
|
---|
131 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
132 |
|
---|
133 | #include <log.h>
|
---|
134 |
|
---|
135 | #define DEADLOCK_THRESHOLD 100000000
|
---|
136 |
|
---|
137 | #define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
|
---|
138 |
|
---|
139 | #define DEADLOCK_PROBE(pname, value) \
|
---|
140 | if ((pname)++ > (value)) { \
|
---|
141 | (pname) = 0; \
|
---|
142 | log(LF_OTHER, LVL_WARN, \
|
---|
143 | "Deadlock probe %s: exceeded threshold %u\n" \
|
---|
144 | "cpu%u: function=%s, line=%u\n", \
|
---|
145 | #pname, (value), CPU->id, __func__, __LINE__); \
|
---|
146 | }
|
---|
147 |
|
---|
148 | #else /* CONFIG_DEBUG_SPINLOCK */
|
---|
149 |
|
---|
150 | #define DEADLOCK_PROBE_INIT(pname)
|
---|
151 | #define DEADLOCK_PROBE(pname, value)
|
---|
152 |
|
---|
153 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
---|
154 |
|
---|
155 | #else /* CONFIG_SMP */
|
---|
156 |
|
---|
157 | /* On UP systems, spinlocks are effectively left out. */
|
---|
158 |
|
---|
159 | /* Allow the use of spinlock_t as an incomplete type. */
|
---|
160 | typedef struct spinlock spinlock_t;
|
---|
161 |
|
---|
162 | #define SPINLOCK_DECLARE(name)
|
---|
163 | #define SPINLOCK_EXTERN(name)
|
---|
164 |
|
---|
165 | #define SPINLOCK_INITIALIZE(name)
|
---|
166 | #define SPINLOCK_STATIC_INITIALIZE(name)
|
---|
167 |
|
---|
168 | #define SPINLOCK_INITIALIZE_NAME(name, desc_name)
|
---|
169 | #define SPINLOCK_STATIC_INITIALIZE_NAME(name, desc_name)
|
---|
170 |
|
---|
171 | #define ASSERT_SPINLOCK(expr, lock) assert(expr)
|
---|
172 |
|
---|
173 | #define spinlock_initialize(lock, name)
|
---|
174 |
|
---|
175 | #define spinlock_lock(lock) preemption_disable()
|
---|
176 | #define spinlock_trylock(lock) ({ preemption_disable(); 1; })
|
---|
177 | #define spinlock_unlock(lock) preemption_enable()
|
---|
178 | #define spinlock_locked(lock) 1
|
---|
179 | #define spinlock_unlocked(lock) 1
|
---|
180 |
|
---|
181 | #define DEADLOCK_PROBE_INIT(pname)
|
---|
182 | #define DEADLOCK_PROBE(pname, value)
|
---|
183 |
|
---|
184 | #endif /* CONFIG_SMP */
|
---|
185 |
|
---|
186 | typedef struct {
|
---|
187 | SPINLOCK_DECLARE(lock); /**< Spinlock */
|
---|
188 | bool guard; /**< Flag whether ipl is valid */
|
---|
189 | ipl_t ipl; /**< Original interrupt level */
|
---|
190 | } irq_spinlock_t;
|
---|
191 |
|
---|
192 | #define IRQ_SPINLOCK_DECLARE(lock_name) irq_spinlock_t lock_name
|
---|
193 | #define IRQ_SPINLOCK_EXTERN(lock_name) extern irq_spinlock_t lock_name
|
---|
194 |
|
---|
195 | #ifdef CONFIG_SMP
|
---|
196 |
|
---|
197 | #define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
|
---|
198 | ASSERT_SPINLOCK(expr, &((irq_lock)->lock))
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * IRQ_SPINLOCK_INITIALIZE and IRQ_SPINLOCK_STATIC_INITIALIZE are to be used
|
---|
202 | * for statically allocated interrupts-disabled spinlocks. They declare (either
|
---|
203 | * as global or static symbol) and initialize the lock.
|
---|
204 | */
|
---|
205 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
206 |
|
---|
207 | #define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
208 | irq_spinlock_t lock_name = { \
|
---|
209 | .lock = { \
|
---|
210 | .name = desc_name, \
|
---|
211 | .flag = ATOMIC_FLAG_INIT \
|
---|
212 | }, \
|
---|
213 | .guard = false, \
|
---|
214 | .ipl = 0 \
|
---|
215 | }
|
---|
216 |
|
---|
217 | #define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
218 | static irq_spinlock_t lock_name = { \
|
---|
219 | .lock = { \
|
---|
220 | .name = desc_name, \
|
---|
221 | .flag = ATOMIC_FLAG_INIT \
|
---|
222 | }, \
|
---|
223 | .guard = false, \
|
---|
224 | .ipl = 0 \
|
---|
225 | }
|
---|
226 |
|
---|
227 | #else /* CONFIG_DEBUG_SPINLOCK */
|
---|
228 |
|
---|
229 | #define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
230 | irq_spinlock_t lock_name = { \
|
---|
231 | .lock = { \
|
---|
232 | .flag = ATOMIC_FLAG_INIT \
|
---|
233 | }, \
|
---|
234 | .guard = false, \
|
---|
235 | .ipl = 0 \
|
---|
236 | }
|
---|
237 |
|
---|
238 | #define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
239 | static irq_spinlock_t lock_name = { \
|
---|
240 | .lock = { \
|
---|
241 | .flag = ATOMIC_FLAG_INIT \
|
---|
242 | }, \
|
---|
243 | .guard = false, \
|
---|
244 | .ipl = 0 \
|
---|
245 | }
|
---|
246 |
|
---|
247 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
---|
248 |
|
---|
249 | #else /* CONFIG_SMP */
|
---|
250 |
|
---|
251 | /*
|
---|
252 | * Since the spinlocks are void on UP systems, we also need
|
---|
253 | * to have a special variant of interrupts-disabled spinlock
|
---|
254 | * macros which take this into account.
|
---|
255 | */
|
---|
256 |
|
---|
257 | #define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
|
---|
258 | ASSERT_SPINLOCK(expr, NULL)
|
---|
259 |
|
---|
260 | #define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
261 | irq_spinlock_t lock_name = { \
|
---|
262 | .guard = false, \
|
---|
263 | .ipl = 0 \
|
---|
264 | }
|
---|
265 |
|
---|
266 | #define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
267 | static irq_spinlock_t lock_name = { \
|
---|
268 | .guard = false, \
|
---|
269 | .ipl = 0 \
|
---|
270 | }
|
---|
271 |
|
---|
272 | #endif /* CONFIG_SMP */
|
---|
273 |
|
---|
274 | #define IRQ_SPINLOCK_INITIALIZE(lock_name) \
|
---|
275 | IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
276 |
|
---|
277 | #define IRQ_SPINLOCK_STATIC_INITIALIZE(lock_name) \
|
---|
278 | IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
279 |
|
---|
280 | extern void irq_spinlock_initialize(irq_spinlock_t *, const char *);
|
---|
281 | extern void irq_spinlock_lock(irq_spinlock_t *, bool);
|
---|
282 | extern void irq_spinlock_unlock(irq_spinlock_t *, bool);
|
---|
283 | extern bool irq_spinlock_trylock(irq_spinlock_t *);
|
---|
284 | extern void irq_spinlock_pass(irq_spinlock_t *, irq_spinlock_t *);
|
---|
285 | extern void irq_spinlock_exchange(irq_spinlock_t *, irq_spinlock_t *);
|
---|
286 | extern bool irq_spinlock_locked(irq_spinlock_t *);
|
---|
287 |
|
---|
288 | #endif
|
---|
289 |
|
---|
290 | /** @}
|
---|
291 | */
|
---|