Skip to content

Commit 2856f5e

Browse files
Mathieu DesnoyersLinus Torvalds
authored andcommitted
atomic.h: atomic_add_unless as inline. Remove system.h atomic.h circular dependency
atomic_add_unless as inline. Remove system.h atomic.h circular dependency. I agree (with Andi Kleen) this typeof is not needed and more error prone. All the original atomic.h code that uses cmpxchg (which includes the atomic_add_unless) uses defines instead of inline functions, probably to circumvent a circular dependency between system.h and atomic.h on powerpc (which my patch addresses). Therefore, it makes sense to use inline functions that will provide type checking. atomic_add_unless as inline. Remove system.h atomic.h circular dependency. Digging into the FRV architecture shows me that it is also affected by such a circular dependency. Here is the diff applying this against the rest of my atomic.h patches. It applies over the atomic.h standardization patches. Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 79d365a commit 2856f5e

18 files changed

Lines changed: 360 additions & 282 deletions

File tree

‎include/asm-alpha/atomic.h‎

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _ALPHA_ATOMIC_H
33

44
#include <asm/barrier.h>
5+
#include <asm/system.h>
56

67
/*
78
* Atomic operations that C can't guarantee us. Useful for
@@ -190,20 +191,21 @@ static __inline__ long atomic64_sub_return(long i, atomic64_t * v)
190191
* Atomically adds @a to @v, so long as it was not @u.
191192
* Returns non-zero if @v was not @u, and zero otherwise.
192193
*/
193-
#define atomic_add_unless(v, a, u) \
194-
({ \
195-
__typeof__((v)->counter) c, old; \
196-
c = atomic_read(v); \
197-
for (;;) { \
198-
if (unlikely(c == (u))) \
199-
break; \
200-
old = atomic_cmpxchg((v), c, c + (a)); \
201-
if (likely(old == c)) \
202-
break; \
203-
c = old; \
204-
} \
205-
c != (u); \
206-
})
194+
static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
195+
{
196+
int c, old;
197+
c = atomic_read(v);
198+
for (;;) {
199+
if (unlikely(c == (u)))
200+
break;
201+
old = atomic_cmpxchg((v), c, c + (a));
202+
if (likely(old == c))
203+
break;
204+
c = old;
205+
}
206+
return c != (u);
207+
}
208+
207209
#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
208210

209211
/**
@@ -215,20 +217,21 @@ static __inline__ long atomic64_sub_return(long i, atomic64_t * v)
215217
* Atomically adds @a to @v, so long as it was not @u.
216218
* Returns non-zero if @v was not @u, and zero otherwise.
217219
*/
218-
#define atomic64_add_unless(v, a, u) \
219-
({ \
220-
__typeof__((v)->counter) c, old; \
221-
c = atomic64_read(v); \
222-
for (;;) { \
223-
if (unlikely(c == (u))) \
224-
break; \
225-
old = atomic64_cmpxchg((v), c, c + (a)); \
226-
if (likely(old == c)) \
227-
break; \
228-
c = old; \
229-
} \
230-
c != (u); \
231-
})
220+
static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
221+
{
222+
long c, old;
223+
c = atomic64_read(v);
224+
for (;;) {
225+
if (unlikely(c == (u)))
226+
break;
227+
old = atomic64_cmpxchg((v), c, c + (a));
228+
if (likely(old == c))
229+
break;
230+
c = old;
231+
}
232+
return c != (u);
233+
}
234+
232235
#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
233236

234237
#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)

‎include/asm-arm/atomic.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define __ASM_ARM_ATOMIC_H
1313

1414
#include <linux/compiler.h>
15+
#include <asm/system.h>
1516

1617
typedef struct { volatile int counter; } atomic_t;
1718

‎include/asm-arm26/atomic.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#ifndef __ASM_ARM_ATOMIC_H
2121
#define __ASM_ARM_ATOMIC_H
2222

23-
2423
#ifdef CONFIG_SMP
2524
#error SMP is NOT supported
2625
#endif

‎include/asm-frv/atomic.h‎

Lines changed: 15 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <linux/types.h>
1818
#include <asm/spr-regs.h>
19+
#include <asm/system.h>
1920

2021
#ifdef CONFIG_SMP
2122
#error not SMP safe
@@ -258,85 +259,23 @@ extern uint32_t __xchg_32(uint32_t i, volatile void *v);
258259

259260
#define tas(ptr) (xchg((ptr), 1))
260261

