ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-alpha / spinlock.h
1 #ifndef _ALPHA_SPINLOCK_H
2 #define _ALPHA_SPINLOCK_H
3
4 #include <linux/config.h>
5 #include <asm/system.h>
6 #include <linux/kernel.h>
7 #include <asm/current.h>
8
9
10 /*
11  * Simple spin lock operations.  There are two variants, one clears IRQ's
12  * on the local processor, one does not.
13  *
14  * We make no fairness assumptions. They have a cost.
15  */
16
17 typedef struct {
18         volatile unsigned int lock /*__attribute__((aligned(32))) */;
19 #ifdef CONFIG_DEBUG_SPINLOCK
20         int on_cpu;
21         int line_no;
22         void *previous;
23         struct task_struct * task;
24         const char *base_file;
25 #endif
26 } spinlock_t;
27
28 #ifdef CONFIG_DEBUG_SPINLOCK
29 #define SPIN_LOCK_UNLOCKED (spinlock_t) {0, -1, 0, 0, 0, 0}
30 #define spin_lock_init(x)                                               \
31         ((x)->lock = 0, (x)->on_cpu = -1, (x)->previous = 0, (x)->task = 0)
32 #else
33 #define SPIN_LOCK_UNLOCKED      (spinlock_t) { 0 }
34 #define spin_lock_init(x)       ((x)->lock = 0)
35 #endif
36
37 #define spin_is_locked(x)       ((x)->lock != 0)
38 #define spin_unlock_wait(x)     ({ do { barrier(); } while ((x)->lock); })
39
40 #ifdef CONFIG_DEBUG_SPINLOCK
41 extern void _raw_spin_unlock(spinlock_t * lock);
42 extern void debug_spin_lock(spinlock_t * lock, const char *, int);
43 extern int debug_spin_trylock(spinlock_t * lock, const char *, int);
44
45 #define _raw_spin_lock(LOCK) debug_spin_lock(LOCK, __BASE_FILE__, __LINE__)
46 #define _raw_spin_trylock(LOCK) debug_spin_trylock(LOCK, __BASE_FILE__, __LINE__)
47
48 #define spin_lock_own(LOCK, LOCATION)                                   \
49 do {                                                                    \
50         if (!((LOCK)->lock && (LOCK)->on_cpu == smp_processor_id()))    \
51                 printk("%s: called on %d from %p but lock %s on %d\n",  \
52                        LOCATION, smp_processor_id(),                    \
53                        __builtin_return_address(0),                     \
54                        (LOCK)->lock ? "taken" : "freed", (LOCK)->on_cpu); \
55 } while (0)
56 #else
57 static inline void _raw_spin_unlock(spinlock_t * lock)
58 {
59         mb();
60         lock->lock = 0;
61 }
62
63 static inline void _raw_spin_lock(spinlock_t * lock)
64 {
65         long tmp;
66
67         /* Use sub-sections to put the actual loop at the end
68            of this object file's text section so as to perfect
69            branch prediction.  */
70         __asm__ __volatile__(
71         "1:     ldl_l   %0,%1\n"
72         "       blbs    %0,2f\n"
73         "       or      %0,1,%0\n"
74         "       stl_c   %0,%1\n"
75         "       beq     %0,2f\n"
76         "       mb\n"
77         ".subsection 2\n"
78         "2:     ldl     %0,%1\n"
79         "       blbs    %0,2b\n"
80         "       br      1b\n"
81         ".previous"
82         : "=&r" (tmp), "=m" (lock->lock)
83         : "m"(lock->lock) : "memory");
84 }
85
86 static inline int _raw_spin_trylock(spinlock_t *lock)
87 {
88         return !test_and_set_bit(0, &lock->lock);
89 }
90
91 #define spin_lock_own(LOCK, LOCATION)   ((void)0)
92 #endif /* CONFIG_DEBUG_SPINLOCK */
93
94 /***********************************************************/
95
96 typedef struct {
97         volatile int write_lock:1, read_counter:31;
98 } /*__attribute__((aligned(32)))*/ rwlock_t;
99
100 #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0 }
101
102 #define rwlock_init(x)  do { *(x) = RW_LOCK_UNLOCKED; } while(0)
103 #define rwlock_is_locked(x)     (*(volatile int *)(x) != 0)
104
105 #ifdef CONFIG_DEBUG_RWLOCK
106 extern void _raw_write_lock(rwlock_t * lock);
107 extern void _raw_read_lock(rwlock_t * lock);
108 #else
109 static inline void _raw_write_lock(rwlock_t * lock)
110 {
111         long regx;
112
113         __asm__ __volatile__(
114         "1:     ldl_l   %1,%0\n"
115         "       bne     %1,6f\n"
116         "       or      $31,1,%1\n"
117         "       stl_c   %1,%0\n"
118         "       beq     %1,6f\n"
119         "       mb\n"
120         ".subsection 2\n"
121         "6:     ldl     %1,%0\n"
122         "       bne     %1,6b\n"
123         "       br      1b\n"
124         ".previous"
125         : "=m" (*lock), "=&r" (regx)
126         : "0" (*lock) : "memory");
127 }
128
129 static inline void _raw_read_lock(rwlock_t * lock)
130 {
131         long regx;
132
133         __asm__ __volatile__(
134         "1:     ldl_l   %1,%0\n"
135         "       blbs    %1,6f\n"
136         "       subl    %1,2,%1\n"
137         "       stl_c   %1,%0\n"
138         "       beq     %1,6f\n"
139         "4:     mb\n"
140         ".subsection 2\n"
141         "6:     ldl     %1,%0\n"
142         "       blbs    %1,6b\n"
143         "       br      1b\n"
144         ".previous"
145         : "=m" (*lock), "=&r" (regx)
146         : "m" (*lock) : "memory");
147 }
148 #endif /* CONFIG_DEBUG_RWLOCK */
149
150 static inline void _raw_write_unlock(rwlock_t * lock)
151 {
152         mb();
153         *(volatile int *)lock = 0;
154 }
155
156 static inline void _raw_read_unlock(rwlock_t * lock)
157 {
158         long regx;
159         __asm__ __volatile__(
160         "       mb\n"
161         "1:     ldl_l   %1,%0\n"
162         "       addl    %1,2,%1\n"
163         "       stl_c   %1,%0\n"
164         "       beq     %1,6f\n"
165         ".subsection 2\n"
166         "6:     br      1b\n"
167         ".previous"
168         : "=m" (*lock), "=&r" (regx)
169         : "m" (*lock) : "memory");
170 }
171
172 #endif /* _ALPHA_SPINLOCK_H */