1 #ifndef _ALPHA_ATOMIC_H
2 #define _ALPHA_ATOMIC_H
5 * Atomic operations that C can't guarantee us. Useful for
6 * resource counting etc...
8 * But use these as seldom as possible since they are much slower
9 * than regular operations.
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.
18 typedef struct { volatile int counter; } atomic_t;
19 typedef struct { volatile long counter; } atomic64_t;
21 #define ATOMIC_INIT(i) ( (atomic_t) { (i) } )
22 #define ATOMIC64_INIT(i) ( (atomic64_t) { (i) } )
24 #define atomic_read(v) ((v)->counter + 0)
25 #define atomic64_read(v) ((v)->counter + 0)
27 #define atomic_set(v,i) ((v)->counter = (i))
28 #define atomic64_set(v,i) ((v)->counter = (i))
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.
36 static __inline__ void atomic_add(int i, atomic_t * v)
47 :"=&r" (temp), "=m" (v->counter)
48 :"Ir" (i), "m" (v->counter));
51 static __inline__ void atomic64_add(long i, atomic64_t * v)
62 :"=&r" (temp), "=m" (v->counter)
63 :"Ir" (i), "m" (v->counter));
66 static __inline__ void atomic_sub(int i, atomic_t * v)
77 :"=&r" (temp), "=m" (v->counter)
78 :"Ir" (i), "m" (v->counter));
81 static __inline__ void atomic64_sub(long i, atomic64_t * v)
92 :"=&r" (temp), "=m" (v->counter)
93 :"Ir" (i), "m" (v->counter));
98 * Same as above, but return the result value
100 static __inline__ long atomic_add_return(int i, atomic_t * v)
104 __asm__ __volatile__(
113 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
114 :"Ir" (i), "m" (v->counter) : "memory");
119 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
121 static __inline__ long atomic64_add_return(long i, atomic64_t * v)
125 __asm__ __volatile__(
134 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
135 :"Ir" (i), "m" (v->counter) : "memory");
140 static __inline__ long atomic_sub_return(int i, atomic_t * v)
144 __asm__ __volatile__(
153 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
154 :"Ir" (i), "m" (v->counter) : "memory");
159 static __inline__ long atomic64_sub_return(long i, atomic64_t * v)
163 __asm__ __volatile__(
172 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
173 :"Ir" (i), "m" (v->counter) : "memory");
178 #define atomic_dec_return(v) atomic_sub_return(1,(v))
179 #define atomic64_dec_return(v) atomic64_sub_return(1,(v))
181 #define atomic_inc_return(v) atomic_add_return(1,(v))
182 #define atomic64_inc_return(v) atomic64_add_return(1,(v))
184 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
185 #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
187 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
188 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
189 #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
191 #define atomic_inc(v) atomic_add(1,(v))
192 #define atomic64_inc(v) atomic64_add(1,(v))
194 #define atomic_dec(v) atomic_sub(1,(v))
195 #define atomic64_dec(v) atomic64_sub(1,(v))
197 #define smp_mb__before_atomic_dec() smp_mb()
198 #define smp_mb__after_atomic_dec() smp_mb()
199 #define smp_mb__before_atomic_inc() smp_mb()
200 #define smp_mb__after_atomic_inc() smp_mb()
202 #endif /* _ALPHA_ATOMIC_H */