ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-alpha / atomic.h
1 #ifndef _ALPHA_ATOMIC_H
2 #define _ALPHA_ATOMIC_H
3
4 /*
5  * Atomic operations that C can't guarantee us.  Useful for
6  * resource counting etc...
7  *
8  * But use these as seldom as possible since they are much slower
9  * than regular operations.
10  */
11
12
13 /*
14  * Counter is volatile to make sure gcc doesn't try to be clever
15  * and move things around on us. We need to use _exactly_ the address
16  * the user gave us, not some alias that contains the same information.
17  */
18 typedef struct { volatile int counter; } atomic_t;
19 typedef struct { volatile long counter; } atomic64_t;
20
21 #define ATOMIC_INIT(i)          ( (atomic_t) { (i) } )
22 #define ATOMIC64_INIT(i)        ( (atomic64_t) { (i) } )
23
24 #define atomic_read(v)          ((v)->counter + 0)
25 #define atomic64_read(v)        ((v)->counter + 0)
26
27 #define atomic_set(v,i)         ((v)->counter = (i))
28 #define atomic64_set(v,i)       ((v)->counter = (i))
29
30 /*
31  * To get proper branch prediction for the main line, we must branch
32  * forward to code at the end of this object's .text section, then
33  * branch back to restart the operation.
34  */
35
36 static __inline__ void atomic_add(int i, atomic_t * v)
37 {
38         unsigned long temp;
39         __asm__ __volatile__(
40         "1:     ldl_l %0,%1\n"
41         "       addl %0,%2,%0\n"
42         "       stl_c %0,%1\n"
43         "       beq %0,2f\n"
44         ".subsection 2\n"
45         "2:     br 1b\n"
46         ".previous"
47         :"=&r" (temp), "=m" (v->counter)
48         :"Ir" (i), "m" (v->counter));
49 }
50
51 static __inline__ void atomic64_add(long i, atomic64_t * v)
52 {
53         unsigned long temp;
54         __asm__ __volatile__(
55         "1:     ldq_l %0,%1\n"
56         "       addq %0,%2,%0\n"
57         "       stq_c %0,%1\n"
58         "       beq %0,2f\n"
59         ".subsection 2\n"
60         "2:     br 1b\n"
61         ".previous"
62         :"=&r" (temp), "=m" (v->counter)
63         :"Ir" (i), "m" (v->counter));
64 }
65
66 static __inline__ void atomic_sub(int i, atomic_t * v)
67 {
68         unsigned long temp;
69         __asm__ __volatile__(
70         "1:     ldl_l %0,%1\n"
71         "       subl %0,%2,%0\n"
72         "       stl_c %0,%1\n"
73         "       beq %0,2f\n"
74         ".subsection 2\n"
75         "2:     br 1b\n"
76         ".previous"
77         :"=&r" (temp), "=m" (v->counter)
78         :"Ir" (i), "m" (v->counter));
79 }
80
81 static __inline__ void atomic64_sub(long i, atomic64_t * v)
82 {
83         unsigned long temp;
84         __asm__ __volatile__(
85         "1:     ldq_l %0,%1\n"
86         "       subq %0,%2,%0\n"
87         "       stq_c %0,%1\n"
88         "       beq %0,2f\n"
89         ".subsection 2\n"
90         "2:     br 1b\n"
91         ".previous"
92         :"=&r" (temp), "=m" (v->counter)
93         :"Ir" (i), "m" (v->counter));
94 }
95
96
97 /*
98  * Same as above, but return the result value
99  */
100 static __inline__ long atomic_add_return(int i, atomic_t * v)
101 {
102         long temp, result;
103         __asm__ __volatile__(
104         "1:     ldl_l %0,%1\n"
105         "       addl %0,%3,%2\n"
106         "       addl %0,%3,%0\n"
107         "       stl_c %0,%1\n"
108         "       beq %0,2f\n"
109         "       mb\n"
110         ".subsection 2\n"
111         "2:     br 1b\n"
112         ".previous"
113         :"=&r" (temp), "=m" (v->counter), "=&r" (result)
114         :"Ir" (i), "m" (v->counter) : "memory");
115         return result;
116 }
117
118 static __inline__ long atomic64_add_return(long i, atomic64_t * v)
119 {
120         long temp, result;
121         __asm__ __volatile__(
122         "1:     ldq_l %0,%1\n"
123         "       addq %0,%3,%2\n"
124         "       addq %0,%3,%0\n"
125         "       stq_c %0,%1\n"
126         "       beq %0,2f\n"
127         "       mb\n"
128         ".subsection 2\n"
129         "2:     br 1b\n"
130         ".previous"
131         :"=&r" (temp), "=m" (v->counter), "=&r" (result)
132         :"Ir" (i), "m" (v->counter) : "memory");
133         return result;
134 }
135
136 static __inline__ long atomic_sub_return(int i, atomic_t * v)
137 {
138         long temp, result;
139         __asm__ __volatile__(
140         "1:     ldl_l %0,%1\n"
141         "       subl %0,%3,%2\n"
142         "       subl %0,%3,%0\n"
143         "       stl_c %0,%1\n"
144         "       beq %0,2f\n"
145         "       mb\n"
146         ".subsection 2\n"
147         "2:     br 1b\n"
148         ".previous"
149         :"=&r" (temp), "=m" (v->counter), "=&r" (result)
150         :"Ir" (i), "m" (v->counter) : "memory");
151         return result;
152 }
153
154 static __inline__ long atomic64_sub_return(long i, atomic64_t * v)
155 {
156         long temp, result;
157         __asm__ __volatile__(
158         "1:     ldq_l %0,%1\n"
159         "       subq %0,%3,%2\n"
160         "       subq %0,%3,%0\n"
161         "       stq_c %0,%1\n"
162         "       beq %0,2f\n"
163         "       mb\n"
164         ".subsection 2\n"
165         "2:     br 1b\n"
166         ".previous"
167         :"=&r" (temp), "=m" (v->counter), "=&r" (result)
168         :"Ir" (i), "m" (v->counter) : "memory");
169         return result;
170 }
171
172 #define atomic_dec_return(v) atomic_sub_return(1,(v))
173 #define atomic64_dec_return(v) atomic64_sub_return(1,(v))
174
175 #define atomic_inc_return(v) atomic_add_return(1,(v))
176 #define atomic64_inc_return(v) atomic64_add_return(1,(v))
177
178 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
179 #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
180
181 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
182 #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
183
184 #define atomic_inc(v) atomic_add(1,(v))
185 #define atomic64_inc(v) atomic64_add(1,(v))
186
187 #define atomic_dec(v) atomic_sub(1,(v))
188 #define atomic64_dec(v) atomic64_sub(1,(v))
189
190 #define smp_mb__before_atomic_dec()     smp_mb()
191 #define smp_mb__after_atomic_dec()      smp_mb()
192 #define smp_mb__before_atomic_inc()     smp_mb()
193 #define smp_mb__after_atomic_inc()      smp_mb()
194
195 #endif /* _ALPHA_ATOMIC_H */