Skip to content

Commit 1f51dee

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking/atomic, arch/alpha: Implement atomic{,64}_fetch_{add,sub,and,andnot,or,xor}()
Implement FETCH-OP atomic primitives, these are very similar to the existing OP-RETURN primitives we already have, except they return the value of the atomic variable _before_ modification. This is especially useful for irreversible operations -- such as bitops (because it becomes impossible to reconstruct the state prior to modification). Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Matt Turner <mattst88@gmail.com> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Richard Henderson <rth@twiddle.net> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-alpha@vger.kernel.org Cc: linux-arch@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent b316ff7 commit 1f51dee

1 file changed

Lines changed: 56 additions & 9 deletions

File tree

‎arch/alpha/include/asm/atomic.h‎

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ static inline int atomic_##op##_return(int i, atomic_t *v) \
6565
return result; \
6666
}
6767

68+
#define ATOMIC_FETCH_OP(op, asm_op) \
69+
static inline int atomic_fetch_##op(int i, atomic_t *v) \
70+
{ \
71+
long temp, result; \
72+
smp_mb(); \
73+
__asm__ __volatile__( \
74+
"1: ldl_l %2,%1\n" \
75+
" " #asm_op " %2,%3,%0\n" \
76+
" stl_c %0,%1\n" \
77+
" beq %0,2f\n" \
78+
".subsection 2\n" \
79+
"2: br 1b\n" \
80+
".previous" \
81+
:"=&r" (temp), "=m" (v->counter), "=&r" (result) \
82+
:"Ir" (i), "m" (v->counter) : "memory"); \
83+
smp_mb(); \
84+
return result; \
85+
}
86+
6887
#define ATOMIC64_OP(op, asm_op) \
6988
static __inline__ void atomic64_##op(long i, atomic64_t * v) \
7089
{ \
@@ -101,30 +120,58 @@ static __inline__ long atomic64_##op##_return(long i, atomic64_t * v) \
101120
return result; \
102121
}
103122

123+
#define ATOMIC64_FETCH_OP(op, asm_op) \
124+
static __inline__ long atomic64_fetch_##op(long i, atomic64_t * v) \
125+
{ \
126+
long temp, result; \
127+
smp_mb(); \
128+
__asm__ __volatile__( \
129+
"1: ldq_l %2,%1\n" \
130+
" " #asm_op " %2,%3,%0\n" \
131+
" stq_c %0,%1\n" \
132+
" beq %0,2f\n" \
133+
".subsection 2\n" \
134+
"2: br 1b\n" \
135+
".previous" \
136+
:"=&r" (temp), "=m" (v->counter), "=&r" (result) \
137+
:"Ir" (i), "m" (v->counter) : "memory"); \
138+
smp_mb(); \
139+
return result; \
140+
}
141+
104142
#define ATOMIC_OPS(op) \
105143
ATOMIC_OP(op, op##l) \
106144
ATOMIC_OP_RETURN(op, op##l) \
145+
ATOMIC_FETCH_OP(op, op##l) \
107146
ATOMIC64_OP(op, op##q) \
108-
ATOMIC64_OP_RETURN(op, op##q)
147+
ATOMIC64_OP_RETURN(op, op##q) \
148+
ATOMIC64_FETCH_OP(op, op##q)
109149

110150
ATOMIC_OPS(add)
111151
ATOMIC_OPS(sub)
112152

113153
#define atomic_andnot atomic_andnot
114154
#define atomic64_andnot atomic64_andnot
115155

116-
ATOMIC_OP(and, and)
117-
ATOMIC_OP(andnot, bic)
118-
ATOMIC_OP(or, bis)
119-
ATOMIC_OP(xor, xor)
120-
ATOMIC64_OP(and, and)
121-
ATOMIC64_OP(andnot, bic)
122-
ATOMIC64_OP(or, bis)
123-
ATOMIC64_OP(xor, xor)
156+
#define atomic_fetch_or atomic_fetch_or
157+
158+
#undef ATOMIC_OPS
159+
#define ATOMIC_OPS(op, asm) \
160+
ATOMIC_OP(op, asm) \
161+
ATOMIC_FETCH_OP(op, asm) \
162+
ATOMIC64_OP(op, asm) \
163+
ATOMIC64_FETCH_OP(op, asm)
164+
165+
ATOMIC_OPS(and, and)
166+
ATOMIC_OPS(andnot, bic)
167+
ATOMIC_OPS(or, bis)
168+
ATOMIC_OPS(xor, xor)
124169

125170
#undef ATOMIC_OPS
171+
#undef ATOMIC64_FETCH_OP
126172
#undef ATOMIC64_OP_RETURN
127173
#undef ATOMIC64_OP
174+
#undef ATOMIC_FETCH_OP
128175
#undef ATOMIC_OP_RETURN
129176
#undef ATOMIC_OP
130177

0 commit comments

Comments
 (0)