1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_ATOMIC_LONG_H
3 #define _ASM_GENERIC_ATOMIC_LONG_H
5 * Copyright (C) 2005 Silicon Graphics, Inc.
8 * Allows to provide arch independent atomic definitions without the need to
9 * edit all arch specific atomic.h files.
12 #include <asm/types.h>
15 * Suppport for atomic_long_t
17 * Casts for parameters are avoided for existing atomic functions in order to
18 * avoid issues with cast-as-lval under gcc 4.x and other limitations that the
19 * macros of a platform may have.
22 #if BITS_PER_LONG == 64
24 typedef atomic64_t atomic_long_t;
26 #define ATOMIC_LONG_INIT(i) ATOMIC64_INIT(i)
27 #define ATOMIC_LONG_PFX(x) atomic64 ## x
28 #define ATOMIC_LONG_TYPE s64
32 typedef atomic_t atomic_long_t;
34 #define ATOMIC_LONG_INIT(i) ATOMIC_INIT(i)
35 #define ATOMIC_LONG_PFX(x) atomic ## x
36 #define ATOMIC_LONG_TYPE int
40 #define ATOMIC_LONG_READ_OP(mo) \
41 static inline long atomic_long_read##mo(const atomic_long_t *l) \
43 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
45 return (long)ATOMIC_LONG_PFX(_read##mo)(v); \
48 ATOMIC_LONG_READ_OP(_acquire)
50 #undef ATOMIC_LONG_READ_OP
52 #define ATOMIC_LONG_SET_OP(mo) \
53 static inline void atomic_long_set##mo(atomic_long_t *l, long i) \
55 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
57 ATOMIC_LONG_PFX(_set##mo)(v, i); \
60 ATOMIC_LONG_SET_OP(_release)
62 #undef ATOMIC_LONG_SET_OP
64 #define ATOMIC_LONG_ADD_SUB_OP(op, mo) \
66 atomic_long_##op##_return##mo(long i, atomic_long_t *l) \
68 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
70 return (long)ATOMIC_LONG_PFX(_##op##_return##mo)(i, v); \
72 ATOMIC_LONG_ADD_SUB_OP(add,)
73 ATOMIC_LONG_ADD_SUB_OP(add, _relaxed)
74 ATOMIC_LONG_ADD_SUB_OP(add, _acquire)
75 ATOMIC_LONG_ADD_SUB_OP(add, _release)
76 ATOMIC_LONG_ADD_SUB_OP(sub,)
77 ATOMIC_LONG_ADD_SUB_OP(sub, _relaxed)
78 ATOMIC_LONG_ADD_SUB_OP(sub, _acquire)
79 ATOMIC_LONG_ADD_SUB_OP(sub, _release)
81 #undef ATOMIC_LONG_ADD_SUB_OP
83 #define atomic_long_cmpxchg_relaxed(l, old, new) \
84 (ATOMIC_LONG_PFX(_cmpxchg_relaxed)((ATOMIC_LONG_PFX(_t) *)(l), \
86 #define atomic_long_cmpxchg_acquire(l, old, new) \
87 (ATOMIC_LONG_PFX(_cmpxchg_acquire)((ATOMIC_LONG_PFX(_t) *)(l), \
89 #define atomic_long_cmpxchg_release(l, old, new) \
90 (ATOMIC_LONG_PFX(_cmpxchg_release)((ATOMIC_LONG_PFX(_t) *)(l), \
92 #define atomic_long_cmpxchg(l, old, new) \
93 (ATOMIC_LONG_PFX(_cmpxchg)((ATOMIC_LONG_PFX(_t) *)(l), (old), (new)))
96 #define atomic_long_try_cmpxchg_relaxed(l, old, new) \
97 (ATOMIC_LONG_PFX(_try_cmpxchg_relaxed)((ATOMIC_LONG_PFX(_t) *)(l), \
98 (ATOMIC_LONG_TYPE *)(old), (ATOMIC_LONG_TYPE)(new)))
99 #define atomic_long_try_cmpxchg_acquire(l, old, new) \
100 (ATOMIC_LONG_PFX(_try_cmpxchg_acquire)((ATOMIC_LONG_PFX(_t) *)(l), \
101 (ATOMIC_LONG_TYPE *)(old), (ATOMIC_LONG_TYPE)(new)))
102 #define atomic_long_try_cmpxchg_release(l, old, new) \
103 (ATOMIC_LONG_PFX(_try_cmpxchg_release)((ATOMIC_LONG_PFX(_t) *)(l), \
104 (ATOMIC_LONG_TYPE *)(old), (ATOMIC_LONG_TYPE)(new)))
105 #define atomic_long_try_cmpxchg(l, old, new) \
106 (ATOMIC_LONG_PFX(_try_cmpxchg)((ATOMIC_LONG_PFX(_t) *)(l), \
107 (ATOMIC_LONG_TYPE *)(old), (ATOMIC_LONG_TYPE)(new)))
110 #define atomic_long_xchg_relaxed(v, new) \
111 (ATOMIC_LONG_PFX(_xchg_relaxed)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
112 #define atomic_long_xchg_acquire(v, new) \
113 (ATOMIC_LONG_PFX(_xchg_acquire)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
114 #define atomic_long_xchg_release(v, new) \
115 (ATOMIC_LONG_PFX(_xchg_release)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
116 #define atomic_long_xchg(v, new) \
117 (ATOMIC_LONG_PFX(_xchg)((ATOMIC_LONG_PFX(_t) *)(v), (new)))
119 static __always_inline void atomic_long_inc(atomic_long_t *l)
121 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
123 ATOMIC_LONG_PFX(_inc)(v);
126 static __always_inline void atomic_long_dec(atomic_long_t *l)
128 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
130 ATOMIC_LONG_PFX(_dec)(v);
133 #define ATOMIC_LONG_FETCH_OP(op, mo) \
135 atomic_long_fetch_##op##mo(long i, atomic_long_t *l) \
137 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
139 return (long)ATOMIC_LONG_PFX(_fetch_##op##mo)(i, v); \
142 ATOMIC_LONG_FETCH_OP(add, )
143 ATOMIC_LONG_FETCH_OP(add, _relaxed)
144 ATOMIC_LONG_FETCH_OP(add, _acquire)
145 ATOMIC_LONG_FETCH_OP(add, _release)
146 ATOMIC_LONG_FETCH_OP(sub, )
147 ATOMIC_LONG_FETCH_OP(sub, _relaxed)
148 ATOMIC_LONG_FETCH_OP(sub, _acquire)
149 ATOMIC_LONG_FETCH_OP(sub, _release)
150 ATOMIC_LONG_FETCH_OP(and, )
151 ATOMIC_LONG_FETCH_OP(and, _relaxed)
152 ATOMIC_LONG_FETCH_OP(and, _acquire)
153 ATOMIC_LONG_FETCH_OP(and, _release)
154 ATOMIC_LONG_FETCH_OP(andnot, )
155 ATOMIC_LONG_FETCH_OP(andnot, _relaxed)
156 ATOMIC_LONG_FETCH_OP(andnot, _acquire)
157 ATOMIC_LONG_FETCH_OP(andnot, _release)
158 ATOMIC_LONG_FETCH_OP(or, )
159 ATOMIC_LONG_FETCH_OP(or, _relaxed)
160 ATOMIC_LONG_FETCH_OP(or, _acquire)
161 ATOMIC_LONG_FETCH_OP(or, _release)
162 ATOMIC_LONG_FETCH_OP(xor, )
163 ATOMIC_LONG_FETCH_OP(xor, _relaxed)
164 ATOMIC_LONG_FETCH_OP(xor, _acquire)
165 ATOMIC_LONG_FETCH_OP(xor, _release)
167 #undef ATOMIC_LONG_FETCH_OP
169 #define ATOMIC_LONG_FETCH_INC_DEC_OP(op, mo) \
171 atomic_long_fetch_##op##mo(atomic_long_t *l) \
173 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
175 return (long)ATOMIC_LONG_PFX(_fetch_##op##mo)(v); \
178 ATOMIC_LONG_FETCH_INC_DEC_OP(inc,)
179 ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _relaxed)
180 ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _acquire)
181 ATOMIC_LONG_FETCH_INC_DEC_OP(inc, _release)
182 ATOMIC_LONG_FETCH_INC_DEC_OP(dec,)
183 ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _relaxed)
184 ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _acquire)
185 ATOMIC_LONG_FETCH_INC_DEC_OP(dec, _release)
187 #undef ATOMIC_LONG_FETCH_INC_DEC_OP
189 #define ATOMIC_LONG_OP(op) \
190 static __always_inline void \
191 atomic_long_##op(long i, atomic_long_t *l) \
193 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
195 ATOMIC_LONG_PFX(_##op)(i, v); \
201 ATOMIC_LONG_OP(andnot)
205 #undef ATOMIC_LONG_OP
207 static inline int atomic_long_sub_and_test(long i, atomic_long_t *l)
209 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
211 return ATOMIC_LONG_PFX(_sub_and_test)(i, v);
214 static inline int atomic_long_dec_and_test(atomic_long_t *l)
216 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
218 return ATOMIC_LONG_PFX(_dec_and_test)(v);
221 static inline int atomic_long_inc_and_test(atomic_long_t *l)
223 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
225 return ATOMIC_LONG_PFX(_inc_and_test)(v);
228 static inline int atomic_long_add_negative(long i, atomic_long_t *l)
230 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
232 return ATOMIC_LONG_PFX(_add_negative)(i, v);
235 #define ATOMIC_LONG_INC_DEC_OP(op, mo) \
237 atomic_long_##op##_return##mo(atomic_long_t *l) \
239 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l; \
241 return (long)ATOMIC_LONG_PFX(_##op##_return##mo)(v); \
243 ATOMIC_LONG_INC_DEC_OP(inc,)
244 ATOMIC_LONG_INC_DEC_OP(inc, _relaxed)
245 ATOMIC_LONG_INC_DEC_OP(inc, _acquire)
246 ATOMIC_LONG_INC_DEC_OP(inc, _release)
247 ATOMIC_LONG_INC_DEC_OP(dec,)
248 ATOMIC_LONG_INC_DEC_OP(dec, _relaxed)
249 ATOMIC_LONG_INC_DEC_OP(dec, _acquire)
250 ATOMIC_LONG_INC_DEC_OP(dec, _release)
252 #undef ATOMIC_LONG_INC_DEC_OP
254 static inline long atomic_long_add_unless(atomic_long_t *l, long a, long u)
256 ATOMIC_LONG_PFX(_t) *v = (ATOMIC_LONG_PFX(_t) *)l;
258 return (long)ATOMIC_LONG_PFX(_add_unless)(v, a, u);
261 #define atomic_long_inc_not_zero(l) \
262 ATOMIC_LONG_PFX(_inc_not_zero)((ATOMIC_LONG_PFX(_t) *)(l))
264 #define atomic_long_cond_read_relaxed(v, c) \
265 ATOMIC_LONG_PFX(_cond_read_relaxed)((ATOMIC_LONG_PFX(_t) *)(v), (c))
266 #define atomic_long_cond_read_acquire(v, c) \
267 ATOMIC_LONG_PFX(_cond_read_acquire)((ATOMIC_LONG_PFX(_t) *)(v), (c))
269 #endif /* _ASM_GENERIC_ATOMIC_LONG_H */