]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/dde/linux26/include/linux/spinlock.h
Inital import
[l4.git] / l4 / pkg / dde / linux26 / include / linux / spinlock.h
1 #ifndef __LINUX_SPINLOCK_H
2 #define __LINUX_SPINLOCK_H
3
4 /*
5  * include/linux/spinlock.h - generic spinlock/rwlock declarations
6  *
7  * here's the role of the various spinlock/rwlock related include files:
8  *
9  * on SMP builds:
10  *
11  *  asm/spinlock_types.h: contains the raw_spinlock_t/raw_rwlock_t and the
12  *                        initializers
13  *
14  *  linux/spinlock_types.h:
15  *                        defines the generic type and initializers
16  *
17  *  asm/spinlock.h:       contains the __raw_spin_*()/etc. lowlevel
18  *                        implementations, mostly inline assembly code
19  *
20  *   (also included on UP-debug builds:)
21  *
22  *  linux/spinlock_api_smp.h:
23  *                        contains the prototypes for the _spin_*() APIs.
24  *
25  *  linux/spinlock.h:     builds the final spin_*() APIs.
26  *
27  * on UP builds:
28  *
29  *  linux/spinlock_type_up.h:
30  *                        contains the generic, simplified UP spinlock type.
31  *                        (which is an empty structure on non-debug builds)
32  *
33  *  linux/spinlock_types.h:
34  *                        defines the generic type and initializers
35  *
36  *  linux/spinlock_up.h:
37  *                        contains the __raw_spin_*()/etc. version of UP
38  *                        builds. (which are NOPs on non-debug, non-preempt
39  *                        builds)
40  *
41  *   (included on UP-non-debug builds:)
42  *
43  *  linux/spinlock_api_up.h:
44  *                        builds the _spin_*() APIs.
45  *
46  *  linux/spinlock.h:     builds the final spin_*() APIs.
47  */
48
49 #include <linux/typecheck.h>
50 #include <linux/preempt.h>
51 #include <linux/linkage.h>
52 #include <linux/compiler.h>
53 #include <linux/thread_info.h>
54 #include <linux/kernel.h>
55 #include <linux/stringify.h>
56 #include <linux/bottom_half.h>
57
58 #include <asm/system.h>
59
60 /*
61  * Must define these before including other files, inline functions need them
62  */
63 #define LOCK_SECTION_NAME ".text.lock."KBUILD_BASENAME
64
65 #define LOCK_SECTION_START(extra)               \
66         ".subsection 1\n\t"                     \
67         extra                                   \
68         ".ifndef " LOCK_SECTION_NAME "\n\t"     \
69         LOCK_SECTION_NAME ":\n\t"               \
70         ".endif\n"
71
72 #define LOCK_SECTION_END                        \
73         ".previous\n\t"
74
75 #define __lockfunc __attribute__((section(".spinlock.text")))
76
77 /*
78  * Pull the raw_spinlock_t and raw_rwlock_t definitions:
79  */
80 #include <linux/spinlock_types.h>
81
82 #ifndef DDE_LINUX
83 extern int __lockfunc generic__raw_read_trylock(raw_rwlock_t *lock);
84
85 /*
86  * Pull the __raw*() functions/declarations (UP-nondebug doesnt need them):
87  */
88 #ifdef CONFIG_SMP
89 # include <asm/spinlock.h>
90 #else
91 # include <linux/spinlock_up.h>
92 #endif
93
94 #ifdef CONFIG_DEBUG_SPINLOCK
95   extern void __spin_lock_init(spinlock_t *lock, const char *name,
96                                struct lock_class_key *key);
97 # define spin_lock_init(lock)                                   \
98 do {                                                            \
99         static struct lock_class_key __key;                     \
100                                                                 \
101         __spin_lock_init((lock), #lock, &__key);                \
102 } while (0)
103
104 #else
105 # define spin_lock_init(lock)                                   \
106         do { *(lock) = SPIN_LOCK_UNLOCKED; } while (0)
107 #endif
108
109 #ifdef CONFIG_DEBUG_SPINLOCK
110   extern void __rwlock_init(rwlock_t *lock, const char *name,
111                             struct lock_class_key *key);
112 # define rwlock_init(lock)                                      \
113 do {                                                            \
114         static struct lock_class_key __key;                     \
115                                                                 \
116         __rwlock_init((lock), #lock, &__key);                   \
117 } while (0)
118 #else
119 # define rwlock_init(lock)                                      \
120         do { *(lock) = RW_LOCK_UNLOCKED; } while (0)
121 #endif
122
123 #define spin_is_locked(lock)    __raw_spin_is_locked(&(lock)->raw_lock)
124
125 #ifdef CONFIG_GENERIC_LOCKBREAK
126 #define spin_is_contended(lock) ((lock)->break_lock)
127 #else
128
129 #ifdef __raw_spin_is_contended
130 #define spin_is_contended(lock) __raw_spin_is_contended(&(lock)->raw_lock)
131 #else
132 #define spin_is_contended(lock) (((void)(lock), 0))
133 #endif /*__raw_spin_is_contended*/
134 #endif
135
136 /**
137  * spin_unlock_wait - wait until the spinlock gets unlocked
138  * @lock: the spinlock in question.
139  */
140 #define spin_unlock_wait(lock)  __raw_spin_unlock_wait(&(lock)->raw_lock)
141
142 /*
143  * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
144  */
145 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
146 # include <linux/spinlock_api_smp.h>
147 #else
148 # include <linux/spinlock_api_up.h>
149 #endif
150
151 #ifdef CONFIG_DEBUG_SPINLOCK
152  extern void _raw_spin_lock(spinlock_t *lock);
153 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
154  extern int _raw_spin_trylock(spinlock_t *lock);
155  extern void _raw_spin_unlock(spinlock_t *lock);
156  extern void _raw_read_lock(rwlock_t *lock);
157  extern int _raw_read_trylock(rwlock_t *lock);
158  extern void _raw_read_unlock(rwlock_t *lock);
159  extern void _raw_write_lock(rwlock_t *lock);
160  extern int _raw_write_trylock(rwlock_t *lock);
161  extern void _raw_write_unlock(rwlock_t *lock);
162 #else
163 # define _raw_spin_lock(lock)           __raw_spin_lock(&(lock)->raw_lock)
164 # define _raw_spin_lock_flags(lock, flags) \
165                 __raw_spin_lock_flags(&(lock)->raw_lock, *(flags))
166 # define _raw_spin_trylock(lock)        __raw_spin_trylock(&(lock)->raw_lock)
167 # define _raw_spin_unlock(lock)         __raw_spin_unlock(&(lock)->raw_lock)
168 # define _raw_read_lock(rwlock)         __raw_read_lock(&(rwlock)->raw_lock)
169 # define _raw_read_trylock(rwlock)      __raw_read_trylock(&(rwlock)->raw_lock)
170 # define _raw_read_unlock(rwlock)       __raw_read_unlock(&(rwlock)->raw_lock)
171 # define _raw_write_lock(rwlock)        __raw_write_lock(&(rwlock)->raw_lock)
172 # define _raw_write_trylock(rwlock)     __raw_write_trylock(&(rwlock)->raw_lock)
173 # define _raw_write_unlock(rwlock)      __raw_write_unlock(&(rwlock)->raw_lock)
174 #endif
175
176 #define read_can_lock(rwlock)           __raw_read_can_lock(&(rwlock)->raw_lock)
177 #define write_can_lock(rwlock)          __raw_write_can_lock(&(rwlock)->raw_lock)
178
179 /*
180  * Define the various spin_lock and rw_lock methods.  Note we define these
181  * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various
182  * methods are defined as nops in the case they are not required.
183  */
184 #define spin_trylock(lock)              __cond_lock(lock, _spin_trylock(lock))
185 #define read_trylock(lock)              __cond_lock(lock, _read_trylock(lock))
186 #define write_trylock(lock)             __cond_lock(lock, _write_trylock(lock))
187
188 #define spin_lock(lock)                 _spin_lock(lock)
189
190 #ifdef CONFIG_DEBUG_LOCK_ALLOC
191 # define spin_lock_nested(lock, subclass) _spin_lock_nested(lock, subclass)
192 # define spin_lock_nest_lock(lock, nest_lock)                           \
193          do {                                                           \
194                  typecheck(struct lockdep_map *, &(nest_lock)->dep_map);\
195                  _spin_lock_nest_lock(lock, &(nest_lock)->dep_map);     \
196          } while (0)
197 #else
198 # define spin_lock_nested(lock, subclass) _spin_lock(lock)
199 # define spin_lock_nest_lock(lock, nest_lock) _spin_lock(lock)
200 #endif
201
202 #define write_lock(lock)                _write_lock(lock)
203 #define read_lock(lock)                 _read_lock(lock)
204
205 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
206
207 #define spin_lock_irqsave(lock, flags)                  \
208         do {                                            \
209                 typecheck(unsigned long, flags);        \
210                 flags = _spin_lock_irqsave(lock);       \
211         } while (0)
212 #define read_lock_irqsave(lock, flags)                  \
213         do {                                            \
214                 typecheck(unsigned long, flags);        \
215                 flags = _read_lock_irqsave(lock);       \
216         } while (0)
217 #define write_lock_irqsave(lock, flags)                 \
218         do {                                            \
219                 typecheck(unsigned long, flags);        \
220                 flags = _write_lock_irqsave(lock);      \
221         } while (0)
222
223 #ifdef CONFIG_DEBUG_LOCK_ALLOC
224 #define spin_lock_irqsave_nested(lock, flags, subclass)                 \
225         do {                                                            \
226                 typecheck(unsigned long, flags);                        \
227                 flags = _spin_lock_irqsave_nested(lock, subclass);      \
228         } while (0)
229 #else
230 #define spin_lock_irqsave_nested(lock, flags, subclass)                 \
231         do {                                                            \
232                 typecheck(unsigned long, flags);                        \
233                 flags = _spin_lock_irqsave(lock);                       \
234         } while (0)
235 #endif
236
237 #else
238
239 #define spin_lock_irqsave(lock, flags)                  \
240         do {                                            \
241                 typecheck(unsigned long, flags);        \
242                 _spin_lock_irqsave(lock, flags);        \
243         } while (0)
244 #define read_lock_irqsave(lock, flags)                  \
245         do {                                            \
246                 typecheck(unsigned long, flags);        \
247                 _read_lock_irqsave(lock, flags);        \
248         } while (0)
249 #define write_lock_irqsave(lock, flags)                 \
250         do {                                            \
251                 typecheck(unsigned long, flags);        \
252                 _write_lock_irqsave(lock, flags);       \
253         } while (0)
254 #define spin_lock_irqsave_nested(lock, flags, subclass) \
255         spin_lock_irqsave(lock, flags)
256
257 #endif
258
259 #define spin_lock_irq(lock)             _spin_lock_irq(lock)
260 #define spin_lock_bh(lock)              _spin_lock_bh(lock)
261
262 #define read_lock_irq(lock)             _read_lock_irq(lock)
263 #define read_lock_bh(lock)              _read_lock_bh(lock)
264
265 #define write_lock_irq(lock)            _write_lock_irq(lock)
266 #define write_lock_bh(lock)             _write_lock_bh(lock)
267
268 /*
269  * We inline the unlock functions in the nondebug case:
270  */
271 #if defined(CONFIG_DEBUG_SPINLOCK) || defined(CONFIG_PREEMPT) || \
272         !defined(CONFIG_SMP)
273 # define spin_unlock(lock)              _spin_unlock(lock)
274 # define read_unlock(lock)              _read_unlock(lock)
275 # define write_unlock(lock)             _write_unlock(lock)
276 # define spin_unlock_irq(lock)          _spin_unlock_irq(lock)
277 # define read_unlock_irq(lock)          _read_unlock_irq(lock)
278 # define write_unlock_irq(lock)         _write_unlock_irq(lock)
279 #else
280 # define spin_unlock(lock) \
281     do {__raw_spin_unlock(&(lock)->raw_lock); __release(lock); } while (0)
282 # define read_unlock(lock) \
283     do {__raw_read_unlock(&(lock)->raw_lock); __release(lock); } while (0)
284 # define write_unlock(lock) \
285     do {__raw_write_unlock(&(lock)->raw_lock); __release(lock); } while (0)
286 # define spin_unlock_irq(lock)                  \
287 do {                                            \
288         __raw_spin_unlock(&(lock)->raw_lock);   \
289         __release(lock);                        \
290         local_irq_enable();                     \
291 } while (0)
292 # define read_unlock_irq(lock)                  \
293 do {                                            \
294         __raw_read_unlock(&(lock)->raw_lock);   \
295         __release(lock);                        \
296         local_irq_enable();                     \
297 } while (0)
298 # define write_unlock_irq(lock)                 \
299 do {                                            \
300         __raw_write_unlock(&(lock)->raw_lock);  \
301         __release(lock);                        \
302         local_irq_enable();                     \
303 } while (0)
304 #endif
305
306 #define spin_unlock_irqrestore(lock, flags)             \
307         do {                                            \
308                 typecheck(unsigned long, flags);        \
309                 _spin_unlock_irqrestore(lock, flags);   \
310         } while (0)
311 #define spin_unlock_bh(lock)            _spin_unlock_bh(lock)
312
313 #define read_unlock_irqrestore(lock, flags)             \
314         do {                                            \
315                 typecheck(unsigned long, flags);        \
316                 _read_unlock_irqrestore(lock, flags);   \
317         } while (0)
318 #define read_unlock_bh(lock)            _read_unlock_bh(lock)
319
320 #define write_unlock_irqrestore(lock, flags)            \
321         do {                                            \
322                 typecheck(unsigned long, flags);        \
323                 _write_unlock_irqrestore(lock, flags);  \
324         } while (0)
325 #define write_unlock_bh(lock)           _write_unlock_bh(lock)
326
327 #define spin_trylock_bh(lock)   __cond_lock(lock, _spin_trylock_bh(lock))
328
329 #define spin_trylock_irq(lock) \
330 ({ \
331         local_irq_disable(); \
332         spin_trylock(lock) ? \
333         1 : ({ local_irq_enable(); 0;  }); \
334 })
335
336 #define spin_trylock_irqsave(lock, flags) \
337 ({ \
338         local_irq_save(flags); \
339         spin_trylock(lock) ? \
340         1 : ({ local_irq_restore(flags); 0; }); \
341 })
342
343 #define write_trylock_irqsave(lock, flags) \
344 ({ \
345         local_irq_save(flags); \
346         write_trylock(lock) ? \
347         1 : ({ local_irq_restore(flags); 0; }); \
348 })
349
350 /*
351  * Pull the atomic_t declaration:
352  * (asm-mips/atomic.h needs above definitions)
353  */
354 #include <asm/atomic.h>
355 /**
356  * atomic_dec_and_lock - lock on reaching reference count zero
357  * @atomic: the atomic counter
358  * @lock: the spinlock in question
359  *
360  * Decrements @atomic by 1.  If the result is 0, returns true and locks
361  * @lock.  Returns false for all other cases.
362  */
363 extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
364 #define atomic_dec_and_lock(atomic, lock) \
365                 __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
366
367 /**
368  * spin_can_lock - would spin_trylock() succeed?
369  * @lock: the spinlock in question.
370  */
371 #define spin_can_lock(lock)     (!spin_is_locked(lock))
372
373 #else /* DDE_LINUX */
374
375 #define spin_lock_init(l) \
376         do { \
377                 ddekit_lock_init(&(l)->ddekit_lock); \
378                 (l)->need_init = 0; \
379         } while (0);
380
381 #define rwlock_init(l) spin_lock_init(l)
382
383 #define COND_INIT_LOCK(l) \
384         do { \
385                 if ((l)->need_init) spin_lock_init(l); \
386         } while (0);
387
388 #define spin_lock(lock) \
389         do { \
390                 COND_INIT_LOCK(lock); \
391                 preempt_disable(); \
392                 ddekit_lock_lock(&(lock)->ddekit_lock); \
393         } while (0)
394
395 #define read_lock(lock) spin_lock(lock)
396 #define write_lock(lock) spin_lock(lock)
397 #define spin_lock_irq(lock) local_irq_disable(); spin_lock(lock)
398 #define spin_lock_bh(lock) spin_lock(lock)
399 #define read_lock_irq(lock) spin_lock_irq(lock)
400 #define read_lock_bh(lock) spin_lock_bh(lock)
401 #define write_lock_irq(lock) spin_lock_irq(lock)
402 #define write_lock_bh(lock) spin_lock_bh(lock)
403
404 #define spin_unlock(lock) \
405         do { \
406                 COND_INIT_LOCK(lock); \
407                 ddekit_lock_unlock(&(lock)->ddekit_lock); \
408                 preempt_enable(); \
409         } while (0)
410
411 #define read_unlock(lock) spin_unlock(lock)
412 #define write_unlock(lock) spin_unlock(lock)
413
414 #define spin_unlock_irq(lock) spin_unlock(lock); local_irq_enable()
415 #define spin_unlock_bh(lock) spin_unlock(lock)
416 #define read_unlock_irq(lock) spin_unlock_irq(lock)
417 #define read_unlock_bh(lock) spin_unlock_bh(lock)
418 #define write_unlock_irq(lock) spin_unlock_irq(lock)
419 #define write_unlock_bh(lock) spin_unlock_bh(lock)
420
421 #define spin_lock_irqsave(lock, flags) \
422         do { \
423                 local_irq_save(flags); \
424                 spin_lock(lock);\
425         } while (0);
426
427 #define read_lock_irqsave(lock, flags)  spin_lock_irqsave(lock, flags)
428 #define write_lock_irqsave(lock, flags) spin_lock_irqsave(lock, flags)
429
430 #define spin_unlock_irqrestore(lock, flags) \
431         do { \
432                 spin_unlock(lock); \
433                 local_irq_restore(flags); \
434         } while (0);
435
436 #define read_unlock_irqrestore(lock, flags) spin_unlock_irqrestore(lock, flags)
437 #define write_unlock_irqrestore(lock, flags) spin_unlock_irqrestore(lock, flags)
438
439 static int __lockfunc spin_trylock(spinlock_t *lock)
440 {
441         COND_INIT_LOCK(lock);
442         return ddekit_lock_try_lock(&lock->ddekit_lock) == 0;
443 }
444
445 #define _raw_spin_lock(l) spin_lock(l)
446 #define _raw_spin_unlock(l) spin_unlock(l)
447 #define _raw_spin_trylock(l) spin_trylock(l)
448
449 #define spin_trylock_irqsave(lock, flags) \
450 ({ \
451         local_irq_save(flags); \
452         spin_trylock(lock) ? \
453         1 : ({ local_irq_restore(flags); 0; }); \
454 })
455
456 #define read_trylock(l) spin_trylock(l)
457 #define write_trylock(l) read_trylock(l)
458
459 #define spin_is_locked(x) \
460                 (ddekit_lock_owner(&(x)->ddekit_lock) != 0)
461
462 #define assert_spin_locked(x)   BUG_ON(!spin_is_locked(x))
463
464
465 #endif /* DDE_LINUX */
466
467 #endif /* __LINUX_SPINLOCK_H */