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;
20 #define ATOMIC_INIT(i) ( (atomic_t) { (i) } )
22 #define atomic_read(v) ((v)->counter)
23 #define atomic_set(v,i) ((v)->counter = (i))
26 * To get proper branch prediction for the main line, we must branch
27 * forward to code at the end of this object's .text section, then
28 * branch back to restart the operation.
31 static __inline__ void atomic_add(int i, atomic_t * v)
42 :"=&r" (temp), "=m" (v->counter)
43 :"Ir" (i), "m" (v->counter));
46 static __inline__ void atomic_sub(int i, atomic_t * v)
57 :"=&r" (temp), "=m" (v->counter)
58 :"Ir" (i), "m" (v->counter));
62 * Same as above, but return the result value
64 static __inline__ long atomic_add_return(int i, atomic_t * v)
77 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
78 :"Ir" (i), "m" (v->counter) : "memory");
82 static __inline__ long atomic_sub_return(int i, atomic_t * v)
95 :"=&r" (temp), "=m" (v->counter), "=&r" (result)
96 :"Ir" (i), "m" (v->counter) : "memory");
100 #define atomic_dec_return(v) atomic_sub_return(1,(v))
101 #define atomic_inc_return(v) atomic_add_return(1,(v))
103 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
104 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
106 #define atomic_inc(v) atomic_add(1,(v))
107 #define atomic_dec(v) atomic_sub(1,(v))
109 #define smp_mb__before_atomic_dec() smp_mb()
110 #define smp_mb__after_atomic_dec() smp_mb()
111 #define smp_mb__before_atomic_inc() smp_mb()
112 #define smp_mb__after_atomic_inc() smp_mb()
114 #endif /* _ALPHA_ATOMIC_H */