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)
103 __asm__ __volatile__(
113 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
114 :"Ir" (i), "m" (v->counter) : "memory");
118 static __inline__ long atomic64_add_return(long i, atomic64_t * v)
121 __asm__ __volatile__(
131 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
132 :"Ir" (i), "m" (v->counter) : "memory");
136 static __inline__ long atomic_sub_return(int i, atomic_t * v)
139 __asm__ __volatile__(
149 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
150 :"Ir" (i), "m" (v->counter) : "memory");
154 static __inline__ long atomic64_sub_return(long i, atomic64_t * v)
157 __asm__ __volatile__(
167 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
168 :"Ir" (i), "m" (v->counter) : "memory");
172 #define atomic_dec_return(v) atomic_sub_return(1,(v))
173 #define atomic64_dec_return(v) atomic64_sub_return(1,(v))
175 #define atomic_inc_return(v) atomic_add_return(1,(v))
176 #define atomic64_inc_return(v) atomic64_add_return(1,(v))
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)
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)
184 #define atomic_inc(v) atomic_add(1,(v))
185 #define atomic64_inc(v) atomic64_add(1,(v))
187 #define atomic_dec(v) atomic_sub(1,(v))
188 #define atomic64_dec(v) atomic64_sub(1,(v))
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()
195 #endif /* _ALPHA_ATOMIC_H */