source: mainline/kernel/generic/include/synch/spinlock.h@ 42bbbe2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 42bbbe2 was 42bbbe2, checked in by Jakub Jermar <jakub@…>, 15 years ago

Include arch/asm.h in spinlock.h

  • Property mode set to 100644
File size: 10.4 KB
Line 
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 <typedefs.h>
39#include <arch/barrier.h>
40#include <preemption.h>
41#include <atomic.h>
42#include <debug.h>
43#include <arch/asm.h>
44
45#ifdef CONFIG_SMP
46
47typedef struct {
48 atomic_t val;
49
50#ifdef CONFIG_DEBUG_SPINLOCK
51 const char *name;
52#endif /* CONFIG_DEBUG_SPINLOCK */
53} spinlock_t;
54
55/*
56 * SPINLOCK_DECLARE is to be used for dynamically allocated spinlocks,
57 * where the lock gets initialized in run time.
58 */
59#define SPINLOCK_DECLARE(lock_name) spinlock_t lock_name
60#define SPINLOCK_EXTERN(lock_name) extern spinlock_t lock_name
61
62/*
63 * SPINLOCK_INITIALIZE and SPINLOCK_STATIC_INITIALIZE are to be used
64 * for statically allocated spinlocks. They declare (either as global
65 * or static) symbol and initialize the lock.
66 */
67#ifdef CONFIG_DEBUG_SPINLOCK
68
69#define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
70 spinlock_t lock_name = { \
71 .name = desc_name, \
72 .val = { 0 } \
73 }
74
75#define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
76 static spinlock_t lock_name = { \
77 .name = desc_name, \
78 .val = { 0 } \
79 }
80
81#define ASSERT_SPINLOCK(expr, lock) \
82 ASSERT_VERBOSE(expr, (lock)->name)
83
84#define spinlock_lock(lock) spinlock_lock_debug((lock))
85#define spinlock_unlock(lock) spinlock_unlock_debug((lock))
86
87#else /* CONFIG_DEBUG_SPINLOCK */
88
89#define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
90 spinlock_t lock_name = { \
91 .val = { 0 } \
92 }
93
94#define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
95 static spinlock_t lock_name = { \
96 .val = { 0 } \
97 }
98
99#define ASSERT_SPINLOCK(expr, lock) \
100 ASSERT(expr)
101
102#define spinlock_lock(lock) atomic_lock_arch(&(lock)->val)
103#define spinlock_unlock(lock) spinlock_unlock_nondebug((lock))
104
105#endif /* CONFIG_DEBUG_SPINLOCK */
106
107#define SPINLOCK_INITIALIZE(lock_name) \
108 SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
109
110#define SPINLOCK_STATIC_INITIALIZE(lock_name) \
111 SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
112
113extern void spinlock_initialize(spinlock_t *, const char *);
114extern int spinlock_trylock(spinlock_t *);
115extern void spinlock_lock_debug(spinlock_t *);
116extern void spinlock_unlock_debug(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 */
125static inline void spinlock_unlock_nondebug(spinlock_t *lock)
126{
127 /*
128 * Prevent critical section code from bleeding out this way down.
129 */
130 CS_LEAVE_BARRIER();
131
132 atomic_set(&lock->val, 0);
133 preemption_enable();
134}
135
136#ifdef CONFIG_DEBUG_SPINLOCK
137
138#include <print.h>
139
140#define DEADLOCK_THRESHOLD 100000000
141
142#define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
143
144#define DEADLOCK_PROBE(pname, value) \
145 if ((pname)++ > (value)) { \
146 (pname) = 0; \
147 printf("Deadlock probe %s: exceeded threshold %u\n", \
148 "cpu%u: function=%s, line=%u\n", \
149 #pname, (value), CPU->id, __func__, __LINE__); \
150 }
151
152#else /* CONFIG_DEBUG_SPINLOCK */
153
154#define DEADLOCK_PROBE_INIT(pname)
155#define DEADLOCK_PROBE(pname, value)
156
157#endif /* CONFIG_DEBUG_SPINLOCK */
158
159#else /* CONFIG_SMP */
160
161/* On UP systems, spinlocks are effectively left out. */
162
163#define SPINLOCK_DECLARE(name)
164#define SPINLOCK_EXTERN(name)
165
166#define SPINLOCK_INITIALIZE(name)
167#define SPINLOCK_STATIC_INITIALIZE(name)
168
169#define SPINLOCK_INITIALIZE_NAME(name, desc_name)
170#define SPINLOCK_STATIC_INITIALIZE_NAME(name, desc_name)
171
172#define ASSERT_SPINLOCK(expr, lock)
173
174#define spinlock_initialize(lock, name)
175
176#define spinlock_lock(lock) preemption_disable()
177#define spinlock_trylock(lock) (preemption_disable(), 1)
178#define spinlock_unlock(lock) preemption_enable()
179
180#define DEADLOCK_PROBE_INIT(pname)
181#define DEADLOCK_PROBE(pname, value)
182
183#endif /* CONFIG_SMP */
184
185typedef struct {
186 spinlock_t lock; /**< Spinlock */
187 bool guard; /**< Flag whether ipl is valid */
188 ipl_t ipl; /**< Original interrupt level */
189} irq_spinlock_t;
190
191#define IRQ_SPINLOCK_DECLARE(lock_name) irq_spinlock_t lock_name
192#define IRQ_SPINLOCK_EXTERN(lock_name) extern irq_spinlock_t lock_name
193
194#define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
195 ASSERT_SPINLOCK(expr, &((irq_lock)->lock))
196
197/*
198 * IRQ_SPINLOCK_INITIALIZE and IRQ_SPINLOCK_STATIC_INITIALIZE are to be used
199 * for statically allocated interrupts-disabled spinlocks. They declare (either
200 * as global or static symbol) and initialize the lock.
201 */
202#ifdef CONFIG_DEBUG_SPINLOCK
203
204#define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
205 irq_spinlock_t lock_name = { \
206 .lock = { \
207 .name = desc_name, \
208 .val = { 0 } \
209 }, \
210 .guard = false, \
211 .ipl = 0 \
212 }
213
214#define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
215 static irq_spinlock_t lock_name = { \
216 .lock = { \
217 .name = desc_name, \
218 .val = { 0 } \
219 }, \
220 .guard = false, \
221 .ipl = 0 \
222 }
223
224#else /* CONFIG_DEBUG_SPINLOCK */
225
226#define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
227 irq_spinlock_t lock_name = { \
228 .lock = { \
229 .val = { 0 } \
230 }, \
231 .guard = false, \
232 .ipl = 0 \
233 }
234
235#define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
236 static irq_spinlock_t lock_name = { \
237 .lock = { \
238 .val = { 0 } \
239 }, \
240 .guard = false, \
241 .ipl = 0 \
242 }
243
244#endif /* CONFIG_DEBUG_SPINLOCK */
245
246#define IRQ_SPINLOCK_INITIALIZE(lock_name) \
247 IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
248
249#define IRQ_SPINLOCK_STATIC_INITIALIZE(lock_name) \
250 IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
251
252/** Initialize interrupts-disabled spinlock
253 *
254 * @param lock IRQ spinlock to be initialized.
255 * @param name IRQ spinlock name.
256 *
257 */
258static inline void irq_spinlock_initialize(irq_spinlock_t *lock, const char *name)
259{
260 spinlock_initialize(&(lock->lock), name);
261 lock->guard = false;
262 lock->ipl = 0;
263}
264
265/** Lock interrupts-disabled spinlock
266 *
267 * Lock a spinlock which requires disabled interrupts.
268 *
269 * @param lock IRQ spinlock to be locked.
270 * @param irq_dis If true, interrupts are actually disabled
271 * prior locking the spinlock. If false, interrupts
272 * are expected to be already disabled.
273 *
274 */
275static inline void irq_spinlock_lock(irq_spinlock_t *lock, bool irq_dis)
276{
277 if (irq_dis) {
278 ipl_t ipl = interrupts_disable();
279 spinlock_lock(&(lock->lock));
280
281 lock->guard = true;
282 lock->ipl = ipl;
283 } else {
284 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
285
286 spinlock_lock(&(lock->lock));
287 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
288 }
289}
290
291/** Unlock interrupts-disabled spinlock
292 *
293 * Unlock a spinlock which requires disabled interrupts.
294 *
295 * @param lock IRQ spinlock to be unlocked.
296 * @param irq_res If true, interrupts are restored to previously
297 * saved interrupt level.
298 *
299 */
300static inline void irq_spinlock_unlock(irq_spinlock_t *lock, bool irq_res)
301{
302 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
303
304 if (irq_res) {
305 ASSERT_IRQ_SPINLOCK(lock->guard, lock);
306
307 lock->guard = false;
308 ipl_t ipl = lock->ipl;
309
310 spinlock_unlock(&(lock->lock));
311 interrupts_restore(ipl);
312 } else {
313 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
314 spinlock_unlock(&(lock->lock));
315 }
316}
317
318/** Lock interrupts-disabled spinlock
319 *
320 * Lock an interrupts-disabled spinlock conditionally. If the
321 * spinlock is not available at the moment, signal failure.
322 * Interrupts are expected to be already disabled.
323 *
324 * @param lock IRQ spinlock to be locked conditionally.
325 *
326 * @return Zero on failure, non-zero otherwise.
327 *
328 */
329static inline int irq_spinlock_trylock(irq_spinlock_t *lock)
330{
331 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
332 int rc = spinlock_trylock(&(lock->lock));
333
334 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
335 return rc;
336}
337
338/** Pass lock from one interrupts-disabled spinlock to another
339 *
340 * Pass lock from one IRQ spinlock to another IRQ spinlock
341 * without enabling interrupts during the process.
342 *
343 * The first IRQ spinlock is supposed to be locked.
344 *
345 * @param unlock IRQ spinlock to be unlocked.
346 * @param lock IRQ spinlock to be locked.
347 *
348 */
349static inline void irq_spinlock_pass(irq_spinlock_t *unlock,
350 irq_spinlock_t *lock)
351{
352 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), unlock);
353
354 /* Pass guard from unlock to lock */
355 bool guard = unlock->guard;
356 ipl_t ipl = unlock->ipl;
357 unlock->guard = false;
358
359 spinlock_unlock(&(unlock->lock));
360 spinlock_lock(&(lock->lock));
361
362 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
363
364 if (guard) {
365 lock->guard = true;
366 lock->ipl = ipl;
367 }
368}
369
370/** Hand-over-hand locking of interrupts-disabled spinlocks
371 *
372 * Implement hand-over-hand locking between two interrupts-disabled
373 * spinlocks without enabling interrupts during the process.
374 *
375 * The first IRQ spinlock is supposed to be locked.
376 *
377 * @param unlock IRQ spinlock to be unlocked.
378 * @param lock IRQ spinlock to be locked.
379 *
380 */
381static inline void irq_spinlock_exchange(irq_spinlock_t *unlock,
382 irq_spinlock_t *lock)
383{
384 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), unlock);
385
386 spinlock_lock(&(lock->lock));
387 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
388
389 /* Pass guard from unlock to lock */
390 if (unlock->guard) {
391 lock->guard = true;
392 lock->ipl = unlock->ipl;
393 unlock->guard = false;
394 }
395
396 spinlock_unlock(&(unlock->lock));
397}
398
399#endif
400
401/** @}
402 */
Note: See TracBrowser for help on using the repository browser.