1 #ifndef __ARCH_S390_ATOMIC__
2 #define __ARCH_S390_ATOMIC__
4 #include <linux/compiler.h>
7 * include/asm-s390/atomic.h
10 * Copyright (C) 1999-2005 IBM Deutschland Entwicklung GmbH, IBM Corporation
11 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
12 * Denis Joseph Barrow,
13 * Arnd Bergmann (arndb@de.ibm.com)
15 * Derived from "include/asm-i386/bitops.h"
16 * Copyright (C) 1992, Linus Torvalds
21 * Atomic operations that C can't guarantee us. Useful for
22 * resource counting etc..
23 * S390 uses 'Compare And Swap' for atomicity in SMP enviroment
28 } __attribute__ ((aligned (4))) atomic_t;
29 #define ATOMIC_INIT(i) { (i) }
33 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
35 #define __CS_LOOP(ptr, op_val, op_string) ({ \
36 typeof(ptr->counter) old_val, new_val; \
40 op_string " %1,%3\n" \
43 : "=&d" (old_val), "=&d" (new_val), \
44 "=Q" (((atomic_t *)(ptr))->counter) \
45 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
52 #define __CS_LOOP(ptr, op_val, op_string) ({ \
53 typeof(ptr->counter) old_val, new_val; \
57 op_string " %1,%4\n" \
60 : "=&d" (old_val), "=&d" (new_val), \
61 "=m" (((atomic_t *)(ptr))->counter) \
62 : "a" (ptr), "d" (op_val), \
63 "m" (((atomic_t *)(ptr))->counter) \
70 static inline int atomic_read(const atomic_t *v)
76 static inline void atomic_set(atomic_t *v, int i)
82 static __inline__ int atomic_add_return(int i, atomic_t * v)
84 return __CS_LOOP(v, i, "ar");
86 #define atomic_add(_i, _v) atomic_add_return(_i, _v)
87 #define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
88 #define atomic_inc(_v) atomic_add_return(1, _v)
89 #define atomic_inc_return(_v) atomic_add_return(1, _v)
90 #define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
92 static __inline__ int atomic_sub_return(int i, atomic_t * v)
94 return __CS_LOOP(v, i, "sr");
96 #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
97 #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
98 #define atomic_dec(_v) atomic_sub_return(1, _v)
99 #define atomic_dec_return(_v) atomic_sub_return(1, _v)
100 #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
102 static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t * v)
104 __CS_LOOP(v, ~mask, "nr");
107 static __inline__ void atomic_set_mask(unsigned long mask, atomic_t * v)
109 __CS_LOOP(v, mask, "or");
112 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
114 static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
116 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
119 : "+d" (old), "=Q" (v->counter)
120 : "d" (new), "Q" (v->counter)
125 : "+d" (old), "=m" (v->counter)
126 : "a" (v), "d" (new), "m" (v->counter)
128 #endif /* __GNUC__ */
132 static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
137 if (unlikely(c == u))
139 old = atomic_cmpxchg(v, c, c + a);
140 if (likely(old == c))
147 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
154 } __attribute__ ((aligned (8))) atomic64_t;
155 #define ATOMIC64_INIT(i) { (i) }
157 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
159 #define __CSG_LOOP(ptr, op_val, op_string) ({ \
160 typeof(ptr->counter) old_val, new_val; \
164 op_string " %1,%3\n" \
167 : "=&d" (old_val), "=&d" (new_val), \
168 "=Q" (((atomic_t *)(ptr))->counter) \
169 : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
170 : "cc", "memory" ); \
176 #define __CSG_LOOP(ptr, op_val, op_string) ({ \
177 typeof(ptr->counter) old_val, new_val; \
181 op_string " %1,%4\n" \
182 " csg %0,%1,0(%3)\n" \
184 : "=&d" (old_val), "=&d" (new_val), \
185 "=m" (((atomic_t *)(ptr))->counter) \
186 : "a" (ptr), "d" (op_val), \
187 "m" (((atomic_t *)(ptr))->counter) \
188 : "cc", "memory" ); \
192 #endif /* __GNUC__ */
194 static inline long long atomic64_read(const atomic64_t *v)
200 static inline void atomic64_set(atomic64_t *v, long long i)
206 static __inline__ long long atomic64_add_return(long long i, atomic64_t * v)
208 return __CSG_LOOP(v, i, "agr");
210 #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
211 #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
212 #define atomic64_inc(_v) atomic64_add_return(1, _v)
213 #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
214 #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
216 static __inline__ long long atomic64_sub_return(long long i, atomic64_t * v)
218 return __CSG_LOOP(v, i, "sgr");
220 #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
221 #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
222 #define atomic64_dec(_v) atomic64_sub_return(1, _v)
223 #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
224 #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
226 static __inline__ void atomic64_clear_mask(unsigned long mask, atomic64_t * v)
228 __CSG_LOOP(v, ~mask, "ngr");
231 static __inline__ void atomic64_set_mask(unsigned long mask, atomic64_t * v)
233 __CSG_LOOP(v, mask, "ogr");
236 #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
238 static __inline__ long long atomic64_cmpxchg(atomic64_t *v,
239 long long old, long long new)
241 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
244 : "+d" (old), "=Q" (v->counter)
245 : "d" (new), "Q" (v->counter)
250 : "+d" (old), "=m" (v->counter)
251 : "a" (v), "d" (new), "m" (v->counter)
253 #endif /* __GNUC__ */
257 static __inline__ int atomic64_add_unless(atomic64_t *v,
258 long long a, long long u)
261 c = atomic64_read(v);
263 if (unlikely(c == u))
265 old = atomic64_cmpxchg(v, c, c + a);
266 if (likely(old == c))
273 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
278 #define smp_mb__before_atomic_dec() smp_mb()
279 #define smp_mb__after_atomic_dec() smp_mb()
280 #define smp_mb__before_atomic_inc() smp_mb()
281 #define smp_mb__after_atomic_inc() smp_mb()
283 #include <asm-generic/atomic.h>
284 #endif /* __KERNEL__ */
285 #endif /* __ARCH_S390_ATOMIC__ */