vserver 1.9.5.x5
[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 #ifdef CONFIG_PREEMPT
27         unsigned int break_lock;
28 #endif
29 } spinlock_t;
30
31 #ifdef CONFIG_DEBUG_SPINLOCK
32 #define SPIN_LOCK_UNLOCKED (spinlock_t) {0, -1, 0, NULL, NULL, NULL}
33 #define spin_lock_init(x)                                               \
34         ((x)->lock = 0, (x)->on_cpu = -1, (x)->previous = NULL, (x)->task = NULL)
35 #else
36 #define SPIN_LOCK_UNLOCKED      (spinlock_t) { 0 }
37 #define spin_lock_init(x)       ((x)->lock = 0)
38 #endif
39
40 #define spin_is_locked(x)       ((x)->lock != 0)
41 #define spin_unlock_wait(x)     ({ do { barrier(); } while ((x)->lock); })
42 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
43
44 #ifdef CONFIG_DEBUG_SPINLOCK
45 extern void _raw_spin_unlock(spinlock_t * lock);
46 extern void debug_spin_lock(spinlock_t * lock, const char *, int);
47 extern int debug_spin_trylock(spinlock_t * lock, const char *, int);
48
49 #define _raw_spin_lock(LOCK) debug_spin_lock(LOCK, __BASE_FILE__, __LINE__)
50 #define _raw_spin_trylock(LOCK) debug_spin_trylock(LOCK, __BASE_FILE__, __LINE__)
51
52 #define spin_lock_own(LOCK, LOCATION)                                   \
53 do {                                                                    \
54         if (!((LOCK)->lock && (LOCK)->on_cpu == smp_processor_id()))    \
55                 printk("%s: called on %d from %p but lock %s on %d\n",  \
56                        LOCATION, smp_processor_id(),                    \
57                        __builtin_return_address(0),                     \
58                        (LOCK)->lock ? "taken" : "freed", (LOCK)->on_cpu); \
59 } while (0)
60 #else
61 static inline void _raw_spin_unlock(spinlock_t * lock)
62 {
63         mb();
64         lock->lock = 0;
65 }
66
67 static inline void _raw_spin_lock(spinlock_t * lock)
68 {
69         long tmp;
70
71         /* Use sub-sections to put the actual loop at the end
72            of this object file's text section so as to perfect
73            branch prediction.  */
74         __asm__ __volatile__(
75         "1:     ldl_l   %0,%1\n"
76         "       blbs    %0,2f\n"
77         "       or      %0,1,%0\n"
78         "       stl_c   %0,%1\n"
79         "       beq     %0,2f\n"
80         "       mb\n"
81         ".subsection 2\n"
82         "2:     ldl     %0,%1\n"
83         "       blbs    %0,2b\n"
84         "       br      1b\n"
85         ".previous"
86         : "=&r" (tmp), "=m" (lock->lock)
87         : "m"(lock->lock) : "memory");
88 }
89
90 static inline int _raw_spin_trylock(spinlock_t *lock)
91 {
92         return !test_and_set_bit(0, &lock->lock);
93 }
94
95 #define spin_lock_own(LOCK, LOCATION)   ((void)0)
96 #endif /* CONFIG_DEBUG_SPINLOCK */
97
98 /***********************************************************/
99
100 typedef struct {
101         volatile unsigned int write_lock:1, read_counter:31;
102 #ifdef CONFIG_PREEMPT
103         unsigned int break_lock;
104 #endif
105 } /*__attribute__((aligned(32)))*/ rwlock_t;
106
107 #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0 }
108
109 #define rwlock_init(x)  do { *(x) = RW_LOCK_UNLOCKED; } while(0)
110
111 #ifdef CONFIG_DEBUG_RWLOCK
112 extern void _raw_write_lock(rwlock_t * lock);
113 extern void _raw_read_lock(rwlock_t * lock);
114 #else
115 static inline void _raw_write_lock(rwlock_t * lock)
116 {
117         long regx;
118
119         __asm__ __volatile__(
120         "1:     ldl_l   %1,%0\n"
121         "       bne     %1,6f\n"
122         "       or      $31,1,%1\n"
123         "       stl_c   %1,%0\n"
124         "       beq     %1,6f\n"
125         "       mb\n"
126         ".subsection 2\n"
127         "6:     ldl     %1,%0\n"
128         "       bne     %1,6b\n"
129         "       br      1b\n"
130         ".previous"
131         : "=m" (*lock), "=&r" (regx)
132         : "m" (*lock) : "memory");
133 }
134
135 static inline void _raw_read_lock(rwlock_t * lock)
136 {
137         long regx;
138
139         __asm__ __volatile__(
140         "1:     ldl_l   %1,%0\n"
141         "       blbs    %1,6f\n"
142         "       subl    %1,2,%1\n"
143         "       stl_c   %1,%0\n"
144         "       beq     %1,6f\n"
145         "4:     mb\n"
146         ".subsection 2\n"
147         "6:     ldl     %1,%0\n"
148         "       blbs    %1,6b\n"
149         "       br      1b\n"
150         ".previous"
151         : "=m" (*lock), "=&r" (regx)
152         : "m" (*lock) : "memory");
153 }
154 #endif /* CONFIG_DEBUG_RWLOCK */
155
156 static inline int _raw_write_trylock(rwlock_t * lock)
157 {
158         long regx;
159         int success;
160
161         __asm__ __volatile__(
162         "1:     ldl_l   %1,%0\n"
163         "       lda     %2,0\n"
164         "       bne     %1,2f\n"
165         "       or      $31,1,%1\n"
166         "       stl_c   %1,%0\n"
167         "       beq     %1,6f\n"
168         "       lda     %2,1\n"
169         "2:     mb\n"
170         ".subsection 2\n"
171         "6:     br      1b\n"
172         ".previous"
173         : "=m" (*lock), "=&r" (regx), "=&r" (success)
174         : "m" (*lock) : "memory");
175
176         return success;
177 }
178
179 static inline void _raw_write_unlock(rwlock_t * lock)
180 {
181         mb();
182         *(volatile int *)lock = 0;
183 }
184
185 static inline void _raw_read_unlock(rwlock_t * lock)
186 {
187         long regx;
188         __asm__ __volatile__(
189         "       mb\n"
190         "1:     ldl_l   %1,%0\n"
191         "       addl    %1,2,%1\n"
192         "       stl_c   %1,%0\n"
193         "       beq     %1,6f\n"
194         ".subsection 2\n"
195         "6:     br      1b\n"
196         ".previous"
197         : "=m" (*lock), "=&r" (regx)
198         : "m" (*lock) : "memory");
199 }
200
201 #endif /* _ALPHA_SPINLOCK_H */