261-
/*****************************************************************************/
262-
/*
263-
* compare and conditionally exchange value with memory
264-
* - if (*ptr == test) then orig = *ptr; *ptr = test;
265-
* - if (*ptr != test) then orig = *ptr;
266-
*/
267-
#ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
268-
269-
#define cmpxchg(ptr, test, new) \
270-
({ \
271-
__typeof__(ptr) __xg_ptr = (ptr); \
272-
__typeof__(*(ptr)) __xg_orig, __xg_tmp; \
273-
__typeof__(*(ptr)) __xg_test = (test); \
274-
__typeof__(*(ptr)) __xg_new = (new); \
275-
\
276-
switch (sizeof(__xg_orig)) { \
277-
case 4: \
278-
asm volatile( \
279-
"0: \n" \
280-
" orcc gr0,gr0,gr0,icc3 \n" \
281-
" ckeq icc3,cc7 \n" \
282-
" ld.p %M0,%1 \n" \
283-
" orcr cc7,cc7,cc3 \n" \
284-
" sub%I4cc %1,%4,%2,icc0 \n" \
285-
" bne icc0,#0,1f \n" \
286-
" cst.p %3,%M0 ,cc3,#1 \n" \
287-
" corcc gr29,gr29,gr0 ,cc3,#1 \n" \
288-
" beq icc3,#0,0b \n" \
289-
"1: \n" \
290-
: "+U"(*__xg_ptr), "=&r"(__xg_orig), "=&r"(__xg_tmp) \
291-
: "r"(__xg_new), "NPr"(__xg_test) \
292-
: "memory", "cc7", "cc3", "icc3", "icc0" \
293-
); \
294-
break; \
295-
\
296-
default: \
297-
__xg_orig = 0; \
298-
asm volatile("break"); \
299-
break; \
300-
} \
301-
\
302-
__xg_orig; \
303-
})
304-
305-
#else
306-
307-
extern uint32_t __cmpxchg_32(uint32_t *v, uint32_t test, uint32_t new);
308-
309-
#define cmpxchg(ptr, test, new) \
310-
({ \
311-
__typeof__(ptr) __xg_ptr = (ptr); \
312-
__typeof__(*(ptr)) __xg_orig; \
313-
__typeof__(*(ptr)) __xg_test = (test); \
314-
__typeof__(*(ptr)) __xg_new = (new); \
315-
\
316-
switch (sizeof(__xg_orig)) { \
317-
case 4: __xg_orig = __cmpxchg_32(__xg_ptr, __xg_test, __xg_new); break; \
318-
default: \
319-
__xg_orig = 0; \
320-
asm volatile("break"); \
321-
break; \
322-
} \
323-
\
324-
__xg_orig; \
325-
})
326-
327-
#endif
328-
329262
#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
330263
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
331264

332-
#define atomic_add_unless(v, a, u) \
333-
({ \
334-
int c, old; \
335-
c = atomic_read(v); \
336-
while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
337-
c = old; \
338-
c != (u); \
339-
})
265+
static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
266+
{
267+
int c, old;
268+
c = atomic_read(v);
269+
for (;;) {
270+
if (unlikely(c == (u)))
271+
break;
272+
old = atomic_cmpxchg((v), c, c + (a));
273+
if (likely(old == c))
274+
break;
275+
c = old;
276+
}
277+
return c != (u);
278+
}
340279

341280
#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
342281

‎include/asm-frv/system.h‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#define _ASM_SYSTEM_H
1414

1515
#include <linux/linkage.h>
16-
#include <asm/atomic.h>
1716

1817
struct thread_struct;
1918

@@ -197,4 +196,73 @@ extern void free_initmem(void);
197196

198197
#define arch_align_stack(x) (x)
199198

