| [f761f1eb] | 1 | /*
|
|---|
| [df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
|---|
| [f761f1eb] | 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 |
|
|---|
| [e88eb48] | 29 | /** @addtogroup kernel_sync
|
|---|
| [b45c443] | 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| [06e1e95] | 35 | #ifndef KERN_SPINLOCK_H_
|
|---|
| 36 | #define KERN_SPINLOCK_H_
|
|---|
| [f761f1eb] | 37 |
|
|---|
| [63e27ef] | 38 | #include <assert.h>
|
|---|
| [78de83de] | 39 | #include <stdatomic.h>
|
|---|
| 40 | #include <stdbool.h>
|
|---|
| [c842f04] | 41 | #include <preemption.h>
|
|---|
| [42bbbe2] | 42 | #include <arch/asm.h>
|
|---|
| [f761f1eb] | 43 |
|
|---|
| [5f85c91] | 44 | #ifdef CONFIG_SMP
|
|---|
| [90c8b8d] | 45 |
|
|---|
| [cc106e4] | 46 | typedef struct spinlock {
|
|---|
| [78de83de] | 47 | atomic_flag flag;
|
|---|
| [a35b458] | 48 |
|
|---|
| [2d93f1f9] | 49 | #ifdef CONFIG_DEBUG_SPINLOCK
|
|---|
| [a000878c] | 50 | const char *name;
|
|---|
| [2b4a9f26] | 51 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| [e71a61d] | 52 | } spinlock_t;
|
|---|
| [f761f1eb] | 53 |
|
|---|
| [dc747e3] | 54 | /*
|
|---|
| 55 | * SPINLOCK_DECLARE is to be used for dynamically allocated spinlocks,
|
|---|
| 56 | * where the lock gets initialized in run time.
|
|---|
| 57 | */
|
|---|
| [90c8b8d] | 58 | #define SPINLOCK_DECLARE(lock_name) spinlock_t lock_name
|
|---|
| 59 | #define SPINLOCK_EXTERN(lock_name) extern spinlock_t lock_name
|
|---|
| [dc747e3] | 60 |
|
|---|
| 61 | /*
|
|---|
| [2b4a9f26] | 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.
|
|---|
| [dc747e3] | 65 | */
|
|---|
| 66 | #ifdef CONFIG_DEBUG_SPINLOCK
|
|---|
| [90c8b8d] | 67 |
|
|---|
| 68 | #define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 69 | spinlock_t lock_name = { \
|
|---|
| 70 | .name = desc_name, \
|
|---|
| [78de83de] | 71 | .flag = ATOMIC_FLAG_INIT \
|
|---|
| [dc747e3] | 72 | }
|
|---|
| [90c8b8d] | 73 |
|
|---|
| 74 | #define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 75 | static spinlock_t lock_name = { \
|
|---|
| 76 | .name = desc_name, \
|
|---|
| [78de83de] | 77 | .flag = ATOMIC_FLAG_INIT \
|
|---|
| [dc747e3] | 78 | }
|
|---|
| 79 |
|
|---|
| [2b4a9f26] | 80 | #define ASSERT_SPINLOCK(expr, lock) \
|
|---|
| [63e27ef] | 81 | assert_verbose(expr, (lock)->name)
|
|---|
| [2b4a9f26] | 82 |
|
|---|
| 83 | #define spinlock_lock(lock) spinlock_lock_debug((lock))
|
|---|
| 84 | #define spinlock_unlock(lock) spinlock_unlock_debug((lock))
|
|---|
| [53f9821] | 85 |
|
|---|
| [2b4a9f26] | 86 | #else /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| [90c8b8d] | 87 |
|
|---|
| 88 | #define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 89 | spinlock_t lock_name = { \
|
|---|
| [78de83de] | 90 | .flag = ATOMIC_FLAG_INIT \
|
|---|
| [90c8b8d] | 91 | }
|
|---|
| 92 |
|
|---|
| 93 | #define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 94 | static spinlock_t lock_name = { \
|
|---|
| [78de83de] | 95 | .flag = ATOMIC_FLAG_INIT \
|
|---|
| [90c8b8d] | 96 | }
|
|---|
| 97 |
|
|---|
| [2b4a9f26] | 98 | #define ASSERT_SPINLOCK(expr, lock) \
|
|---|
| [63e27ef] | 99 | assert(expr)
|
|---|
| [90c8b8d] | 100 |
|
|---|
| [543662b] | 101 | /** Acquire spinlock
|
|---|
| 102 | *
|
|---|
| 103 | * @param lock Pointer to spinlock_t structure.
|
|---|
| 104 | */
|
|---|
| 105 | NO_TRACE static inline void spinlock_lock(spinlock_t *lock)
|
|---|
| 106 | {
|
|---|
| 107 | preemption_disable();
|
|---|
| 108 | while (atomic_flag_test_and_set_explicit(&lock->flag,
|
|---|
| 109 | memory_order_acquire))
|
|---|
| 110 | ;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /** Release spinlock
|
|---|
| 114 | *
|
|---|
| 115 | * @param lock Pointer to spinlock_t structure.
|
|---|
| 116 | */
|
|---|
| 117 | NO_TRACE static inline void spinlock_unlock(spinlock_t *lock)
|
|---|
| 118 | {
|
|---|
| 119 | atomic_flag_clear_explicit(&lock->flag, memory_order_release);
|
|---|
| 120 | preemption_enable();
|
|---|
| 121 | }
|
|---|
| [2b4a9f26] | 122 |
|
|---|
| 123 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| [53f9821] | 124 |
|
|---|
| [90c8b8d] | 125 | #define SPINLOCK_INITIALIZE(lock_name) \
|
|---|
| 126 | SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
|
|---|
| 127 |
|
|---|
| 128 | #define SPINLOCK_STATIC_INITIALIZE(lock_name) \
|
|---|
| 129 | SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
|
|---|
| 130 |
|
|---|
| [2b4a9f26] | 131 | extern void spinlock_initialize(spinlock_t *, const char *);
|
|---|
| [89ea2dc] | 132 | extern bool spinlock_trylock(spinlock_t *);
|
|---|
| [2b4a9f26] | 133 | extern void spinlock_lock_debug(spinlock_t *);
|
|---|
| 134 | extern void spinlock_unlock_debug(spinlock_t *);
|
|---|
| [ffe4a87] | 135 | extern bool spinlock_locked(spinlock_t *);
|
|---|
| [90c8b8d] | 136 |
|
|---|
| [31d8e10] | 137 | #ifdef CONFIG_DEBUG_SPINLOCK
|
|---|
| 138 |
|
|---|
| [b2fa1204] | 139 | #include <log.h>
|
|---|
| [90c8b8d] | 140 |
|
|---|
| 141 | #define DEADLOCK_THRESHOLD 100000000
|
|---|
| [31d8e10] | 142 |
|
|---|
| [90c8b8d] | 143 | #define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
|
|---|
| 144 |
|
|---|
| 145 | #define DEADLOCK_PROBE(pname, value) \
|
|---|
| 146 | if ((pname)++ > (value)) { \
|
|---|
| 147 | (pname) = 0; \
|
|---|
| [b2fa1204] | 148 | log(LF_OTHER, LVL_WARN, \
|
|---|
| 149 | "Deadlock probe %s: exceeded threshold %u\n" \
|
|---|
| [90c8b8d] | 150 | "cpu%u: function=%s, line=%u\n", \
|
|---|
| 151 | #pname, (value), CPU->id, __func__, __LINE__); \
|
|---|
| [31d8e10] | 152 | }
|
|---|
| [90c8b8d] | 153 |
|
|---|
| [2b4a9f26] | 154 | #else /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| [90c8b8d] | 155 |
|
|---|
| [31d8e10] | 156 | #define DEADLOCK_PROBE_INIT(pname)
|
|---|
| 157 | #define DEADLOCK_PROBE(pname, value)
|
|---|
| [90c8b8d] | 158 |
|
|---|
| [2b4a9f26] | 159 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| [31d8e10] | 160 |
|
|---|
| [90c8b8d] | 161 | #else /* CONFIG_SMP */
|
|---|
| [f761f1eb] | 162 |
|
|---|
| [dc747e3] | 163 | /* On UP systems, spinlocks are effectively left out. */
|
|---|
| [90c8b8d] | 164 |
|
|---|
| [cc106e4] | 165 | /* Allow the use of spinlock_t as an incomplete type. */
|
|---|
| 166 | typedef struct spinlock spinlock_t;
|
|---|
| 167 |
|
|---|
| [dc747e3] | 168 | #define SPINLOCK_DECLARE(name)
|
|---|
| [8be8cfa] | 169 | #define SPINLOCK_EXTERN(name)
|
|---|
| [90c8b8d] | 170 |
|
|---|
| [dc747e3] | 171 | #define SPINLOCK_INITIALIZE(name)
|
|---|
| [90c8b8d] | 172 | #define SPINLOCK_STATIC_INITIALIZE(name)
|
|---|
| 173 |
|
|---|
| 174 | #define SPINLOCK_INITIALIZE_NAME(name, desc_name)
|
|---|
| 175 | #define SPINLOCK_STATIC_INITIALIZE_NAME(name, desc_name)
|
|---|
| 176 |
|
|---|
| [63e27ef] | 177 | #define ASSERT_SPINLOCK(expr, lock) assert(expr)
|
|---|
| [2b4a9f26] | 178 |
|
|---|
| [90c8b8d] | 179 | #define spinlock_initialize(lock, name)
|
|---|
| [f761f1eb] | 180 |
|
|---|
| [90c8b8d] | 181 | #define spinlock_lock(lock) preemption_disable()
|
|---|
| [cc106e4] | 182 | #define spinlock_trylock(lock) ({ preemption_disable(); 1; })
|
|---|
| [90c8b8d] | 183 | #define spinlock_unlock(lock) preemption_enable()
|
|---|
| [ffe4a87] | 184 | #define spinlock_locked(lock) 1
|
|---|
| 185 | #define spinlock_unlocked(lock) 1
|
|---|
| [f761f1eb] | 186 |
|
|---|
| [31d8e10] | 187 | #define DEADLOCK_PROBE_INIT(pname)
|
|---|
| 188 | #define DEADLOCK_PROBE(pname, value)
|
|---|
| 189 |
|
|---|
| [2b4a9f26] | 190 | #endif /* CONFIG_SMP */
|
|---|
| 191 |
|
|---|
| 192 | typedef struct {
|
|---|
| [8aa9265] | 193 | SPINLOCK_DECLARE(lock); /**< Spinlock */
|
|---|
| 194 | bool guard; /**< Flag whether ipl is valid */
|
|---|
| 195 | ipl_t ipl; /**< Original interrupt level */
|
|---|
| [2b4a9f26] | 196 | } irq_spinlock_t;
|
|---|
| 197 |
|
|---|
| 198 | #define IRQ_SPINLOCK_DECLARE(lock_name) irq_spinlock_t lock_name
|
|---|
| 199 | #define IRQ_SPINLOCK_EXTERN(lock_name) extern irq_spinlock_t lock_name
|
|---|
| 200 |
|
|---|
| [8aa9265] | 201 | #ifdef CONFIG_SMP
|
|---|
| 202 |
|
|---|
| [2b4a9f26] | 203 | #define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
|
|---|
| 204 | ASSERT_SPINLOCK(expr, &((irq_lock)->lock))
|
|---|
| 205 |
|
|---|
| 206 | /*
|
|---|
| 207 | * IRQ_SPINLOCK_INITIALIZE and IRQ_SPINLOCK_STATIC_INITIALIZE are to be used
|
|---|
| 208 | * for statically allocated interrupts-disabled spinlocks. They declare (either
|
|---|
| 209 | * as global or static symbol) and initialize the lock.
|
|---|
| 210 | */
|
|---|
| 211 | #ifdef CONFIG_DEBUG_SPINLOCK
|
|---|
| 212 |
|
|---|
| 213 | #define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 214 | irq_spinlock_t lock_name = { \
|
|---|
| 215 | .lock = { \
|
|---|
| 216 | .name = desc_name, \
|
|---|
| [78de83de] | 217 | .flag = ATOMIC_FLAG_INIT \
|
|---|
| [2b4a9f26] | 218 | }, \
|
|---|
| 219 | .guard = false, \
|
|---|
| 220 | .ipl = 0 \
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | #define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 224 | static irq_spinlock_t lock_name = { \
|
|---|
| 225 | .lock = { \
|
|---|
| 226 | .name = desc_name, \
|
|---|
| [78de83de] | 227 | .flag = ATOMIC_FLAG_INIT \
|
|---|
| [2b4a9f26] | 228 | }, \
|
|---|
| 229 | .guard = false, \
|
|---|
| 230 | .ipl = 0 \
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | #else /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| 234 |
|
|---|
| 235 | #define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 236 | irq_spinlock_t lock_name = { \
|
|---|
| 237 | .lock = { \
|
|---|
| [78de83de] | 238 | .flag = ATOMIC_FLAG_INIT \
|
|---|
| [2b4a9f26] | 239 | }, \
|
|---|
| 240 | .guard = false, \
|
|---|
| 241 | .ipl = 0 \
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | #define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 245 | static irq_spinlock_t lock_name = { \
|
|---|
| 246 | .lock = { \
|
|---|
| [78de83de] | 247 | .flag = ATOMIC_FLAG_INIT \
|
|---|
| [2b4a9f26] | 248 | }, \
|
|---|
| 249 | .guard = false, \
|
|---|
| 250 | .ipl = 0 \
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| 254 |
|
|---|
| [8aa9265] | 255 | #else /* CONFIG_SMP */
|
|---|
| 256 |
|
|---|
| 257 | /*
|
|---|
| 258 | * Since the spinlocks are void on UP systems, we also need
|
|---|
| 259 | * to have a special variant of interrupts-disabled spinlock
|
|---|
| 260 | * macros which take this into account.
|
|---|
| 261 | */
|
|---|
| 262 |
|
|---|
| 263 | #define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
|
|---|
| 264 | ASSERT_SPINLOCK(expr, NULL)
|
|---|
| 265 |
|
|---|
| 266 | #define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 267 | irq_spinlock_t lock_name = { \
|
|---|
| 268 | .guard = false, \
|
|---|
| 269 | .ipl = 0 \
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | #define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 273 | static irq_spinlock_t lock_name = { \
|
|---|
| 274 | .guard = false, \
|
|---|
| 275 | .ipl = 0 \
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | #endif /* CONFIG_SMP */
|
|---|
| 279 |
|
|---|
| [2b4a9f26] | 280 | #define IRQ_SPINLOCK_INITIALIZE(lock_name) \
|
|---|
| 281 | IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
|
|---|
| 282 |
|
|---|
| 283 | #define IRQ_SPINLOCK_STATIC_INITIALIZE(lock_name) \
|
|---|
| 284 | IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
|
|---|
| 285 |
|
|---|
| [a9f1372] | 286 | extern void irq_spinlock_initialize(irq_spinlock_t *, const char *);
|
|---|
| 287 | extern void irq_spinlock_lock(irq_spinlock_t *, bool);
|
|---|
| 288 | extern void irq_spinlock_unlock(irq_spinlock_t *, bool);
|
|---|
| [89ea2dc] | 289 | extern bool irq_spinlock_trylock(irq_spinlock_t *);
|
|---|
| [a9f1372] | 290 | extern void irq_spinlock_pass(irq_spinlock_t *, irq_spinlock_t *);
|
|---|
| 291 | extern void irq_spinlock_exchange(irq_spinlock_t *, irq_spinlock_t *);
|
|---|
| [ffe4a87] | 292 | extern bool irq_spinlock_locked(irq_spinlock_t *);
|
|---|
| [f761f1eb] | 293 |
|
|---|
| 294 | #endif
|
|---|
| [b45c443] | 295 |
|
|---|
| [06e1e95] | 296 | /** @}
|
|---|
| [b45c443] | 297 | */
|
|---|