[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 |
|
---|
| 38 | #include <arch/types.h>
|
---|
[59e4864] | 39 | #include <arch/barrier.h>
|
---|
[c842f04] | 40 | #include <preemption.h>
|
---|
[23684b7] | 41 | #include <atomic.h>
|
---|
[53f9821] | 42 | #include <debug.h>
|
---|
[f761f1eb] | 43 |
|
---|
[5f85c91] | 44 | #ifdef CONFIG_SMP
|
---|
[90c8b8d] | 45 |
|
---|
[e71a61d] | 46 | typedef struct {
|
---|
[90c8b8d] | 47 | atomic_t val;
|
---|
| 48 |
|
---|
[2d93f1f9] | 49 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
| 50 | char *name;
|
---|
| 51 | #endif
|
---|
[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 | /*
|
---|
| 62 | * SPINLOCK_INITIALIZE is to be used for statically allocated spinlocks.
|
---|
| 63 | * It declares and initializes the lock.
|
---|
| 64 | */
|
---|
| 65 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
[90c8b8d] | 66 |
|
---|
| 67 | #define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
| 68 | spinlock_t lock_name = { \
|
---|
| 69 | .name = desc_name, \
|
---|
| 70 | .val = { 0 } \
|
---|
[dc747e3] | 71 | }
|
---|
[90c8b8d] | 72 |
|
---|
| 73 | #define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
| 74 | static spinlock_t lock_name = { \
|
---|
| 75 | .name = desc_name, \
|
---|
| 76 | .val = { 0 } \
|
---|
[dc747e3] | 77 | }
|
---|
| 78 |
|
---|
[90c8b8d] | 79 | #define spinlock_lock(lock) spinlock_lock_debug(lock)
|
---|
[53f9821] | 80 |
|
---|
| 81 | #else
|
---|
[90c8b8d] | 82 |
|
---|
| 83 | #define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
| 84 | spinlock_t lock_name = { \
|
---|
| 85 | .val = { 0 } \
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | #define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
| 89 | static spinlock_t lock_name = { \
|
---|
| 90 | .val = { 0 } \
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | #define spinlock_lock(lock) atomic_lock_arch(&(lock)->val)
|
---|
| 94 |
|
---|
[53f9821] | 95 | #endif
|
---|
| 96 |
|
---|
[90c8b8d] | 97 | #define SPINLOCK_INITIALIZE(lock_name) \
|
---|
| 98 | SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
| 99 |
|
---|
| 100 | #define SPINLOCK_STATIC_INITIALIZE(lock_name) \
|
---|
| 101 | SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
| 102 |
|
---|
| 103 | extern void spinlock_initialize(spinlock_t *lock, char *name);
|
---|
| 104 | extern int spinlock_trylock(spinlock_t *lock);
|
---|
| 105 | extern void spinlock_lock_debug(spinlock_t *lock);
|
---|
| 106 |
|
---|
[53f9821] | 107 | /** Unlock spinlock
|
---|
| 108 | *
|
---|
| 109 | * Unlock spinlock.
|
---|
| 110 | *
|
---|
| 111 | * @param sl Pointer to spinlock_t structure.
|
---|
| 112 | */
|
---|
[90c8b8d] | 113 | static inline void spinlock_unlock(spinlock_t *lock)
|
---|
[53f9821] | 114 | {
|
---|
[90c8b8d] | 115 | ASSERT(atomic_get(&lock->val) != 0);
|
---|
| 116 |
|
---|
[53f9821] | 117 | /*
|
---|
| 118 | * Prevent critical section code from bleeding out this way down.
|
---|
| 119 | */
|
---|
| 120 | CS_LEAVE_BARRIER();
|
---|
| 121 |
|
---|
[90c8b8d] | 122 | atomic_set(&lock->val, 0);
|
---|
[53f9821] | 123 | preemption_enable();
|
---|
| 124 | }
|
---|
[f761f1eb] | 125 |
|
---|
[31d8e10] | 126 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
| 127 |
|
---|
[90c8b8d] | 128 | #include <print.h>
|
---|
| 129 |
|
---|
| 130 | #define DEADLOCK_THRESHOLD 100000000
|
---|
[31d8e10] | 131 |
|
---|
[90c8b8d] | 132 | #define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
|
---|
| 133 |
|
---|
| 134 | #define DEADLOCK_PROBE(pname, value) \
|
---|
| 135 | if ((pname)++ > (value)) { \
|
---|
| 136 | (pname) = 0; \
|
---|
| 137 | printf("Deadlock probe %s: exceeded threshold %u\n", \
|
---|
| 138 | "cpu%u: function=%s, line=%u\n", \
|
---|
| 139 | #pname, (value), CPU->id, __func__, __LINE__); \
|
---|
[31d8e10] | 140 | }
|
---|
[90c8b8d] | 141 |
|
---|
[31d8e10] | 142 | #else
|
---|
[90c8b8d] | 143 |
|
---|
[31d8e10] | 144 | #define DEADLOCK_PROBE_INIT(pname)
|
---|
| 145 | #define DEADLOCK_PROBE(pname, value)
|
---|
[90c8b8d] | 146 |
|
---|
[31d8e10] | 147 | #endif
|
---|
| 148 |
|
---|
[90c8b8d] | 149 | #else /* CONFIG_SMP */
|
---|
[f761f1eb] | 150 |
|
---|
[dc747e3] | 151 | /* On UP systems, spinlocks are effectively left out. */
|
---|
[90c8b8d] | 152 |
|
---|
[dc747e3] | 153 | #define SPINLOCK_DECLARE(name)
|
---|
[8be8cfa] | 154 | #define SPINLOCK_EXTERN(name)
|
---|
[90c8b8d] | 155 |
|
---|
[dc747e3] | 156 | #define SPINLOCK_INITIALIZE(name)
|
---|
[90c8b8d] | 157 | #define SPINLOCK_STATIC_INITIALIZE(name)
|
---|
| 158 |
|
---|
| 159 | #define SPINLOCK_INITIALIZE_NAME(name, desc_name)
|
---|
| 160 | #define SPINLOCK_STATIC_INITIALIZE_NAME(name, desc_name)
|
---|
| 161 |
|
---|
| 162 | #define spinlock_initialize(lock, name)
|
---|
[f761f1eb] | 163 |
|
---|
[90c8b8d] | 164 | #define spinlock_lock(lock) preemption_disable()
|
---|
| 165 | #define spinlock_trylock(lock) (preemption_disable(), 1)
|
---|
| 166 | #define spinlock_unlock(lock) preemption_enable()
|
---|
[f761f1eb] | 167 |
|
---|
[31d8e10] | 168 | #define DEADLOCK_PROBE_INIT(pname)
|
---|
| 169 | #define DEADLOCK_PROBE(pname, value)
|
---|
| 170 |
|
---|
[f761f1eb] | 171 | #endif
|
---|
| 172 |
|
---|
| 173 | #endif
|
---|
[b45c443] | 174 |
|
---|
[06e1e95] | 175 | /** @}
|
---|
[b45c443] | 176 | */
|
---|