199+
/*****************************************************************************/
200+
/*
201+
* compare and conditionally exchange value with memory
202+
* - if (*ptr == test) then orig = *ptr; *ptr = test;
203+
* - if (*ptr != test) then orig = *ptr;
204+
*/
205+
#ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
206+
207+
#define cmpxchg(ptr, test, new) \
208+
({ \
209+
__typeof__(ptr) __xg_ptr = (ptr); \
210+
__typeof__(*(ptr)) __xg_orig, __xg_tmp; \
211+
__typeof__(*(ptr)) __xg_test = (test); \
212+
__typeof__(*(ptr)) __xg_new = (new); \
213+
\
214+
switch (sizeof(__xg_orig)) { \
215+
case 4: \
216+
asm volatile( \
217+
"0: \n" \
218+
" orcc gr0,gr0,gr0,icc3 \n" \
219+
" ckeq icc3,cc7 \n" \
220+
" ld.p %M0,%1 \n" \
221+
" orcr cc7,cc7,cc3 \n" \
222+
" sub%I4cc %1,%4,%2,icc0 \n" \
223+
" bne icc0,#0,1f \n" \
224+
" cst.p %3,%M0 ,cc3,#1 \n" \
225+
" corcc gr29,gr29,gr0 ,cc3,#1 \n" \
226+
" beq icc3,#0,0b \n" \
227+
"1: \n" \
228+
: "+U"(*__xg_ptr), "=&r"(__xg_orig), "=&r"(__xg_tmp) \
229+
: "r"(__xg_new), "NPr"(__xg_test) \
230+
: "memory", "cc7", "cc3", "icc3", "icc0" \
231+
); \
232+
break; \
233+
\
234+
default: \
235+
__xg_orig = 0; \
236+
asm volatile("break"); \
237+
break; \
238+
} \
239+
\
240+
__xg_orig; \
241+
})
242+
243+
#else
244+
245+
extern uint32_t __cmpxchg_32(uint32_t *v, uint32_t test, uint32_t new);
246+
247+
#define cmpxchg(ptr, test, new) \
248+
({ \
249+
__typeof__(ptr) __xg_ptr = (ptr); \
250+
__typeof__(*(ptr)) __xg_orig; \
251+
__typeof__(*(ptr)) __xg_test = (test); \
252+
__typeof__(*(ptr)) __xg_new = (new); \
253+
\
254+
switch (sizeof(__xg_orig)) { \
255+
case 4: __xg_orig = __cmpxchg_32(__xg_ptr, __xg_test, __xg_new); break; \
256+
default: \
257+
__xg_orig = 0; \
258+
asm volatile("break"); \
259+
break; \
260+
} \
261+
\
262+
__xg_orig; \
263+
})
264+
265+
#endif
266+
267+
200268
#endif /* _ASM_SYSTEM_H */

‎include/asm-generic/atomic.h‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010

1111
#include <asm/types.h>
12-
#include <asm/system.h>
1312

1413
/*
1514
* Suppport for atomic_long_t
@@ -123,8 +122,12 @@ static inline long atomic_long_dec_return(atomic_long_t *l)
123122
return (long)atomic64_dec_return(v);
124123
}
125124

126-
#define atomic_long_add_unless(l, a, u) \
127-
atomic64_add_unless((atomic64_t *)(l), (a), (u))
125+
static inline long atomic_long_add_unless(atomic_long_t *l, long a, long u)
126+
{
127+
atomic64_t *v = (atomic64_t *)l;
128+
129+
return (long)atomic64_add_unless(v, a, u);
130+
}
128131

129132
#define atomic_long_inc_not_zero(l) atomic64_inc_not_zero((atomic64_t *)(l))
130133

@@ -236,8 +239,12 @@ static inline long atomic_long_dec_return(atomic_long_t *l)
236239
return (long)atomic_dec_return(v);
237240
}
238241

239-
#define atomic_long_add_unless(l, a, u) \
240-
atomic_add_unless((atomic_t *)(l), (a), (u))
242+
static inline long atomic_long_add_unless(atomic_long_t *l, long a, long u)
243+
{
244+
atomic_t *v = (atomic_t *)l;
245+
246+
return (long)atomic_add_unless(v, a, u);
247+
}
241248

242249
#define atomic_long_inc_not_zero(l) atomic_inc_not_zero((atomic_t *)(l))
243250

‎include/asm-i386/atomic.h‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,20 +219,21 @@ static __inline__ int atomic_sub_return(int i, atomic_t *v)
219219
* Atomically adds @a to @v, so long as @v was not already @u.
220220
* Returns non-zero if @v was not @u, and zero otherwise.
221221
*/
222-
#define atomic_add_unless(v, a, u) \
223-
({ \
224-
__typeof__((v)->counter) c, old; \
225-
c = atomic_read(v); \
226-
for (;;) { \
227-
if (unlikely(c == (u))) \
228-
break; \
229-
old = atomic_cmpxchg((v), c, c + (a)); \
230-
if (likely(old == c)) \
231-
break; \
232-
c = old; \
233-
} \
234-
c != (u); \
235-
})
222+
static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
223+
{
224+
int c, old;
225+
c = atomic_read(v);
226+
for (;;) {
227+
if (unlikely(c == (u)))
228+
break;
229+
old = atomic_cmpxchg((v), c, c + (a));
230+
if (likely(old == c))
231+
break;
232+
c = old;
233+
}
234+
return c != (u);
235+
}
236+
236237
#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
237238

238239
#define atomic_inc_return(v) (atomic_add_return(1,v))

0 commit comments

Comments
 (0)