[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 |
|
---|
[06e1e95] | 29 | /** @addtogroup 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 |
|
---|
[2b4a9f26] | 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 */
|
---|
[53f9821] | 105 |
|
---|
[90c8b8d] | 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 |
|
---|
[2b4a9f26] | 112 | extern void spinlock_initialize(spinlock_t *, const char *);
|
---|
[89ea2dc] | 113 | extern bool spinlock_trylock(spinlock_t *);
|
---|
[2b4a9f26] | 114 | extern void spinlock_lock_debug(spinlock_t *);
|
---|
| 115 | extern void spinlock_unlock_debug(spinlock_t *);
|
---|
[ffe4a87] | 116 | extern bool spinlock_locked(spinlock_t *);
|
---|
[90c8b8d] | 117 |
|
---|
[53f9821] | 118 | /** Unlock spinlock
|
---|
| 119 | *
|
---|
[13108f24] | 120 | * Unlock spinlock for non-debug kernels.
|
---|
[53f9821] | 121 | *
|
---|
| 122 | * @param sl Pointer to spinlock_t structure.
|
---|
[2b4a9f26] | 123 | *
|
---|
[53f9821] | 124 | */
|
---|
[7a0359b] | 125 | NO_TRACE static inline void spinlock_unlock_nondebug(spinlock_t *lock)
|
---|
[53f9821] | 126 | {
|
---|
[78de83de] | 127 | atomic_flag_clear_explicit(&lock->flag, memory_order_release);
|
---|
[53f9821] | 128 | preemption_enable();
|
---|
| 129 | }
|
---|
[f761f1eb] | 130 |
|
---|
[31d8e10] | 131 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
| 132 |
|
---|
[b2fa1204] | 133 | #include <log.h>
|
---|
[90c8b8d] | 134 |
|
---|
| 135 | #define DEADLOCK_THRESHOLD 100000000
|
---|
[31d8e10] | 136 |
|
---|
[90c8b8d] | 137 | #define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
|
---|
| 138 |
|
---|
| 139 | #define DEADLOCK_PROBE(pname, value) \
|
---|
| 140 | if ((pname)++ > (value)) { \
|
---|
| 141 | (pname) = 0; \
|
---|
[b2fa1204] | 142 | log(LF_OTHER, LVL_WARN, \
|
---|
| 143 | "Deadlock probe %s: exceeded threshold %u\n" \
|
---|
[90c8b8d] | 144 | "cpu%u: function=%s, line=%u\n", \
|
---|
| 145 | #pname, (value), CPU->id, __func__, __LINE__); \
|
---|
[31d8e10] | 146 | }
|
---|
[90c8b8d] | 147 |
|
---|
[2b4a9f26] | 148 | #else /* CONFIG_DEBUG_SPINLOCK */
|
---|
[90c8b8d] | 149 |
|
---|
[31d8e10] | 150 | #define DEADLOCK_PROBE_INIT(pname)
|
---|
| 151 | #define DEADLOCK_PROBE(pname, value)
|
---|
[90c8b8d] | 152 |
|
---|
[2b4a9f26] | 153 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
---|
[31d8e10] | 154 |
|
---|
[90c8b8d] | 155 | #else /* CONFIG_SMP */
|
---|
[f761f1eb] | 156 |
|
---|
[dc747e3] | 157 | /* On UP systems, spinlocks are effectively left out. */
|
---|
[90c8b8d] | 158 |
|
---|
[cc106e4] | 159 | /* Allow the use of spinlock_t as an incomplete type. */
|
---|
| 160 | typedef struct spinlock spinlock_t;
|
---|
| 161 |
|
---|
[dc747e3] | 162 | #define SPINLOCK_DECLARE(name)
|
---|
[8be8cfa] | 163 | #define SPINLOCK_EXTERN(name)
|
---|
[90c8b8d] | 164 |
|
---|
[dc747e3] | 165 | #define SPINLOCK_INITIALIZE(name)
|
---|
[90c8b8d] | 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 |
|
---|
[63e27ef] | 171 | #define ASSERT_SPINLOCK(expr, lock) assert(expr)
|
---|
[2b4a9f26] | 172 |
|
---|
[90c8b8d] | 173 | #define spinlock_initialize(lock, name)
|
---|
[f761f1eb] | 174 |
|
---|
[90c8b8d] | 175 | #define spinlock_lock(lock) preemption_disable()
|
---|
[cc106e4] | 176 | #define spinlock_trylock(lock) ({ preemption_disable(); 1; })
|
---|
[90c8b8d] | 177 | #define spinlock_unlock(lock) preemption_enable()
|
---|
[ffe4a87] | 178 | #define spinlock_locked(lock) 1
|
---|
| 179 | #define spinlock_unlocked(lock) 1
|
---|
[f761f1eb] | 180 |
|
---|
[31d8e10] | 181 | #define DEADLOCK_PROBE_INIT(pname)
|
---|
| 182 | #define DEADLOCK_PROBE(pname, value)
|
---|
| 183 |
|
---|
[2b4a9f26] | 184 | #endif /* CONFIG_SMP */
|
---|
| 185 |
|
---|
| 186 | typedef struct {
|
---|
[8aa9265] | 187 | SPINLOCK_DECLARE(lock); /**< Spinlock */
|
---|
| 188 | bool guard; /**< Flag whether ipl is valid */
|
---|
| 189 | ipl_t ipl; /**< Original interrupt level */
|
---|
[2b4a9f26] | 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 |
|
---|
[8aa9265] | 195 | #ifdef CONFIG_SMP
|
---|
| 196 |
|
---|
[2b4a9f26] | 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, \
|
---|
[78de83de] | 211 | .flag = ATOMIC_FLAG_INIT \
|
---|
[2b4a9f26] | 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, \
|
---|
[78de83de] | 221 | .flag = ATOMIC_FLAG_INIT \
|
---|
[2b4a9f26] | 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 = { \
|
---|
[78de83de] | 232 | .flag = ATOMIC_FLAG_INIT \
|
---|
[2b4a9f26] | 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 = { \
|
---|
[78de83de] | 241 | .flag = ATOMIC_FLAG_INIT \
|
---|
[2b4a9f26] | 242 | }, \
|
---|
| 243 | .guard = false, \
|
---|
| 244 | .ipl = 0 \
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
---|
| 248 |
|
---|
[8aa9265] | 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 |
|
---|
[2b4a9f26] | 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 |
|
---|
[a9f1372] | 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);
|
---|
[89ea2dc] | 283 | extern bool irq_spinlock_trylock(irq_spinlock_t *);
|
---|
[a9f1372] | 284 | extern void irq_spinlock_pass(irq_spinlock_t *, irq_spinlock_t *);
|
---|
| 285 | extern void irq_spinlock_exchange(irq_spinlock_t *, irq_spinlock_t *);
|
---|
[ffe4a87] | 286 | extern bool irq_spinlock_locked(irq_spinlock_t *);
|
---|
[f761f1eb] | 287 |
|
---|
| 288 | #endif
|
---|
[b45c443] | 289 |
|
---|
[06e1e95] | 290 | /** @}
|
---|
[b45c443] | 291 | */
|
---|