1 #ifndef _ALPHA_ATOMIC_H
2 #define _ALPHA_ATOMIC_H
4 #include <linux/types.h>
5 #include <asm/barrier.h>
6 #include <asm/cmpxchg.h>
9 * Atomic operations that C can't guarantee us. Useful for
10 * resource counting etc...
12 * But use these as seldom as possible since they are much slower
13 * than regular operations.
17 #define ATOMIC_INIT(i) { (i) }
18 #define ATOMIC64_INIT(i) { (i) }
20 #define atomic_read(v) ACCESS_ONCE((v)->counter)
21 #define atomic64_read(v) ACCESS_ONCE((v)->counter)
23 #define atomic_set(v,i) ((v)->counter = (i))
24 #define atomic64_set(v,i) ((v)->counter = (i))
27 * To get proper branch prediction for the main line, we must branch
28 * forward to code at the end of this object's .text section, then
29 * branch back to restart the operation.
32 #define ATOMIC_OP(op) \
33 static __inline__ void atomic_##op(int i, atomic_t * v) \
36 __asm__ __volatile__( \
38 " " #op "l %0,%2,%0\n" \
44 :"=&r" (temp), "=m" (v->counter) \
45 :"Ir" (i), "m" (v->counter)); \
48 #define ATOMIC_OP_RETURN(op) \
49 static inline int atomic_##op##_return(int i, atomic_t *v) \
53 __asm__ __volatile__( \
55 " " #op "l %0,%3,%2\n" \
56 " " #op "l %0,%3,%0\n" \
62 :"=&r" (temp), "=m" (v->counter), "=&r" (result) \
63 :"Ir" (i), "m" (v->counter) : "memory"); \
68 #define ATOMIC64_OP(op) \
69 static __inline__ void atomic64_##op(long i, atomic64_t * v) \
72 __asm__ __volatile__( \
74 " " #op "q %0,%2,%0\n" \
80 :"=&r" (temp), "=m" (v->counter) \
81 :"Ir" (i), "m" (v->counter)); \
84 #define ATOMIC64_OP_RETURN(op) \
85 static __inline__ long atomic64_##op##_return(long i, atomic64_t * v) \
89 __asm__ __volatile__( \
91 " " #op "q %0,%3,%2\n" \
92 " " #op "q %0,%3,%0\n" \
98 :"=&r" (temp), "=m" (v->counter), "=&r" (result) \
99 :"Ir" (i), "m" (v->counter) : "memory"); \
104 #define ATOMIC_OPS(opg) \
106 ATOMIC_OP_RETURN(opg) \
108 ATOMIC64_OP_RETURN(opg)
114 #undef ATOMIC64_OP_RETURN
116 #undef ATOMIC_OP_RETURN
119 #define atomic64_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
120 #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
122 #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
123 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
126 * __atomic_add_unless - add unless the number is a given value
127 * @v: pointer of type atomic_t
128 * @a: the amount to add to v...
129 * @u: ...unless v is equal to u.
131 * Atomically adds @a to @v, so long as it was not @u.
132 * Returns the old value of @v.
134 static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
138 __asm__ __volatile__(
139 "1: ldl_l %[old],%[mem]\n"
140 " cmpeq %[old],%[u],%[c]\n"
141 " addl %[old],%[a],%[new]\n"
143 " stl_c %[new],%[mem]\n"
149 : [old] "=&r"(old), [new] "=&r"(new), [c] "=&r"(c)
150 : [mem] "m"(*v), [a] "rI"(a), [u] "rI"((long)u)
158 * atomic64_add_unless - add unless the number is a given value
159 * @v: pointer of type atomic64_t
160 * @a: the amount to add to v...
161 * @u: ...unless v is equal to u.
163 * Atomically adds @a to @v, so long as it was not @u.
164 * Returns true iff @v was not @u.
166 static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
170 __asm__ __volatile__(
171 "1: ldq_l %[tmp],%[mem]\n"
172 " cmpeq %[tmp],%[u],%[c]\n"
173 " addq %[tmp],%[a],%[tmp]\n"
175 " stq_c %[tmp],%[mem]\n"
181 : [tmp] "=&r"(tmp), [c] "=&r"(c)
182 : [mem] "m"(*v), [a] "rI"(a), [u] "rI"(u)
189 * atomic64_dec_if_positive - decrement by 1 if old value positive
190 * @v: pointer of type atomic_t
192 * The function returns the old value of *v minus 1, even if
193 * the atomic variable, v, was not decremented.
195 static inline long atomic64_dec_if_positive(atomic64_t *v)
199 __asm__ __volatile__(
200 "1: ldq_l %[old],%[mem]\n"
201 " subq %[old],1,%[tmp]\n"
203 " stq_c %[tmp],%[mem]\n"
209 : [old] "=&r"(old), [tmp] "=&r"(tmp)
216 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
218 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
219 #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
221 #define atomic_dec_return(v) atomic_sub_return(1,(v))
222 #define atomic64_dec_return(v) atomic64_sub_return(1,(v))
224 #define atomic_inc_return(v) atomic_add_return(1,(v))
225 #define atomic64_inc_return(v) atomic64_add_return(1,(v))
227 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
228 #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
230 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
231 #define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0)
233 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
234 #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
236 #define atomic_inc(v) atomic_add(1,(v))
237 #define atomic64_inc(v) atomic64_add(1,(v))
239 #define atomic_dec(v) atomic_sub(1,(v))
240 #define atomic64_dec(v) atomic64_sub(1,(v))
242 #endif /* _ALPHA_ATOMIC_H */