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

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

Add interfaces for testing the status of plain spinlocks and the IRQ spinlocks.

Note that because of the non-SMP version of spinlocks, the status must be
checked only in the affirmative manner. Instead of:

ASSERT(!spinlock_locked(…));


one needs to do:

ASSERT(spinlock_unlocked(…));

Otherwise the assertion will be hit on debug non-SMP kernels.

  • Property mode set to 100644
File size: 8.0 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 *);
117extern bool spinlock_locked(spinlock_t *);
118extern bool spinlock_unlocked(spinlock_t *);
119
120/** Unlock spinlock
121 *
122 * Unlock spinlock for non-debug kernels.
123 *
124 * @param sl Pointer to spinlock_t structure.
125 *
126 */
127static inline void spinlock_unlock_nondebug(spinlock_t *lock)
128{
129 /*
130 * Prevent critical section code from bleeding out this way down.
131 */
132 CS_LEAVE_BARRIER();
133
134 atomic_set(&lock->val, 0);
135 preemption_enable();
136}
137
138#ifdef CONFIG_DEBUG_SPINLOCK
139
140#include <print.h>
141
142#define DEADLOCK_THRESHOLD 100000000
143
144#define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
145
146#define DEADLOCK_PROBE(pname, value) \
147 if ((pname)++ > (value)) { \
148 (pname) = 0; \
149 printf("Deadlock probe %s: exceeded threshold %u\n", \
150 "cpu%u: function=%s, line=%u\n", \
151 #pname, (value), CPU->id, __func__, __LINE__); \
152 }
153
154#else /* CONFIG_DEBUG_SPINLOCK */
155
156#define DEADLOCK_PROBE_INIT(pname)
157#define DEADLOCK_PROBE(pname, value)
158
159#endif /* CONFIG_DEBUG_SPINLOCK */
160
161#else /* CONFIG_SMP */
162
163/* On UP systems, spinlocks are effectively left out. */
164
165#define SPINLOCK_DECLARE(name)
166#define SPINLOCK_EXTERN(name)
167
168#define SPINLOCK_INITIALIZE(name)
169#define SPINLOCK_STATIC_INITIALIZE(name)
170
171#define SPINLOCK_INITIALIZE_NAME(name, desc_name)
172#define SPINLOCK_STATIC_INITIALIZE_NAME(name, desc_name)
173
174#define ASSERT_SPINLOCK(expr, lock) ASSERT(expr)
175
176#define spinlock_initialize(lock, name)
177
178#define spinlock_lock(lock) preemption_disable()
179#define spinlock_trylock(lock) (preemption_disable(), 1)
180#define spinlock_unlock(lock) preemption_enable()
181#define spinlock_locked(lock) 1
182#define spinlock_unlocked(lock) 1
183
184#define DEADLOCK_PROBE_INIT(pname)
185#define DEADLOCK_PROBE(pname, value)
186
187#endif /* CONFIG_SMP */
188
189typedef struct {
190 SPINLOCK_DECLARE(lock); /**< Spinlock */
191 bool guard; /**< Flag whether ipl is valid */
192 ipl_t ipl; /**< Original interrupt level */
193} irq_spinlock_t;
194
195#define IRQ_SPINLOCK_DECLARE(lock_name) irq_spinlock_t lock_name
196#define IRQ_SPINLOCK_EXTERN(lock_name) extern irq_spinlock_t lock_name
197
198#ifdef CONFIG_SMP
199
200#define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
201 ASSERT_SPINLOCK(expr, &((irq_lock)->lock))
202
203/*
204 * IRQ_SPINLOCK_INITIALIZE and IRQ_SPINLOCK_STATIC_INITIALIZE are to be used
205 * for statically allocated interrupts-disabled spinlocks. They declare (either
206 * as global or static symbol) and initialize the lock.
207 */
208#ifdef CONFIG_DEBUG_SPINLOCK
209
210#define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
211 irq_spinlock_t lock_name = { \
212 .lock = { \
213 .name = desc_name, \
214 .val = { 0 } \
215 }, \
216 .guard = false, \
217 .ipl = 0 \
218 }
219
220#define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
221 static irq_spinlock_t lock_name = { \
222 .lock = { \
223 .name = desc_name, \
224 .val = { 0 } \
225 }, \
226 .guard = false, \
227 .ipl = 0 \
228 }
229
230#else /* CONFIG_DEBUG_SPINLOCK */
231
232#define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
233 irq_spinlock_t lock_name = { \
234 .lock = { \
235 .val = { 0 } \
236 }, \
237 .guard = false, \
238 .ipl = 0 \
239 }
240
241#define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
242 static irq_spinlock_t lock_name = { \
243 .lock = { \
244 .val = { 0 } \
245 }, \
246 .guard = false, \
247 .ipl = 0 \
248 }
249
250#endif /* CONFIG_DEBUG_SPINLOCK */
251
252#else /* CONFIG_SMP */
253
254/*
255 * Since the spinlocks are void on UP systems, we also need
256 * to have a special variant of interrupts-disabled spinlock
257 * macros which take this into account.
258 */
259
260#define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
261 ASSERT_SPINLOCK(expr, NULL)
262
263#define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
264 irq_spinlock_t lock_name = { \
265 .guard = false, \
266 .ipl = 0 \
267 }
268
269#define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
270 static irq_spinlock_t lock_name = { \
271 .guard = false, \
272 .ipl = 0 \
273 }
274
275#endif /* CONFIG_SMP */
276
277#define IRQ_SPINLOCK_INITIALIZE(lock_name) \
278 IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
279
280#define IRQ_SPINLOCK_STATIC_INITIALIZE(lock_name) \
281 IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
282
283extern void irq_spinlock_initialize(irq_spinlock_t *, const char *);
284extern void irq_spinlock_lock(irq_spinlock_t *, bool);
285extern void irq_spinlock_unlock(irq_spinlock_t *, bool);
286extern int irq_spinlock_trylock(irq_spinlock_t *);
287extern void irq_spinlock_pass(irq_spinlock_t *, irq_spinlock_t *);
288extern void irq_spinlock_exchange(irq_spinlock_t *, irq_spinlock_t *);
289extern bool irq_spinlock_locked(irq_spinlock_t *);
290extern bool irq_spinlock_unlocked(irq_spinlock_t *);
291
292#endif
293
294/** @}
295 */
Note: See TracBrowser for help on using the repository browser.