1 #ifndef _ASM_IA64_ATOMIC_H
2 #define _ASM_IA64_ATOMIC_H
5 * Atomic operations that C can't guarantee us. Useful for
6 * resource counting etc..
8 * NOTE: don't mess with the types below! The "unsigned long" and
9 * "int" types were carefully placed so as to ensure proper operation
12 * Copyright (C) 1998, 1999, 2002-2003 Hewlett-Packard Co
13 * David Mosberger-Tang <davidm@hpl.hp.com>
15 #include <linux/types.h>
17 #include <asm/intrinsics.h>
20 * On IA-64, counter must always be volatile to ensure that that the
21 * memory accesses are ordered.
23 typedef struct { volatile __s32 counter; } atomic_t;
24 typedef struct { volatile __s64 counter; } atomic64_t;
26 #define ATOMIC_INIT(i) ((atomic_t) { (i) })
27 #define ATOMIC64_INIT(i) ((atomic64_t) { (i) })
29 #define atomic_read(v) ((v)->counter)
30 #define atomic64_read(v) ((v)->counter)
32 #define atomic_set(v,i) (((v)->counter) = (i))
33 #define atomic64_set(v,i) (((v)->counter) = (i))
36 ia64_atomic_add (int i, atomic_t *v)
45 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old);
50 ia64_atomic64_add (__s64 i, atomic64_t *v)
59 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old);
64 ia64_atomic_sub (int i, atomic_t *v)
73 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old);
78 ia64_atomic64_sub (__s64 i, atomic64_t *v)
87 } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old);
91 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
92 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
94 #define atomic_add_unless(v, a, u) \
99 if (unlikely(c == (u))) \
101 old = atomic_cmpxchg((v), c, c + (a)); \
102 if (likely(old == c)) \
108 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
110 #define atomic_add_return(i,v) \
112 int __ia64_aar_i = (i); \
113 (__builtin_constant_p(i) \
114 && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \
115 || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \
116 || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \
117 || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \
118 ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \
119 : ia64_atomic_add(__ia64_aar_i, v); \
122 #define atomic64_add_return(i,v) \
124 long __ia64_aar_i = (i); \
125 (__builtin_constant_p(i) \
126 && ( (__ia64_aar_i == 1) || (__ia64_aar_i == 4) \
127 || (__ia64_aar_i == 8) || (__ia64_aar_i == 16) \
128 || (__ia64_aar_i == -1) || (__ia64_aar_i == -4) \
129 || (__ia64_aar_i == -8) || (__ia64_aar_i == -16))) \
130 ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \
131 : ia64_atomic64_add(__ia64_aar_i, v); \
135 * Atomically add I to V and return TRUE if the resulting value is
138 static __inline__ int
139 atomic_add_negative (int i, atomic_t *v)
141 return atomic_add_return(i, v) < 0;
144 static __inline__ int
145 atomic64_add_negative (__s64 i, atomic64_t *v)
147 return atomic64_add_return(i, v) < 0;
150 #define atomic_sub_return(i,v) \
152 int __ia64_asr_i = (i); \
153 (__builtin_constant_p(i) \
154 && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \
155 || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \
156 || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \
157 || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \
158 ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \
159 : ia64_atomic_sub(__ia64_asr_i, v); \
162 #define atomic64_sub_return(i,v) \
164 long __ia64_asr_i = (i); \
165 (__builtin_constant_p(i) \
166 && ( (__ia64_asr_i == 1) || (__ia64_asr_i == 4) \
167 || (__ia64_asr_i == 8) || (__ia64_asr_i == 16) \
168 || (__ia64_asr_i == -1) || (__ia64_asr_i == -4) \
169 || (__ia64_asr_i == -8) || (__ia64_asr_i == -16))) \
170 ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \
171 : ia64_atomic64_sub(__ia64_asr_i, v); \
174 #define atomic_dec_return(v) atomic_sub_return(1, (v))
175 #define atomic_inc_return(v) atomic_add_return(1, (v))
176 #define atomic64_dec_return(v) atomic64_sub_return(1, (v))
177 #define atomic64_inc_return(v) atomic64_add_return(1, (v))
179 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
180 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
181 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
182 #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
183 #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
184 #define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0)
186 #define atomic_add(i,v) atomic_add_return((i), (v))
187 #define atomic_sub(i,v) atomic_sub_return((i), (v))
188 #define atomic_inc(v) atomic_add(1, (v))
189 #define atomic_dec(v) atomic_sub(1, (v))
191 #define atomic64_add(i,v) atomic64_add_return((i), (v))
192 #define atomic64_sub(i,v) atomic64_sub_return((i), (v))
193 #define atomic64_inc(v) atomic64_add(1, (v))
194 #define atomic64_dec(v) atomic64_sub(1, (v))
196 /* Atomic operations are already serializing */
197 #define smp_mb__before_atomic_dec() barrier()
198 #define smp_mb__after_atomic_dec() barrier()
199 #define smp_mb__before_atomic_inc() barrier()
200 #define smp_mb__after_atomic_inc() barrier()
202 #include <asm-generic/atomic.h>
203 #endif /* _ASM_IA64_ATOMIC_H */