Skip to content

Commit 5e0eb67

Browse files
ubizjakingomolnar
authored andcommitted
locking/local, arch: Rewrite local_add_unless() as a static inline function
Rewrite local_add_unless() as a static inline function with boolean return value, similar to the arch_atomic_add_unless() arch fallbacks. The function is currently unused. Signed-off-by: Uros Bizjak <ubizjak@gmail.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20230731084458.28096-1-ubizjak@gmail.com
1 parent 8788c6c commit 5e0eb67

5 files changed

Lines changed: 70 additions & 62 deletions

File tree

arch/alpha/include/asm/local.h

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,27 @@ static __inline__ bool local_try_cmpxchg(local_t *l, long *old, long new)
6565
#define local_xchg(l, n) (xchg_local(&((l)->a.counter), (n)))
6666

6767
/**
68-
* local_add_unless - add unless the number is a given value
68+
* local_add_unless - add unless the number is already a given value
6969
* @l: pointer of type local_t
7070
* @a: the amount to add to l...
7171
* @u: ...unless l is equal to u.
7272
*
73-
* Atomically adds @a to @l, so long as it was not @u.
74-
* Returns non-zero if @l was not @u, and zero otherwise.
73+
* Atomically adds @a to @l, if @v was not already @u.
74+
* Returns true if the addition was done.
7575
*/
76-
#define local_add_unless(l, a, u) \
77-
({ \
78-
long c, old; \
79-
c = local_read(l); \
80-
for (;;) { \
81-
if (unlikely(c == (u))) \
82-
break; \
83-
old = local_cmpxchg((l), c, c + (a)); \
84-
if (likely(old == c)) \
85-
break; \
86-
c = old; \
87-
} \
88-
c != (u); \
89-
})
76+
static __inline__ bool
77+
local_add_unless(local_t *l, long a, long u)
78+
{
79+
long c = local_read(l);
80+
81+
do {
82+
if (unlikely(c == u))
83+
return false;
84+
} while (!local_try_cmpxchg(l, &c, c + a));
85+
86+
return true;
87+
}
88+
9089
#define local_inc_not_zero(l) local_add_unless((l), 1, 0)
9190

9291
#define local_add_negative(a, l) (local_add_return((a), (l)) < 0)

arch/loongarch/include/asm/local.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,27 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new)
7070
#define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n)))
7171

7272
/**
73-
* local_add_unless - add unless the number is a given value
73+
* local_add_unless - add unless the number is already a given value
7474
* @l: pointer of type local_t
7575
* @a: the amount to add to l...
7676
* @u: ...unless l is equal to u.
7777
*
78-
* Atomically adds @a to @l, so long as it was not @u.
79-
* Returns non-zero if @l was not @u, and zero otherwise.
78+
* Atomically adds @a to @l, if @v was not already @u.
79+
* Returns true if the addition was done.
8080
*/
81-
#define local_add_unless(l, a, u) \
82-
({ \
83-
long c, old; \
84-
c = local_read(l); \
85-
while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \
86-
c = old; \
87-
c != (u); \
88-
})
81+
static inline bool
82+
local_add_unless(local_t *l, long a, long u)
83+
{
84+
long c = local_read(l);
85+
86+
do {
87+
if (unlikely(c == u))
88+
return false;
89+
} while (!local_try_cmpxchg(l, &c, c + a));
90+
91+
return true;
92+
}
93+
8994
#define local_inc_not_zero(l) local_add_unless((l), 1, 0)
9095

9196
#define local_dec_return(l) local_sub_return(1, (l))

arch/mips/include/asm/local.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,27 @@ static __inline__ bool local_try_cmpxchg(local_t *l, long *old, long new)
108108
#define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n)))
109109

110110
/**
111-
* local_add_unless - add unless the number is a given value
111+
* local_add_unless - add unless the number is already a given value
112112
* @l: pointer of type local_t
113113
* @a: the amount to add to l...
114114
* @u: ...unless l is equal to u.
115115
*
116-
* Atomically adds @a to @l, so long as it was not @u.
117-
* Returns non-zero if @l was not @u, and zero otherwise.
116+
* Atomically adds @a to @l, if @v was not already @u.
117+
* Returns true if the addition was done.
118118
*/
119-
#define local_add_unless(l, a, u) \
120-
({ \
121-
long c, old; \
122-
c = local_read(l); \
123-
while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \
124-
c = old; \
125-
c != (u); \
126-
})
119+
static __inline__ bool
120+
local_add_unless(local_t *l, long a, long u)
121+
{
122+
long c = local_read(l);
123+
124+
do {
125+
if (unlikely(c == u))
126+
return false;
127+
} while (!local_try_cmpxchg(l, &c, c + a));
128+
129+
return true;
130+
}
131+
127132
#define local_inc_not_zero(l) local_add_unless((l), 1, 0)
128133

129134
#define local_dec_return(l) local_sub_return(1, (l))

arch/powerpc/include/asm/local.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,23 @@ static __inline__ long local_xchg(local_t *l, long n)
115115
}
116116

117117
/**
118-
* local_add_unless - add unless the number is a given value
118+
* local_add_unless - add unless the number is already a given value
119119
* @l: pointer of type local_t
120120
* @a: the amount to add to v...
121121
* @u: ...unless v is equal to u.
122122
*
123-
* Atomically adds @a to @l, so long as it was not @u.
124-
* Returns non-zero if @l was not @u, and zero otherwise.
123+
* Atomically adds @a to @l, if @v was not already @u.
124+
* Returns true if the addition was done.
125125
*/
126-
static __inline__ int local_add_unless(local_t *l, long a, long u)
126+
static __inline__ bool local_add_unless(local_t *l, long a, long u)
127127
{
128128
unsigned long flags;
129-
int ret = 0;
129+
bool ret = false;
130130

131131
powerpc_local_irq_pmu_save(flags);
132132
if (l->v != u) {
133133
l->v += a;
134-
ret = 1;
134+
ret = true;
135135
}
136136
powerpc_local_irq_pmu_restore(flags);
137137

arch/x86/include/asm/local.h

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,28 +135,27 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new)
135135
#define local_xchg(l, n) (xchg(&((l)->a.counter), (n)))
136136

137137
/**
138-
* local_add_unless - add unless the number is a given value
138+
* local_add_unless - add unless the number is already a given value
139139
* @l: pointer of type local_t
140140
* @a: the amount to add to l...
141141
* @u: ...unless l is equal to u.
142142
*
143-
* Atomically adds @a to @l, so long as it was not @u.
144-
* Returns non-zero if @l was not @u, and zero otherwise.
143+
* Atomically adds @a to @l, if @v was not already @u.
144+
* Returns true if the addition was done.
145145
*/
146-
#define local_add_unless(l, a, u) \
147-
({ \
148-
long c, old; \
149-
c = local_read((l)); \
150-
for (;;) { \
151-
if (unlikely(c == (u))) \
152-
break; \
153-
old = local_cmpxchg((l), c, c + (a)); \
154-
if (likely(old == c)) \
155-
break; \
156-
c = old; \
157-
} \
158-
c != (u); \
159-
})
146+
static __always_inline bool
147+
local_add_unless(local_t *l, long a, long u)
148+
{
149+
long c = local_read(l);
150+
151+
do {
152+
if (unlikely(c == u))
153+
return false;
154+
} while (!local_try_cmpxchg(l, &c, c + a));
155+
156+
return true;
157+
}
158+
160159
#define local_inc_not_zero(l) local_add_unless((l), 1, 0)
161160

162161
/* On x86_32, these are no better than the atomic variants.

0 commit comments

Comments
 (0)