1 | /*
|
---|
2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
3 | * Copyright (c) 2023 Jiří Zárevúcky
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup kernel_sync
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef KERN_SPINLOCK_H_
|
---|
37 | #define KERN_SPINLOCK_H_
|
---|
38 |
|
---|
39 | #include <stdatomic.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 |
|
---|
42 | #include <arch/types.h>
|
---|
43 | #include <assert.h>
|
---|
44 |
|
---|
45 | #define DEADLOCK_THRESHOLD 100000000
|
---|
46 |
|
---|
47 | #if defined(CONFIG_SMP) && defined(CONFIG_DEBUG_SPINLOCK)
|
---|
48 |
|
---|
49 | #include <log.h>
|
---|
50 |
|
---|
51 | #define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
|
---|
52 |
|
---|
53 | #define DEADLOCK_PROBE(pname, value) \
|
---|
54 | if ((pname)++ > (value)) { \
|
---|
55 | (pname) = 0; \
|
---|
56 | log(LF_OTHER, LVL_WARN, \
|
---|
57 | "Deadlock probe %s: exceeded threshold %u\n" \
|
---|
58 | "cpu%u: function=%s, line=%u\n", \
|
---|
59 | #pname, (value), CPU->id, __func__, __LINE__); \
|
---|
60 | }
|
---|
61 |
|
---|
62 | #else /* CONFIG_DEBUG_SPINLOCK */
|
---|
63 |
|
---|
64 | #define DEADLOCK_PROBE_INIT(pname)
|
---|
65 | #define DEADLOCK_PROBE(pname, value)
|
---|
66 |
|
---|
67 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
---|
68 |
|
---|
69 | typedef struct spinlock {
|
---|
70 | #ifdef CONFIG_SMP
|
---|
71 | atomic_flag flag;
|
---|
72 |
|
---|
73 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
74 | const char *name;
|
---|
75 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
---|
76 | #endif
|
---|
77 | } spinlock_t;
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * SPINLOCK_DECLARE is to be used for dynamically allocated spinlocks,
|
---|
81 | * where the lock gets initialized in run time.
|
---|
82 | */
|
---|
83 | #define SPINLOCK_DECLARE(lock_name) spinlock_t lock_name
|
---|
84 | #define SPINLOCK_EXTERN(lock_name) extern spinlock_t lock_name
|
---|
85 |
|
---|
86 | #ifdef CONFIG_SMP
|
---|
87 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
88 | #define SPINLOCK_INITIALIZER(desc_name) { .name = (desc_name), .flag = ATOMIC_FLAG_INIT }
|
---|
89 | #else
|
---|
90 | #define SPINLOCK_INITIALIZER(desc_name) { .flag = ATOMIC_FLAG_INIT }
|
---|
91 | #endif
|
---|
92 | #else
|
---|
93 | #define SPINLOCK_INITIALIZER(desc_name) {}
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * SPINLOCK_INITIALIZE and SPINLOCK_STATIC_INITIALIZE are to be used
|
---|
98 | * for statically allocated spinlocks. They declare (either as global
|
---|
99 | * or static) symbol and initialize the lock.
|
---|
100 | */
|
---|
101 | #define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
102 | spinlock_t lock_name = SPINLOCK_INITIALIZER(desc_name)
|
---|
103 |
|
---|
104 | #define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
105 | static spinlock_t lock_name = SPINLOCK_INITIALIZER(desc_name)
|
---|
106 |
|
---|
107 | #if defined(CONFIG_SMP) && defined(CONFIG_DEBUG_SPINLOCK)
|
---|
108 | #define ASSERT_SPINLOCK(expr, lock) assert_verbose(expr, (lock)->name)
|
---|
109 | #else /* CONFIG_DEBUG_SPINLOCK */
|
---|
110 | #define ASSERT_SPINLOCK(expr, lock) assert(expr)
|
---|
111 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
---|
112 |
|
---|
113 | #define SPINLOCK_INITIALIZE(lock_name) \
|
---|
114 | SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
115 |
|
---|
116 | #define SPINLOCK_STATIC_INITIALIZE(lock_name) \
|
---|
117 | SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
118 |
|
---|
119 | #ifdef CONFIG_SMP
|
---|
120 |
|
---|
121 | extern void spinlock_initialize(spinlock_t *, const char *);
|
---|
122 | extern bool spinlock_trylock(spinlock_t *);
|
---|
123 | extern void spinlock_lock(spinlock_t *);
|
---|
124 | extern void spinlock_unlock(spinlock_t *);
|
---|
125 | extern bool spinlock_locked(spinlock_t *);
|
---|
126 |
|
---|
127 | #else
|
---|
128 |
|
---|
129 | #include <preemption.h>
|
---|
130 |
|
---|
131 | static inline void spinlock_initialize(spinlock_t *l, const char *name)
|
---|
132 | {
|
---|
133 | }
|
---|
134 |
|
---|
135 | static inline bool spinlock_trylock(spinlock_t *l)
|
---|
136 | {
|
---|
137 | preemption_disable();
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 |
|
---|
141 | static inline void spinlock_lock(spinlock_t *l)
|
---|
142 | {
|
---|
143 | preemption_disable();
|
---|
144 | }
|
---|
145 |
|
---|
146 | static inline void spinlock_unlock(spinlock_t *l)
|
---|
147 | {
|
---|
148 | preemption_enable();
|
---|
149 | }
|
---|
150 |
|
---|
151 | static inline bool spinlock_locked(spinlock_t *l)
|
---|
152 | {
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 |
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | typedef struct {
|
---|
159 | spinlock_t lock; /**< Spinlock */
|
---|
160 | bool guard; /**< Flag whether ipl is valid */
|
---|
161 | ipl_t ipl; /**< Original interrupt level */
|
---|
162 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
163 | _Atomic(struct cpu *) owner; /**< Which cpu currently owns this lock */
|
---|
164 | #endif
|
---|
165 | } irq_spinlock_t;
|
---|
166 |
|
---|
167 | #define IRQ_SPINLOCK_DECLARE(lock_name) irq_spinlock_t lock_name
|
---|
168 | #define IRQ_SPINLOCK_EXTERN(lock_name) extern irq_spinlock_t lock_name
|
---|
169 |
|
---|
170 | #define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
|
---|
171 | ASSERT_SPINLOCK(expr, &((irq_lock)->lock))
|
---|
172 |
|
---|
173 | #define IRQ_SPINLOCK_INITIALIZER(desc_name) \
|
---|
174 | { \
|
---|
175 | .lock = SPINLOCK_INITIALIZER(desc_name), \
|
---|
176 | .guard = false, \
|
---|
177 | .ipl = 0, \
|
---|
178 | }
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * IRQ_SPINLOCK_INITIALIZE and IRQ_SPINLOCK_STATIC_INITIALIZE are to be used
|
---|
182 | * for statically allocated interrupts-disabled spinlocks. They declare (either
|
---|
183 | * as global or static symbol) and initialize the lock.
|
---|
184 | */
|
---|
185 | #define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
186 | irq_spinlock_t lock_name = IRQ_SPINLOCK_INITIALIZER(desc_name)
|
---|
187 |
|
---|
188 | #define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
---|
189 | static irq_spinlock_t lock_name = IRQ_SPINLOCK_INITIALIZER(desc_name)
|
---|
190 |
|
---|
191 | #define IRQ_SPINLOCK_INITIALIZE(lock_name) \
|
---|
192 | IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
193 |
|
---|
194 | #define IRQ_SPINLOCK_STATIC_INITIALIZE(lock_name) \
|
---|
195 | IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
|
---|
196 |
|
---|
197 | extern void irq_spinlock_initialize(irq_spinlock_t *, const char *);
|
---|
198 | extern void irq_spinlock_lock(irq_spinlock_t *, bool);
|
---|
199 | extern void irq_spinlock_unlock(irq_spinlock_t *, bool);
|
---|
200 | extern bool irq_spinlock_trylock(irq_spinlock_t *);
|
---|
201 | extern void irq_spinlock_pass(irq_spinlock_t *, irq_spinlock_t *);
|
---|
202 | extern void irq_spinlock_exchange(irq_spinlock_t *, irq_spinlock_t *);
|
---|
203 | extern bool irq_spinlock_locked(irq_spinlock_t *);
|
---|
204 |
|
---|
205 | #endif
|
---|
206 |
|
---|
207 | /** @}
|
---|
208 | */
|
---|