Skip to content

Commit d6dfe25

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking,arch,metag: Fold atomic_ops
Many of the atomic op implementations are the same except for one instruction; fold the lot into a few CPP macros and reduce LoC. This also prepares for easy addition of new ops. Signed-off-by: Peter Zijlstra <peterz@infradead.org> Acked-by: James Hogan <james.hogan@imgtec.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: linux-metag@vger.kernel.org Link: http://lkml.kernel.org/r/20140508135852.453864110@infradead.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent d839bae commit d6dfe25

2 files changed

Lines changed: 77 additions & 120 deletions

File tree

‎arch/metag/include/asm/atomic_lnkget.h‎

Lines changed: 46 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -27,85 +27,56 @@ static inline int atomic_read(const atomic_t *v)
2727
return temp;
2828
}
2929

30-
static inline void atomic_add(int i, atomic_t *v)
31-
{
32-
int temp;
33-
34-
asm volatile (
35-
"1: LNKGETD %0, [%1]\n"
36-
" ADD %0, %0, %2\n"
37-
" LNKSETD [%1], %0\n"
38-
" DEFR %0, TXSTAT\n"
39-
" ANDT %0, %0, #HI(0x3f000000)\n"
40-
" CMPT %0, #HI(0x02000000)\n"
41-
" BNZ 1b\n"
42-
: "=&d" (temp)
43-
: "da" (&v->counter), "bd" (i)
44-
: "cc");
30+
#define ATOMIC_OP(op) \
31+
static inline void atomic_##op(int i, atomic_t *v) \
32+
{ \
33+
int temp; \
34+
\
35+
asm volatile ( \
36+
"1: LNKGETD %0, [%1]\n" \
37+
" " #op " %0, %0, %2\n" \
38+
" LNKSETD [%1], %0\n" \
39+
" DEFR %0, TXSTAT\n" \
40+
" ANDT %0, %0, #HI(0x3f000000)\n" \
41+
" CMPT %0, #HI(0x02000000)\n" \
42+
" BNZ 1b\n" \
43+
: "=&d" (temp) \
44+
: "da" (&v->counter), "bd" (i) \
45+
: "cc"); \
46+
} \
47+
48+
#define ATOMIC_OP_RETURN(op) \
49+
static inline int atomic_##op##_return(int i, atomic_t *v) \
50+
{ \
51+
int result, temp; \
52+
\
53+
smp_mb(); \
54+
\
55+
asm volatile ( \
56+
"1: LNKGETD %1, [%2]\n" \
57+
" " #op " %1, %1, %3\n" \
58+
" LNKSETD [%2], %1\n" \
59+
" DEFR %0, TXSTAT\n" \
60+
" ANDT %0, %0, #HI(0x3f000000)\n" \
61+
" CMPT %0, #HI(0x02000000)\n" \
62+
" BNZ 1b\n" \
63+
: "=&d" (temp), "=&da" (result) \
64+
: "da" (&v->counter), "bd" (i) \
65+
: "cc"); \
66+
\
67+
smp_mb(); \
68+
\
69+
return result; \
4570
}
4671

47-
static inline void atomic_sub(int i, atomic_t *v)
48-
{
49-
int temp;
72+
#define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op)
5073

51-
asm volatile (
52-
"1: LNKGETD %0, [%1]\n"
53-
" SUB %0, %0, %2\n"
54-
" LNKSETD [%1], %0\n"
55-
" DEFR %0, TXSTAT\n"
56-
" ANDT %0, %0, #HI(0x3f000000)\n"
57-
" CMPT %0, #HI(0x02000000)\n"
58-
" BNZ 1b\n"
59-
: "=&d" (temp)
60-
: "da" (&v->counter), "bd" (i)
61-
: "cc");
62-
}
74+
ATOMIC_OPS(add)
75+
ATOMIC_OPS(sub)
6376

64-
static inline int atomic_add_return(int i, atomic_t *v)
65-
{
66-
int result, temp;
67-
68-
smp_mb();
69-
70-
asm volatile (
71-
"1: LNKGETD %1, [%2]\n"
72-
" ADD %1, %1, %3\n"
73-
" LNKSETD [%2], %1\n"
74-
" DEFR %0, TXSTAT\n"
75-
" ANDT %0, %0, #HI(0x3f000000)\n"
76-
" CMPT %0, #HI(0x02000000)\n"
77-
" BNZ 1b\n"
78-
: "=&d" (temp), "=&da" (result)
79-
: "da" (&v->counter), "bd" (i)
80-
: "cc");
81-
82-
smp_mb();
83-
84-
return result;
85-
}
86-
87-
static inline int atomic_sub_return(int i, atomic_t *v)
88-
{
89-
int result, temp;
90-
91-
smp_mb();
92-
93-
asm volatile (
94-
"1: LNKGETD %1, [%2]\n"
95-
" SUB %1, %1, %3\n"
96-
" LNKSETD [%2], %1\n"
97-
" DEFR %0, TXSTAT\n"
98-
" ANDT %0, %0, #HI(0x3f000000)\n"
99-
" CMPT %0, #HI(0x02000000)\n"
100-
" BNZ 1b\n"
101-
: "=&d" (temp), "=&da" (result)
102-
: "da" (&v->counter), "bd" (i)
103-
: "cc");
104-
105-
smp_mb();
106-
107-
return result;
108-
}
77+
#undef ATOMIC_OPS
78+
#undef ATOMIC_OP_RETURN
79+
#undef ATOMIC_OP
10980

11081
static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
11182
{

‎arch/metag/include/asm/atomic_lock1.h‎

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,55 +37,41 @@ static inline int atomic_set(atomic_t *v, int i)
3737
return i;
3838
}
3939

40-
static inline void atomic_add(int i, atomic_t *v)
41-
{
42-
unsigned long flags;
43-
44-
__global_lock1(flags);
45-
fence();
46-
v->counter += i;
47-
__global_unlock1(flags);
40+
#define ATOMIC_OP(op, c_op) \
41+
static inline void atomic_##op(int i, atomic_t *v) \
42+
{ \
43+
unsigned long flags; \
44+
\
45+
__global_lock1(flags); \
46+
fence(); \
47+
v->counter c_op i; \
48+
__global_unlock1(flags); \
49+
} \
50+
51+
#define ATOMIC_OP_RETURN(op, c_op) \
52+
static inline int atomic_##op##_return(int i, atomic_t *v) \
53+
{ \
54+
unsigned long result; \
55+
unsigned long flags; \
56+
\
57+
__global_lock1(flags); \
58+
result = v->counter; \
59+
result c_op i; \
60+
fence(); \
61+
v->counter = result; \
62+
__global_unlock1(flags); \
63+
\
64+
return result; \
4865
}
4966

50-
static inline void atomic_sub(int i, atomic_t *v)
51-
{
52-
unsigned long flags;
67+
#define ATOMIC_OPS(op, c_op) ATOMIC_OP(op, c_op) ATOMIC_OP_RETURN(op, c_op)
5368

54-
__global_lock1(flags);
55-
fence();
56-
v->counter -= i;
57-
__global_unlock1(flags);
58-
}
59-
60-
static inline int atomic_add_return(int i, atomic_t *v)
61-
{
62-
unsigned long result;
63-
unsigned long flags;
69+
ATOMIC_OPS(add, +=)
70+
ATOMIC_OPS(sub, -=)
6471

65-
__global_lock1(flags);
66-
result = v->counter;
67-
result += i;
68-
fence();
69-
v->counter = result;
70-
__global_unlock1(flags);
71-
72-
return result;
73-
}
74-
75-
static inline int atomic_sub_return(int i, atomic_t *v)
76-
{
77-
unsigned long result;
78-
unsigned long flags;
79-
80-
__global_lock1(flags);
81-
result = v->counter;
82-
result -= i;
83-
fence();
84-
v->counter = result;
85-
__global_unlock1(flags);
86-
87-
return result;
88-
}
72+
#undef ATOMIC_OPS
73+
#undef ATOMIC_OP_RETURN
74+
#undef ATOMIC_OP
8975

9076
static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
9177
{

0 commit comments

Comments
 (0)