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 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
120 static __inline__ long atomic64_add_return(long i, atomic64_t * v)
123 __asm__ __volatile__(
133 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
134 :"Ir" (i), "m" (v->counter) : "memory");
138 static __inline__ long atomic_sub_return(int i, atomic_t * v)
141 __asm__ __volatile__(
151 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
152 :"Ir" (i), "m" (v->counter) : "memory");
156 static __inline__ long atomic64_sub_return(long i, atomic64_t * v)
159 __asm__ __volatile__(
169 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
170 :"Ir" (i), "m" (v->counter) : "memory");
174 #define atomic_dec_return(v) atomic_sub_return(1,(v))
175 #define atomic64_dec_return(v) atomic64_sub_return(1,(v))
177 #define atomic_inc_return(v) atomic_add_return(1,(v))
178 #define atomic64_inc_return(v) atomic64_add_return(1,(v))
180 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
181 #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
183 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
184 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
185 #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
187 #define atomic_inc(v) atomic_add(1,(v))
188 #define atomic64_inc(v) atomic64_add(1,(v))
190 #define atomic_dec(v) atomic_sub(1,(v))
191 #define atomic64_dec(v) atomic64_sub(1,(v))
193 #define smp_mb__before_atomic_dec() smp_mb()
194 #define smp_mb__after_atomic_dec() smp_mb()
195 #define smp_mb__before_atomic_inc() smp_mb()
196 #define smp_mb__after_atomic_inc() smp_mb()
198 #endif /* _ALPHA_ATOMIC_H */